s3manager-web/internal/app/s3manager/errors.go

41 lines
876 B
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-04-09 16:28:57 +02:00
import (
2017-05-28 19:38:59 +02:00
"encoding/json"
"io"
2017-04-09 16:28:57 +02:00
"log"
"net/http"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-04-09 16:28:57 +02:00
)
2018-06-23 12:49:44 +02:00
// Error codes that may be returned from an S3 client.
const (
ErrBucketDoesNotExist = "The specified bucket does not exist"
ErrKeyDoesNotExist = "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) {
2018-06-23 12:49:44 +02:00
cause := errors.Cause(err)
code := http.StatusInternalServerError
2017-05-29 16:38:48 +02:00
2018-06-23 12:49:44 +02:00
_, ok := cause.(*json.SyntaxError)
if ok {
2017-05-25 18:33:44 +02:00
code = http.StatusUnprocessableEntity
2018-06-23 12:49:44 +02:00
}
switch {
case cause == io.EOF || cause == io.ErrUnexpectedEOF:
code = http.StatusUnprocessableEntity
case cause.Error() == ErrBucketDoesNotExist || cause.Error() == ErrKeyDoesNotExist:
code = http.StatusNotFound
2017-05-25 18:33:44 +02:00
}
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
}
}