Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 0d0d22e

Browse files
committed
errdefs: convert containerd errors to the correct status code
In situations where the containerd error is consumed directly and not received over gRPC, errors were not translated. This patch converts containerd errors to the correct HTTP status code. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit 4a516215e26542b723fe2d74c6c196a366164473) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 7b2d5556d57a9e140c6068a7b7d85e44d28e2d31 Component: engine
1 parent a5906db commit 0d0d22e

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

components/engine/errdefs/http_helpers.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66

7+
containerderrors "github.com/containerd/containerd/errdefs"
78
"github.com/docker/distribution/registry/api/errcode"
89
"github.com/sirupsen/logrus"
910
"google.golang.org/grpc/codes"
@@ -47,6 +48,10 @@ func GetHTTPErrorStatusCode(err error) int {
4748
if statusCode != http.StatusInternalServerError {
4849
return statusCode
4950
}
51+
statusCode = statusCodeFromContainerdError(err)
52+
if statusCode != http.StatusInternalServerError {
53+
return statusCode
54+
}
5055
statusCode = statusCodeFromDistributionError(err)
5156
if statusCode != http.StatusInternalServerError {
5257
return statusCode
@@ -170,3 +175,24 @@ func statusCodeFromDistributionError(err error) int {
170175
}
171176
return http.StatusInternalServerError
172177
}
178+
179+
// statusCodeFromContainerdError returns status code for containerd errors when
180+
// consumed directory (not through gRPC)
181+
func statusCodeFromContainerdError(err error) int {
182+
switch {
183+
case containerderrors.IsInvalidArgument(err):
184+
return http.StatusBadRequest
185+
case containerderrors.IsNotFound(err):
186+
return http.StatusNotFound
187+
case containerderrors.IsAlreadyExists(err):
188+
return http.StatusConflict
189+
case containerderrors.IsFailedPrecondition(err):
190+
return http.StatusPreconditionFailed
191+
case containerderrors.IsUnavailable(err):
192+
return http.StatusServiceUnavailable
193+
case containerderrors.IsNotImplemented(err):
194+
return http.StatusNotImplemented
195+
default:
196+
return http.StatusInternalServerError
197+
}
198+
}

0 commit comments

Comments
 (0)