mirror of
https://github.com/distribution/distribution.git
synced 2025-08-21 00:04:10 +00:00
registry: convert AWS S3 error to correct errcode
This commit is contained in:
parent
a837179414
commit
07956fa8da
@ -3,6 +3,9 @@ package errcode
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
|
"github.com/docker/distribution/registry/storage/driver"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServeJSON attempts to serve the errcode in a JSON envelope. It marshals err
|
// ServeJSON attempts to serve the errcode in a JSON envelope. It marshals err
|
||||||
@ -18,13 +21,27 @@ func ServeJSON(w http.ResponseWriter, err error) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := range errs {
|
||||||
|
if err2, ok := errs[i].(Error); ok {
|
||||||
|
errs[i] = replaceError(err2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err, ok := errs[0].(ErrorCoder); ok {
|
if err, ok := errs[0].(ErrorCoder); ok {
|
||||||
sc = err.ErrorCode().Descriptor().HTTPStatusCode
|
sc = err.ErrorCode().Descriptor().HTTPStatusCode
|
||||||
}
|
}
|
||||||
case ErrorCoder:
|
case ErrorCoder:
|
||||||
|
if err2, ok := errs.(Error); ok {
|
||||||
|
errs = replaceError(err2)
|
||||||
|
}
|
||||||
|
|
||||||
sc = errs.ErrorCode().Descriptor().HTTPStatusCode
|
sc = errs.ErrorCode().Descriptor().HTTPStatusCode
|
||||||
err = Errors{err} // create an envelope.
|
err = Errors{err} // create an envelope.
|
||||||
default:
|
default:
|
||||||
|
if err2, ok := err.(Error); ok {
|
||||||
|
err = replaceError(err2)
|
||||||
|
}
|
||||||
|
|
||||||
// We just have an unhandled error type, so just place in an envelope
|
// We just have an unhandled error type, so just place in an envelope
|
||||||
// and move along.
|
// and move along.
|
||||||
err = Errors{err}
|
err = Errors{err}
|
||||||
@ -38,3 +55,29 @@ func ServeJSON(w http.ResponseWriter, err error) error {
|
|||||||
|
|
||||||
return json.NewEncoder(w).Encode(err)
|
return json.NewEncoder(w).Encode(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func replaceError(e Error) Error {
|
||||||
|
serr, ok := e.Detail.(driver.Error)
|
||||||
|
if !ok {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
err, ok := serr.Enclosed.(awserr.RequestFailure)
|
||||||
|
if !ok {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
code := ErrorCodeUnknown
|
||||||
|
switch err.StatusCode() {
|
||||||
|
case http.StatusForbidden:
|
||||||
|
code = ErrorCodeDenied
|
||||||
|
case http.StatusServiceUnavailable:
|
||||||
|
code = ErrorCodeUnavailable
|
||||||
|
case http.StatusUnauthorized:
|
||||||
|
code = ErrorCodeUnauthorized
|
||||||
|
case http.StatusTooManyRequests:
|
||||||
|
code = ErrorCodeTooManyRequests
|
||||||
|
}
|
||||||
|
|
||||||
|
return code.WithDetail(err.Code())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user