Skip to content

daemon/server/httpstatus: add mappings for missing errdefs types#52530

Open
htoyoda18 wants to merge 1 commit into
moby:masterfrom
htoyoda18:toyo/improve-httpstatus-error-handling
Open

daemon/server/httpstatus: add mappings for missing errdefs types#52530
htoyoda18 wants to merge 1 commit into
moby:masterfrom
htoyoda18:toyo/improve-httpstatus-error-handling

Conversation

@htoyoda18
Copy link
Copy Markdown
Contributor

- What I did

Added HTTP status code mappings for five containerd errdefs error types that were previously unmapped in the FromError function. Without these mappings, these errors incorrectly returned 500 Internal Server Error instead of appropriate status codes.

- How I did it

  1. Added case statements in FromError() for:

    • IsAlreadyExists → 409 Conflict
    • IsFailedPrecondition → 400 Bad Request
    • IsOutOfRange → 400 Bad Request
    • IsAborted → 409 Conflict
    • IsResourceExhausted → 429 Too Many Requests
  2. These mappings align with the existing gRPC error code mappings in statusCodeFromGRPCError.

  3. Added TestAllErrdefsTypesAreMapped to ensure all 16 containerd errdefs types are explicitly handled, preventing future regressions when new error types are added.

- How to verify it

Run the new test:

go test -v ./daemon/server/httpstatus -run TestAllErrdefsTypesAreMapped

All 16 errdefs types should be tested and pass.

- Human readable description for the release notes

Fix incorrect HTTP 500 errors for certain API operations that should return 409 Conflict or 429 Too Many Requests.

- Related issues

This addresses the FIXME comment at #48359 (comment) by ensuring more error types are properly mapped, reducing cases where the "Got an API for which error does not match any expected type" debug log appears.

Add HTTP status mappings for five errdefs types that were previously
unmapped, causing them to incorrectly return 500 Internal Server Error:

- IsAlreadyExists → 409 Conflict
- IsFailedPrecondition → 400 Bad Request
- IsOutOfRange → 400 Bad Request
- IsAborted → 409 Conflict
- IsResourceExhausted → 429 Too Many Requests

Signed-off-by: hiroto.toyoda <hiroto.toyoda@dena.com>
@github-actions github-actions Bot added the area/daemon Core Engine label May 2, 2026
@htoyoda18
Copy link
Copy Markdown
Contributor Author

@thaJeztah Hi! Could you please take a look at this PR when you have a chance? Thank you!

Comment on lines 28 to 55
case cerrdefs.IsNotFound(rerr):
return http.StatusNotFound
case cerrdefs.IsInvalidArgument(rerr):
return http.StatusBadRequest
case cerrdefs.IsConflict(rerr):
return http.StatusConflict
case cerrdefs.IsAlreadyExists(rerr):
return http.StatusConflict
case cerrdefs.IsFailedPrecondition(rerr):
return http.StatusBadRequest
case cerrdefs.IsOutOfRange(rerr):
return http.StatusBadRequest
case cerrdefs.IsAborted(rerr):
return http.StatusConflict
case cerrdefs.IsResourceExhausted(rerr):
return http.StatusTooManyRequests
case cerrdefs.IsUnauthorized(rerr):
return http.StatusUnauthorized
case cerrdefs.IsUnavailable(rerr):
return http.StatusServiceUnavailable
case cerrdefs.IsPermissionDenied(rerr):
return http.StatusForbidden
case cerrdefs.IsNotModified(rerr):
return http.StatusNotModified
case cerrdefs.IsNotImplemented(rerr):
return http.StatusNotImplemented
case cerrdefs.IsInternal(rerr) || cerrdefs.IsDataLoss(rerr) || cerrdefs.IsDeadlineExceeded(rerr) || cerrdefs.IsCanceled(rerr):
return http.StatusInternalServerError
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually wondering now why we're not using the utility that was added in containerd/errdefs; we probably should check if there's any differences in mapping, and we could still keep a wrapper around it, but I think most (if not all) of these should also be handled there; https://github.com/containerd/errdefs/blob/4817405e4a3caeb7aee9dac68ed55339c59cb635/pkg/errhttp/http.go#L32-L66

Comment on lines 57 to 59
if statusCode := statusCodeFromGRPCError(err); statusCode != http.StatusInternalServerError {
return statusCode
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a utility for gRPC errors (but I haven't looked yet how that maps); https://github.com/containerd/errdefs/blob/4817405e4a3caeb7aee9dac68ed55339c59cb635/pkg/errgrpc/grpc.go#L205-L211

@vvoland vvoland modified the milestones: 29.5.0, 29.5.2 May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants