Browse Source

More clear downloading timeout errors; Add image URL to fallback image usage warning

DarthSim 4 years ago
parent
commit
56858a3692
3 changed files with 19 additions and 4 deletions
  1. 3 0
      CHANGELOG.md
  2. 15 3
      download.go
  3. 1 1
      processing_handler.go

+ 3 - 0
CHANGELOG.md

@@ -1,6 +1,9 @@
 # Changelog
 
 ## [Unreleased]
+### Change
+- More clear downloading errors.
+
 ### Fix
 - Fix ICC profile handling in some cases.
 

+ 15 - 3
download.go

@@ -4,6 +4,7 @@ import (
 	"compress/gzip"
 	"context"
 	"crypto/tls"
+	"errors"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -100,6 +101,17 @@ func initDownloading() error {
 	return nil
 }
 
+type httpError interface {
+	Timeout() bool
+}
+
+func checkTimeoutErr(err error) error {
+	if httpErr, ok := err.(httpError); ok && httpErr.Timeout() {
+		return errors.New("The image request timed out")
+	}
+	return err
+}
+
 func checkDimensions(width, height int) error {
 	if conf.MaxSrcDimension > 0 && (width > conf.MaxSrcDimension || height > conf.MaxSrcDimension) {
 		return errSourceDimensionsTooBig
@@ -118,7 +130,7 @@ func checkTypeAndDimensions(r io.Reader) (imageType, error) {
 		return imageTypeUnknown, errSourceImageTypeNotSupported
 	}
 	if err != nil {
-		return imageTypeUnknown, newUnexpectedError(err.Error(), 0)
+		return imageTypeUnknown, newUnexpectedError(checkTimeoutErr(err).Error(), 0)
 	}
 
 	imgtype, imgtypeOk := imageTypes[meta.Format()]
@@ -153,7 +165,7 @@ func readAndCheckImage(r io.Reader, contentLength int) (*imageData, error) {
 
 	if _, err = buf.ReadFrom(r); err != nil {
 		cancel()
-		return nil, newError(404, err.Error(), msgSourceImageIsUnreachable)
+		return nil, newError(404, checkTimeoutErr(err).Error(), msgSourceImageIsUnreachable)
 	}
 
 	return &imageData{buf.Bytes(), imgtype, cancel}, nil
@@ -169,7 +181,7 @@ func requestImage(imageURL string) (*http.Response, error) {
 
 	res, err := downloadClient.Do(req)
 	if err != nil {
-		return res, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
+		return res, newError(404, checkTimeoutErr(err).Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
 	}
 
 	if res.StatusCode != 200 {

+ 1 - 1
processing_handler.go

@@ -185,7 +185,7 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
 			reportError(err, r)
 		}
 
-		logWarning("Could not load image. Using fallback image: %s", err.Error())
+		logWarning("Could not load image %s. Using fallback image. %s", getImageURL(ctx), err.Error())
 		ctx = context.WithValue(ctx, imageDataCtxKey, fallbackImage)
 	}