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"
|
2019-09-05 00:44:02 +02:00
|
|
|
"errors"
|
2017-05-28 19:38:59 +02:00
|
|
|
"io"
|
2017-04-09 16:28:57 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-09-05 00:44:02 +02:00
|
|
|
"strings"
|
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
|
|
|
code := http.StatusInternalServerError
|
2017-05-29 16:38:48 +02:00
|
|
|
|
2019-09-05 00:44:02 +02:00
|
|
|
var se *json.SyntaxError
|
|
|
|
ok := errors.As(err, &se)
|
2018-06-23 12:49:44 +02:00
|
|
|
if ok {
|
2017-05-25 18:33:44 +02:00
|
|
|
code = http.StatusUnprocessableEntity
|
2018-06-23 12:49:44 +02:00
|
|
|
}
|
2019-09-05 00:44:02 +02:00
|
|
|
|
2018-06-23 12:49:44 +02:00
|
|
|
switch {
|
2019-09-05 00:44:02 +02:00
|
|
|
case errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF):
|
2018-06-23 12:49:44 +02:00
|
|
|
code = http.StatusUnprocessableEntity
|
2019-09-05 00:44:02 +02:00
|
|
|
case strings.Contains(err.Error(), ErrBucketDoesNotExist) || strings.Contains(err.Error(), ErrKeyDoesNotExist):
|
2018-06-23 12:49:44 +02:00
|
|
|
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
|
2019-09-05 00:44:02 +02:00
|
|
|
if code >= http.StatusInternalServerError {
|
2017-05-25 18:33:44 +02:00
|
|
|
log.Println(err)
|
2017-04-09 16:28:57 +02:00
|
|
|
}
|
|
|
|
}
|