From a81add5d96a027fc96e5f5f164b26aa62e6133c8 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Sat, 22 Jan 2022 10:29:37 -0800 Subject: [PATCH] lint: improve error handling --- .golangci.yml | 1 + cmd/imageproxy-sign/main.go | 2 +- cmd/imageproxy/main.go | 2 +- imageproxy.go | 2 +- internal/gcscache/gcscache.go | 3 ++- internal/s3cache/s3cache.go | 4 +++- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index fb41023..d08bcd5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,6 +2,7 @@ linters: enable: - dogsled - dupl + - errorlint - goimports - gosec - misspell diff --git a/cmd/imageproxy-sign/main.go b/cmd/imageproxy-sign/main.go index 5024b20..3233418 100644 --- a/cmd/imageproxy-sign/main.go +++ b/cmd/imageproxy-sign/main.go @@ -53,7 +53,7 @@ func sign(key string, s string, urlOnly bool) ([]byte, error) { k, err := parseKey(key) if err != nil { - return nil, fmt.Errorf("error parsing key: %v", err) + return nil, fmt.Errorf("error parsing key: %w", err) } mac := hmac.New(sha256.New, k) diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index 7bf3f55..68bf2ae 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -161,7 +161,7 @@ func parseCache(c string) (imageproxy.Cache, error) { u, err := url.Parse(c) if err != nil { - return nil, fmt.Errorf("error parsing cache flag: %v", err) + return nil, fmt.Errorf("error parsing cache flag: %w", err) } switch u.Scheme { diff --git a/imageproxy.go b/imageproxy.go index a5686cd..37a779f 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -276,7 +276,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) { // the content type. Returns empty string if error occurs. func peekContentType(p *bufio.Reader) string { byt, err := p.Peek(512) - if err != nil && err != bufio.ErrBufferFull && err != io.EOF { + if err != nil && !errors.Is(err, bufio.ErrBufferFull) && !errors.Is(err, io.EOF) { return "" } return http.DetectContentType(byt) diff --git a/internal/gcscache/gcscache.go b/internal/gcscache/gcscache.go index 6eda139..951b334 100644 --- a/internal/gcscache/gcscache.go +++ b/internal/gcscache/gcscache.go @@ -9,6 +9,7 @@ import ( "context" "crypto/md5" "encoding/hex" + "errors" "io" "io/ioutil" "log" @@ -27,7 +28,7 @@ type cache struct { func (c *cache) Get(key string) ([]byte, bool) { r, err := c.object(key).NewReader(ctx) if err != nil { - if err != storage.ErrObjectNotExist { + if !errors.Is(err, storage.ErrObjectNotExist) { log.Printf("error reading from gcs: %v", err) } return nil, false diff --git a/internal/s3cache/s3cache.go b/internal/s3cache/s3cache.go index abfd28a..cdf38d0 100644 --- a/internal/s3cache/s3cache.go +++ b/internal/s3cache/s3cache.go @@ -9,6 +9,7 @@ import ( "bytes" "crypto/md5" "encoding/hex" + "errors" "io" "io/ioutil" "log" @@ -36,7 +37,8 @@ func (c *cache) Get(key string) ([]byte, bool) { resp, err := c.GetObject(input) if err != nil { - if aerr, ok := err.(awserr.Error); ok && aerr.Code() != "NoSuchKey" { + var aerr awserr.Error + if errors.As(err, &aerr) && aerr.Code() != "NoSuchKey" { log.Printf("error fetching from s3: %v", aerr) } return nil, false