Browse Source

Predefine static errors

DarthSim 6 years ago
parent
commit
776f57d003
6 changed files with 29 additions and 18 deletions
  1. 7 2
      crypt.go
  2. 7 3
      download.go
  3. 0 5
      errors.go
  4. 1 1
      etag.go
  5. 8 4
      process.go
  6. 6 3
      server.go

+ 7 - 2
crypt.go

@@ -7,10 +7,15 @@ import (
 	"errors"
 	"errors"
 )
 )
 
 
+var (
+	errInvalidToken       = errors.New("Invalid token")
+	errInvalidURLEncoding = errors.New("Invalid token encoding")
+)
+
 func validatePath(token, path string) error {
 func validatePath(token, path string) error {
 	messageMAC, err := base64.RawURLEncoding.DecodeString(token)
 	messageMAC, err := base64.RawURLEncoding.DecodeString(token)
 	if err != nil {
 	if err != nil {
-		return errors.New("Invalid token encoding")
+		return errInvalidURLEncoding
 	}
 	}
 
 
 	mac := hmac.New(sha256.New, conf.Key)
 	mac := hmac.New(sha256.New, conf.Key)
@@ -19,7 +24,7 @@ func validatePath(token, path string) error {
 	expectedMAC := mac.Sum(nil)
 	expectedMAC := mac.Sum(nil)
 
 
 	if !hmac.Equal(messageMAC, expectedMAC) {
 	if !hmac.Equal(messageMAC, expectedMAC) {
-		return errors.New("Invalid token")
+		return errInvalidToken
 	}
 	}
 
 
 	return nil
 	return nil

+ 7 - 3
download.go

@@ -25,6 +25,10 @@ var (
 	downloadClient  *http.Client
 	downloadClient  *http.Client
 	imageTypeCtxKey = ctxKey("imageType")
 	imageTypeCtxKey = ctxKey("imageType")
 	imageDataCtxKey = ctxKey("imageData")
 	imageDataCtxKey = ctxKey("imageData")
+
+	errSourceDimensionsTooBig      = errors.New("Source image dimensions are too big")
+	errSourceResolutionTooBig      = errors.New("Source image resolution are too big")
+	errSourceImageTypeNotSupported = errors.New("Source image type not supported")
 )
 )
 
 
 var downloadBufPool = sync.Pool{
 var downloadBufPool = sync.Pool{
@@ -95,13 +99,13 @@ func checkTypeAndDimensions(r io.Reader) (imageType, error) {
 		return imageTypeUnknown, err
 		return imageTypeUnknown, err
 	}
 	}
 	if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
 	if imgconf.Width > conf.MaxSrcDimension || imgconf.Height > conf.MaxSrcDimension {
-		return imageTypeUnknown, errors.New("Source image is too big")
+		return imageTypeUnknown, errSourceDimensionsTooBig
 	}
 	}
 	if imgconf.Width*imgconf.Height > conf.MaxSrcResolution {
 	if imgconf.Width*imgconf.Height > conf.MaxSrcResolution {
-		return imageTypeUnknown, errors.New("Source image is too big")
+		return imageTypeUnknown, errSourceResolutionTooBig
 	}
 	}
 	if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
 	if !imgtypeOk || !vipsTypeSupportLoad[imgtype] {
-		return imageTypeUnknown, errors.New("Source image type not supported")
+		return imageTypeUnknown, errSourceImageTypeNotSupported
 	}
 	}
 
 
 	return imgtype, nil
 	return imgtype, nil

+ 0 - 5
errors.go

@@ -26,11 +26,6 @@ func newUnexpectedError(err error, skip int) imgproxyError {
 	return imgproxyError{500, msg, "Internal error"}
 	return imgproxyError{500, msg, "Internal error"}
 }
 }
 
 
-var (
-	invalidSecretErr = newError(403, "Invalid secret", "Forbidden")
-	invalidMethodErr = newError(422, "Invalid request method", "Method doesn't allowed")
-)
-
 func stacktrace(skip int) string {
 func stacktrace(skip int) string {
 	callers := make([]uintptr, 10)
 	callers := make([]uintptr, 10)
 	n := runtime.Callers(skip+1, callers)
 	n := runtime.Callers(skip+1, callers)

+ 1 - 1
etag.go

@@ -9,7 +9,7 @@ import (
 	"sync"
 	"sync"
 )
 )
 
 
-var notModifiedErr = newError(304, "Not modified", "Not modified")
+var errNotModified = newError(304, "Not modified", "Not modified")
 
 
 type eTagCalc struct {
 type eTagCalc struct {
 	hash hash.Hash
 	hash hash.Hash

+ 8 - 4
process.go

@@ -17,9 +17,13 @@ import (
 	"unsafe"
 	"unsafe"
 )
 )
 
 
-var vipsSupportSmartcrop bool
-var vipsTypeSupportLoad = make(map[imageType]bool)
-var vipsTypeSupportSave = make(map[imageType]bool)
+var (
+	vipsSupportSmartcrop bool
+	vipsTypeSupportLoad  = make(map[imageType]bool)
+	vipsTypeSupportSave  = make(map[imageType]bool)
+
+	errSmartCropNotSupported = errors.New("Smart crop is not supported by used version of libvips")
+)
 
 
 type cConfig struct {
 type cConfig struct {
 	Quality         C.int
 	Quality         C.int
@@ -212,7 +216,7 @@ func processImage(ctx context.Context) ([]byte, error) {
 	imgtype := getImageType(ctx)
 	imgtype := getImageType(ctx)
 
 
 	if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {
 	if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {
-		return nil, errors.New("Smart crop is not supported by used version of libvips")
+		return nil, errSmartCropNotSupported
 	}
 	}
 
 
 	img, err := vipsLoadImage(data, imgtype, 1)
 	img, err := vipsLoadImage(data, imgtype, 1)

+ 6 - 3
server.go

@@ -25,6 +25,9 @@ var (
 	healthRequestURI = []byte("/health")
 	healthRequestURI = []byte("/health")
 
 
 	serverMutex mutex
 	serverMutex mutex
+
+	errInvalidMethod = newError(422, "Invalid request method", "Method doesn't allowed")
+	errInvalidSecret = newError(403, "Invalid secret", "Forbidden")
 )
 )
 
 
 func startServer() *fasthttp.Server {
 func startServer() *fasthttp.Server {
@@ -146,11 +149,11 @@ func serveHTTP(rctx *fasthttp.RequestCtx) {
 	}
 	}
 
 
 	if !rctx.IsGet() {
 	if !rctx.IsGet() {
-		panic(invalidMethodErr)
+		panic(errInvalidMethod)
 	}
 	}
 
 
 	if !checkSecret(rctx) {
 	if !checkSecret(rctx) {
-		panic(invalidSecretErr)
+		panic(errInvalidSecret)
 	}
 	}
 
 
 	serverMutex.Lock()
 	serverMutex.Lock()
@@ -183,7 +186,7 @@ func serveHTTP(rctx *fasthttp.RequestCtx) {
 		rctx.Response.Header.SetBytesV("ETag", eTag)
 		rctx.Response.Header.SetBytesV("ETag", eTag)
 
 
 		if bytes.Equal(eTag, rctx.Request.Header.Peek("If-None-Match")) {
 		if bytes.Equal(eTag, rctx.Request.Header.Peek("If-None-Match")) {
-			panic(notModifiedErr)
+			panic(errNotModified)
 		}
 		}
 	}
 	}