s3manager-web/errors.go

46 lines
1.2 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-04-09 16:28:57 +02:00
import (
"log"
"net/http"
2017-05-25 18:33:44 +02:00
"strings"
"github.com/pkg/errors"
2017-04-09 16:28:57 +02:00
)
2017-05-25 18:33:44 +02:00
// Error codes commonly used throughout the application
2017-04-09 16:28:57 +02:00
const (
2017-05-25 18:33:44 +02:00
errDecodingBody = "error decoding body JSON"
errEncodingJSON = "error encoding JSON"
errExecutingTemplate = "error executing template"
errParsingForm = "error parsing form"
errParsingTemplates = "error parsing template files"
)
// Errors that may be returned from an S3 client.
var (
ErrBucketDoesNotExist = errors.New("The specified bucket does not exist.")
ErrKeyDoesNotExist = errors.New("The specified key does not exist.")
2017-04-09 16:28:57 +02:00
)
2017-05-08 23:07:07 +02:00
// handleHTTPError handles HTTP errors.
2017-05-25 18:33:44 +02:00
func handleHTTPError(w http.ResponseWriter, err error) {
code := http.StatusInternalServerError
if errors.Cause(err) == ErrBucketDoesNotExist {
code = http.StatusNotFound
} else if errors.Cause(err) == ErrKeyDoesNotExist {
code = http.StatusNotFound
} else if strings.Contains(err.Error(), errDecodingBody) {
code = http.StatusUnprocessableEntity
} else if strings.Contains(err.Error(), errParsingForm) {
code = http.StatusUnprocessableEntity
}
http.Error(w, http.StatusText(code), code)
2017-04-09 16:28:57 +02:00
2017-05-25 18:33:44 +02:00
// Log if server error
if code >= 500 {
log.Println(err)
2017-04-09 16:28:57 +02:00
}
}