Signed-off-by: Derek McGowan <derek@mcg.dev>
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,124 +0,0 @@ |
| 1 |
-package httpstatus |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "context" |
|
| 5 |
- "fmt" |
|
| 6 |
- "net/http" |
|
| 7 |
- |
|
| 8 |
- cerrdefs "github.com/containerd/errdefs" |
|
| 9 |
- "github.com/containerd/log" |
|
| 10 |
- "github.com/docker/distribution/registry/api/errcode" |
|
| 11 |
- "google.golang.org/grpc/codes" |
|
| 12 |
- "google.golang.org/grpc/status" |
|
| 13 |
-) |
|
| 14 |
- |
|
| 15 |
-// FromError retrieves status code from error message. |
|
| 16 |
-func FromError(err error) int {
|
|
| 17 |
- if err == nil {
|
|
| 18 |
- log.G(context.TODO()).WithError(err).Error("unexpected HTTP error handling")
|
|
| 19 |
- return http.StatusInternalServerError |
|
| 20 |
- } |
|
| 21 |
- |
|
| 22 |
- // Resolve the error to ensure status is chosen from the first outermost error |
|
| 23 |
- rerr := cerrdefs.Resolve(err) |
|
| 24 |
- |
|
| 25 |
- // Note that the below functions are already checking the error causal chain for matches. |
|
| 26 |
- // Only check errors from the errdefs package, no new error type checking may be added |
|
| 27 |
- switch {
|
|
| 28 |
- case cerrdefs.IsNotFound(rerr): |
|
| 29 |
- return http.StatusNotFound |
|
| 30 |
- case cerrdefs.IsInvalidArgument(rerr): |
|
| 31 |
- return http.StatusBadRequest |
|
| 32 |
- case cerrdefs.IsConflict(rerr): |
|
| 33 |
- return http.StatusConflict |
|
| 34 |
- case cerrdefs.IsUnauthorized(rerr): |
|
| 35 |
- return http.StatusUnauthorized |
|
| 36 |
- case cerrdefs.IsUnavailable(rerr): |
|
| 37 |
- return http.StatusServiceUnavailable |
|
| 38 |
- case cerrdefs.IsPermissionDenied(rerr): |
|
| 39 |
- return http.StatusForbidden |
|
| 40 |
- case cerrdefs.IsNotModified(rerr): |
|
| 41 |
- return http.StatusNotModified |
|
| 42 |
- case cerrdefs.IsNotImplemented(rerr): |
|
| 43 |
- return http.StatusNotImplemented |
|
| 44 |
- case cerrdefs.IsInternal(rerr) || cerrdefs.IsDataLoss(rerr) || cerrdefs.IsDeadlineExceeded(rerr) || cerrdefs.IsCanceled(rerr): |
|
| 45 |
- return http.StatusInternalServerError |
|
| 46 |
- default: |
|
| 47 |
- if statusCode := statusCodeFromGRPCError(err); statusCode != http.StatusInternalServerError {
|
|
| 48 |
- return statusCode |
|
| 49 |
- } |
|
| 50 |
- if statusCode := statusCodeFromDistributionError(err); statusCode != http.StatusInternalServerError {
|
|
| 51 |
- return statusCode |
|
| 52 |
- } |
|
| 53 |
- switch e := err.(type) {
|
|
| 54 |
- case interface{ Unwrap() error }:
|
|
| 55 |
- return FromError(e.Unwrap()) |
|
| 56 |
- case interface{ Unwrap() []error }:
|
|
| 57 |
- for _, ue := range e.Unwrap() {
|
|
| 58 |
- if statusCode := FromError(ue); statusCode != http.StatusInternalServerError {
|
|
| 59 |
- return statusCode |
|
| 60 |
- } |
|
| 61 |
- } |
|
| 62 |
- } |
|
| 63 |
- |
|
| 64 |
- if !cerrdefs.IsUnknown(err) {
|
|
| 65 |
- log.G(context.TODO()).WithFields(log.Fields{
|
|
| 66 |
- "module": "api", |
|
| 67 |
- "error": err, |
|
| 68 |
- "error_type": fmt.Sprintf("%T", err),
|
|
| 69 |
- }).Debug("FIXME: Got an API for which error does not match any expected type!!!")
|
|
| 70 |
- } |
|
| 71 |
- |
|
| 72 |
- return http.StatusInternalServerError |
|
| 73 |
- } |
|
| 74 |
-} |
|
| 75 |
- |
|
| 76 |
-// statusCodeFromGRPCError returns status code according to gRPC error |
|
| 77 |
-func statusCodeFromGRPCError(err error) int {
|
|
| 78 |
- switch status.Code(err) {
|
|
| 79 |
- case codes.InvalidArgument: // code 3 |
|
| 80 |
- return http.StatusBadRequest |
|
| 81 |
- case codes.NotFound: // code 5 |
|
| 82 |
- return http.StatusNotFound |
|
| 83 |
- case codes.AlreadyExists: // code 6 |
|
| 84 |
- return http.StatusConflict |
|
| 85 |
- case codes.PermissionDenied: // code 7 |
|
| 86 |
- return http.StatusForbidden |
|
| 87 |
- case codes.FailedPrecondition: // code 9 |
|
| 88 |
- return http.StatusBadRequest |
|
| 89 |
- case codes.Unauthenticated: // code 16 |
|
| 90 |
- return http.StatusUnauthorized |
|
| 91 |
- case codes.OutOfRange: // code 11 |
|
| 92 |
- return http.StatusBadRequest |
|
| 93 |
- case codes.Unimplemented: // code 12 |
|
| 94 |
- return http.StatusNotImplemented |
|
| 95 |
- case codes.Unavailable: // code 14 |
|
| 96 |
- return http.StatusServiceUnavailable |
|
| 97 |
- default: |
|
| 98 |
- // codes.Canceled(1) |
|
| 99 |
- // codes.Unknown(2) |
|
| 100 |
- // codes.DeadlineExceeded(4) |
|
| 101 |
- // codes.ResourceExhausted(8) |
|
| 102 |
- // codes.Aborted(10) |
|
| 103 |
- // codes.Internal(13) |
|
| 104 |
- // codes.DataLoss(15) |
|
| 105 |
- return http.StatusInternalServerError |
|
| 106 |
- } |
|
| 107 |
-} |
|
| 108 |
- |
|
| 109 |
-// statusCodeFromDistributionError returns status code according to registry errcode |
|
| 110 |
-// code is loosely based on errcode.ServeJSON() in docker/distribution |
|
| 111 |
-func statusCodeFromDistributionError(err error) int {
|
|
| 112 |
- switch errs := err.(type) {
|
|
| 113 |
- case errcode.Errors: |
|
| 114 |
- if len(errs) < 1 {
|
|
| 115 |
- return http.StatusInternalServerError |
|
| 116 |
- } |
|
| 117 |
- if _, ok := errs[0].(errcode.ErrorCoder); ok {
|
|
| 118 |
- return statusCodeFromDistributionError(errs[0]) |
|
| 119 |
- } |
|
| 120 |
- case errcode.ErrorCoder: |
|
| 121 |
- return errs.ErrorCode().Descriptor().HTTPStatusCode |
|
| 122 |
- } |
|
| 123 |
- return http.StatusInternalServerError |
|
| 124 |
-} |
| ... | ... |
@@ -5,10 +5,10 @@ import ( |
| 5 | 5 |
"net/http" |
| 6 | 6 |
|
| 7 | 7 |
"github.com/containerd/log" |
| 8 |
- "github.com/docker/docker/api/server/httpstatus" |
|
| 9 | 8 |
"github.com/docker/docker/api/server/httputils" |
| 10 | 9 |
"github.com/docker/docker/api/types" |
| 11 | 10 |
"github.com/docker/docker/api/types/versions" |
| 11 |
+ "github.com/docker/docker/daemon/server/httpstatus" |
|
| 12 | 12 |
"github.com/docker/docker/daemon/server/middleware" |
| 13 | 13 |
"github.com/docker/docker/daemon/server/router" |
| 14 | 14 |
"github.com/docker/docker/dockerversion" |
| 15 | 15 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,124 @@ |
| 0 |
+package httpstatus |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "net/http" |
|
| 6 |
+ |
|
| 7 |
+ cerrdefs "github.com/containerd/errdefs" |
|
| 8 |
+ "github.com/containerd/log" |
|
| 9 |
+ "github.com/docker/distribution/registry/api/errcode" |
|
| 10 |
+ "google.golang.org/grpc/codes" |
|
| 11 |
+ "google.golang.org/grpc/status" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+// FromError retrieves status code from error message. |
|
| 15 |
+func FromError(err error) int {
|
|
| 16 |
+ if err == nil {
|
|
| 17 |
+ log.G(context.TODO()).WithError(err).Error("unexpected HTTP error handling")
|
|
| 18 |
+ return http.StatusInternalServerError |
|
| 19 |
+ } |
|
| 20 |
+ |
|
| 21 |
+ // Resolve the error to ensure status is chosen from the first outermost error |
|
| 22 |
+ rerr := cerrdefs.Resolve(err) |
|
| 23 |
+ |
|
| 24 |
+ // Note that the below functions are already checking the error causal chain for matches. |
|
| 25 |
+ // Only check errors from the errdefs package, no new error type checking may be added |
|
| 26 |
+ switch {
|
|
| 27 |
+ case cerrdefs.IsNotFound(rerr): |
|
| 28 |
+ return http.StatusNotFound |
|
| 29 |
+ case cerrdefs.IsInvalidArgument(rerr): |
|
| 30 |
+ return http.StatusBadRequest |
|
| 31 |
+ case cerrdefs.IsConflict(rerr): |
|
| 32 |
+ return http.StatusConflict |
|
| 33 |
+ case cerrdefs.IsUnauthorized(rerr): |
|
| 34 |
+ return http.StatusUnauthorized |
|
| 35 |
+ case cerrdefs.IsUnavailable(rerr): |
|
| 36 |
+ return http.StatusServiceUnavailable |
|
| 37 |
+ case cerrdefs.IsPermissionDenied(rerr): |
|
| 38 |
+ return http.StatusForbidden |
|
| 39 |
+ case cerrdefs.IsNotModified(rerr): |
|
| 40 |
+ return http.StatusNotModified |
|
| 41 |
+ case cerrdefs.IsNotImplemented(rerr): |
|
| 42 |
+ return http.StatusNotImplemented |
|
| 43 |
+ case cerrdefs.IsInternal(rerr) || cerrdefs.IsDataLoss(rerr) || cerrdefs.IsDeadlineExceeded(rerr) || cerrdefs.IsCanceled(rerr): |
|
| 44 |
+ return http.StatusInternalServerError |
|
| 45 |
+ default: |
|
| 46 |
+ if statusCode := statusCodeFromGRPCError(err); statusCode != http.StatusInternalServerError {
|
|
| 47 |
+ return statusCode |
|
| 48 |
+ } |
|
| 49 |
+ if statusCode := statusCodeFromDistributionError(err); statusCode != http.StatusInternalServerError {
|
|
| 50 |
+ return statusCode |
|
| 51 |
+ } |
|
| 52 |
+ switch e := err.(type) {
|
|
| 53 |
+ case interface{ Unwrap() error }:
|
|
| 54 |
+ return FromError(e.Unwrap()) |
|
| 55 |
+ case interface{ Unwrap() []error }:
|
|
| 56 |
+ for _, ue := range e.Unwrap() {
|
|
| 57 |
+ if statusCode := FromError(ue); statusCode != http.StatusInternalServerError {
|
|
| 58 |
+ return statusCode |
|
| 59 |
+ } |
|
| 60 |
+ } |
|
| 61 |
+ } |
|
| 62 |
+ |
|
| 63 |
+ if !cerrdefs.IsUnknown(err) {
|
|
| 64 |
+ log.G(context.TODO()).WithFields(log.Fields{
|
|
| 65 |
+ "module": "api", |
|
| 66 |
+ "error": err, |
|
| 67 |
+ "error_type": fmt.Sprintf("%T", err),
|
|
| 68 |
+ }).Debug("FIXME: Got an API for which error does not match any expected type!!!")
|
|
| 69 |
+ } |
|
| 70 |
+ |
|
| 71 |
+ return http.StatusInternalServerError |
|
| 72 |
+ } |
|
| 73 |
+} |
|
| 74 |
+ |
|
| 75 |
+// statusCodeFromGRPCError returns status code according to gRPC error |
|
| 76 |
+func statusCodeFromGRPCError(err error) int {
|
|
| 77 |
+ switch status.Code(err) {
|
|
| 78 |
+ case codes.InvalidArgument: // code 3 |
|
| 79 |
+ return http.StatusBadRequest |
|
| 80 |
+ case codes.NotFound: // code 5 |
|
| 81 |
+ return http.StatusNotFound |
|
| 82 |
+ case codes.AlreadyExists: // code 6 |
|
| 83 |
+ return http.StatusConflict |
|
| 84 |
+ case codes.PermissionDenied: // code 7 |
|
| 85 |
+ return http.StatusForbidden |
|
| 86 |
+ case codes.FailedPrecondition: // code 9 |
|
| 87 |
+ return http.StatusBadRequest |
|
| 88 |
+ case codes.Unauthenticated: // code 16 |
|
| 89 |
+ return http.StatusUnauthorized |
|
| 90 |
+ case codes.OutOfRange: // code 11 |
|
| 91 |
+ return http.StatusBadRequest |
|
| 92 |
+ case codes.Unimplemented: // code 12 |
|
| 93 |
+ return http.StatusNotImplemented |
|
| 94 |
+ case codes.Unavailable: // code 14 |
|
| 95 |
+ return http.StatusServiceUnavailable |
|
| 96 |
+ default: |
|
| 97 |
+ // codes.Canceled(1) |
|
| 98 |
+ // codes.Unknown(2) |
|
| 99 |
+ // codes.DeadlineExceeded(4) |
|
| 100 |
+ // codes.ResourceExhausted(8) |
|
| 101 |
+ // codes.Aborted(10) |
|
| 102 |
+ // codes.Internal(13) |
|
| 103 |
+ // codes.DataLoss(15) |
|
| 104 |
+ return http.StatusInternalServerError |
|
| 105 |
+ } |
|
| 106 |
+} |
|
| 107 |
+ |
|
| 108 |
+// statusCodeFromDistributionError returns status code according to registry errcode |
|
| 109 |
+// code is loosely based on errcode.ServeJSON() in docker/distribution |
|
| 110 |
+func statusCodeFromDistributionError(err error) int {
|
|
| 111 |
+ switch errs := err.(type) {
|
|
| 112 |
+ case errcode.Errors: |
|
| 113 |
+ if len(errs) < 1 {
|
|
| 114 |
+ return http.StatusInternalServerError |
|
| 115 |
+ } |
|
| 116 |
+ if _, ok := errs[0].(errcode.ErrorCoder); ok {
|
|
| 117 |
+ return statusCodeFromDistributionError(errs[0]) |
|
| 118 |
+ } |
|
| 119 |
+ case errcode.ErrorCoder: |
|
| 120 |
+ return errs.ErrorCode().Descriptor().HTTPStatusCode |
|
| 121 |
+ } |
|
| 122 |
+ return http.StatusInternalServerError |
|
| 123 |
+} |
| ... | ... |
@@ -9,8 +9,8 @@ import ( |
| 9 | 9 |
"strings" |
| 10 | 10 |
|
| 11 | 11 |
"github.com/containerd/log" |
| 12 |
- "github.com/docker/docker/api/server/httpstatus" |
|
| 13 | 12 |
"github.com/docker/docker/api/server/httputils" |
| 13 |
+ "github.com/docker/docker/daemon/server/httpstatus" |
|
| 14 | 14 |
"github.com/docker/docker/pkg/ioutils" |
| 15 | 15 |
"github.com/sirupsen/logrus" |
| 16 | 16 |
) |
| ... | ... |
@@ -12,7 +12,6 @@ import ( |
| 12 | 12 |
|
| 13 | 13 |
"github.com/containerd/log" |
| 14 | 14 |
"github.com/containerd/platforms" |
| 15 |
- "github.com/docker/docker/api/server/httpstatus" |
|
| 16 | 15 |
"github.com/docker/docker/api/server/httputils" |
| 17 | 16 |
"github.com/docker/docker/api/types" |
| 18 | 17 |
"github.com/docker/docker/api/types/backend" |
| ... | ... |
@@ -22,6 +21,7 @@ import ( |
| 22 | 22 |
"github.com/docker/docker/api/types/network" |
| 23 | 23 |
"github.com/docker/docker/api/types/versions" |
| 24 | 24 |
networkSettings "github.com/docker/docker/daemon/network" |
| 25 |
+ "github.com/docker/docker/daemon/server/httpstatus" |
|
| 25 | 26 |
"github.com/docker/docker/errdefs" |
| 26 | 27 |
"github.com/docker/docker/libnetwork/netlabel" |
| 27 | 28 |
"github.com/docker/docker/pkg/ioutils" |