0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05:00

lint: improve error handling

This commit is contained in:
Will Norris 2022-01-22 10:29:37 -08:00
parent ef8aec77a9
commit a81add5d96
6 changed files with 9 additions and 5 deletions

View file

@ -2,6 +2,7 @@ linters:
enable: enable:
- dogsled - dogsled
- dupl - dupl
- errorlint
- goimports - goimports
- gosec - gosec
- misspell - misspell

View file

@ -53,7 +53,7 @@ func sign(key string, s string, urlOnly bool) ([]byte, error) {
k, err := parseKey(key) k, err := parseKey(key)
if err != nil { 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) mac := hmac.New(sha256.New, k)

View file

@ -161,7 +161,7 @@ func parseCache(c string) (imageproxy.Cache, error) {
u, err := url.Parse(c) u, err := url.Parse(c)
if err != nil { 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 { switch u.Scheme {

View file

@ -276,7 +276,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
// the content type. Returns empty string if error occurs. // the content type. Returns empty string if error occurs.
func peekContentType(p *bufio.Reader) string { func peekContentType(p *bufio.Reader) string {
byt, err := p.Peek(512) 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 ""
} }
return http.DetectContentType(byt) return http.DetectContentType(byt)

View file

@ -9,6 +9,7 @@ import (
"context" "context"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -27,7 +28,7 @@ type cache struct {
func (c *cache) Get(key string) ([]byte, bool) { func (c *cache) Get(key string) ([]byte, bool) {
r, err := c.object(key).NewReader(ctx) r, err := c.object(key).NewReader(ctx)
if err != nil { if err != nil {
if err != storage.ErrObjectNotExist { if !errors.Is(err, storage.ErrObjectNotExist) {
log.Printf("error reading from gcs: %v", err) log.Printf("error reading from gcs: %v", err)
} }
return nil, false return nil, false

View file

@ -9,6 +9,7 @@ import (
"bytes" "bytes"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -36,7 +37,8 @@ func (c *cache) Get(key string) ([]byte, bool) {
resp, err := c.GetObject(input) resp, err := c.GetObject(input)
if err != nil { 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) log.Printf("error fetching from s3: %v", aerr)
} }
return nil, false return nil, false