42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package httphelpers
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/minio/minio-go/v6"
|
|
)
|
|
|
|
var ErrorBadRequest = errors.New("bad request")
|
|
var ErrorMethodNotAllowed = errors.New("method not allowed")
|
|
|
|
func ErrorResponse(w http.ResponseWriter, err error) {
|
|
if err == nil {
|
|
panic("Sneaky, you called ErrorResponse without an error. I shall destroy you")
|
|
}
|
|
errorMessage := err.Error()
|
|
errorStatus := http.StatusInternalServerError
|
|
|
|
if errors.Is(err, ErrorBadRequest) {
|
|
errorStatus = http.StatusBadRequest
|
|
}
|
|
if errors.Is(err, ErrorMethodNotAllowed) {
|
|
errorStatus = http.StatusMethodNotAllowed
|
|
}
|
|
|
|
var minioErrorResponse minio.ErrorResponse
|
|
if errors.As(err, &minioErrorResponse) {
|
|
if minioErrorResponse.Code == "NoSuchKey" {
|
|
errorStatus = http.StatusNotFound
|
|
}
|
|
if minioErrorResponse.Code == "NoSuchBucket" {
|
|
errorStatus = http.StatusNotFound
|
|
}
|
|
// Do not handle minio AccessDenied: that's a server error. Our credentials should always be correct
|
|
}
|
|
|
|
// Let Header.Write() handle escaping
|
|
w.Header().Add("X-Debug-Error", errorMessage)
|
|
w.WriteHeader(errorStatus)
|
|
}
|