瀏覽代碼

Mark downloading errors as unexpected

DarthSim 5 年之前
父節點
當前提交
de31ee50d8
共有 2 個文件被更改,包括 8 次插入3 次删除
  1. 3 3
      download.go
  2. 5 0
      errors.go

+ 3 - 3
download.go

@@ -171,7 +171,7 @@ func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, er
 
 	req, err := http.NewRequest("GET", url, nil)
 	if err != nil {
-		return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable)
+		return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable).MarkAsUnexpected()
 	}
 
 	req.Header.Set("User-Agent", conf.UserAgent)
@@ -181,13 +181,13 @@ func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, er
 		defer res.Body.Close()
 	}
 	if err != nil {
-		return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable)
+		return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable).MarkAsUnexpected()
 	}
 
 	if res.StatusCode != 200 {
 		body, _ := ioutil.ReadAll(res.Body)
 		msg := fmt.Sprintf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
-		return ctx, func() {}, newError(404, msg, msgSourceImageIsUnreachable)
+		return ctx, func() {}, newError(404, msg, msgSourceImageIsUnreachable).MarkAsUnexpected()
 	}
 
 	return readAndCheckImage(ctx, res)

+ 5 - 0
errors.go

@@ -31,6 +31,11 @@ func (e *imgproxyError) StackTrace() []uintptr {
 	return e.stack
 }
 
+func (e *imgproxyError) MarkAsUnexpected() *imgproxyError {
+	e.Unexpected = true
+	return e
+}
+
 func newError(status int, msg string, pub string) *imgproxyError {
 	return &imgproxyError{
 		StatusCode:    status,