Переглянути джерело

GCS transport should return 404 if bucket/object is not found

DarthSim 3 роки тому
батько
коміт
6cc887dfc4
2 змінених файлів з 22 додано та 2 видалено
  1. 1 0
      CHANGELOG.md
  2. 21 2
      transport/gcs/gcs.go

+ 1 - 0
CHANGELOG.md

@@ -21,6 +21,7 @@
 ### Fix
 - Fix trimming of CMYK images.
 - Respond with 404 when the source image can not be found in OpenStack Object Storage.
+- Respond with 404 when file wasn't found in the GCS storage.
 
 ## [3.6.0] - 2022-06-13
 ### Add

+ 21 - 2
transport/gcs/gcs.go

@@ -3,6 +3,7 @@ package gcs
 import (
 	"context"
 	"fmt"
+	"io"
 	"net/http"
 	"strconv"
 	"strings"
@@ -62,7 +63,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
 	if config.ETagEnabled {
 		attrs, err := obj.Attrs(req.Context())
 		if err != nil {
-			return nil, err
+			return handleError(req, err)
 		}
 		header.Set("ETag", attrs.Etag)
 
@@ -83,7 +84,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
 
 	reader, err := obj.NewReader(req.Context())
 	if err != nil {
-		return nil, err
+		return handleError(req, err)
 	}
 
 	header.Set("Cache-Control", reader.Attrs.CacheControl)
@@ -101,3 +102,21 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
 		Request:       req,
 	}, nil
 }
+
+func handleError(req *http.Request, err error) (*http.Response, error) {
+	if err != storage.ErrBucketNotExist && err != storage.ErrObjectNotExist {
+		return nil, err
+	}
+
+	return &http.Response{
+		StatusCode:    http.StatusNotFound,
+		Proto:         "HTTP/1.0",
+		ProtoMajor:    1,
+		ProtoMinor:    0,
+		Header:        make(http.Header),
+		ContentLength: int64(len(err.Error())),
+		Body:          io.NopCloser(strings.NewReader(err.Error())),
+		Close:         false,
+		Request:       req,
+	}, nil
+}