143 lines
4.7 KiB
Go
143 lines
4.7 KiB
Go
// Package problem writes RFC 9457 problem details for the public HTTP API.
|
|
//
|
|
// Every public error body is an api.Problem with a stable api.ProblemCode.
|
|
// The status, title, and detail of each code are fixed here, so a response
|
|
// never carries internal error text or echoed request values.
|
|
package problem
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
kitmiddleware "github.com/wahrwelt-kit/go-httpkit/httputil/middleware"
|
|
|
|
"lincodex/backend/internal/adapter/primary/http/api"
|
|
)
|
|
|
|
// MediaType is the RFC 9457 media type of every public error response.
|
|
const MediaType = "application/problem+json"
|
|
|
|
// typePrefix forms the problem type URI; the contract documents this form.
|
|
const typePrefix = "urn:lincodex:problem:"
|
|
|
|
type definition struct {
|
|
status int32
|
|
title string
|
|
detail string
|
|
}
|
|
|
|
var definitions = map[api.ProblemCode]definition{
|
|
api.ProblemCodeValidationFailed: {
|
|
status: http.StatusBadRequest,
|
|
title: "Request validation failed",
|
|
detail: "One or more request fields are invalid.",
|
|
},
|
|
api.ProblemCodeUnauthenticated: {
|
|
status: http.StatusUnauthorized,
|
|
title: "Authentication required",
|
|
detail: "Sign in and repeat the request.",
|
|
},
|
|
api.ProblemCodeForbidden: {
|
|
status: http.StatusForbidden,
|
|
title: "Access denied",
|
|
detail: "You do not have permission to perform this operation.",
|
|
},
|
|
api.ProblemCodeNotFound: {
|
|
status: http.StatusNotFound,
|
|
title: "Resource not found",
|
|
detail: "The requested resource does not exist.",
|
|
},
|
|
api.ProblemCodeMethodNotAllowed: {
|
|
status: http.StatusMethodNotAllowed,
|
|
title: "Method not allowed",
|
|
detail: "The resource does not support this method; see the Allow header.",
|
|
},
|
|
api.ProblemCodeIdempotencyInProgress: {
|
|
status: http.StatusConflict,
|
|
title: "Request with this idempotency key is in progress",
|
|
detail: "Retry after the original request completes.",
|
|
},
|
|
api.ProblemCodeStateConflict: {
|
|
status: http.StatusConflict,
|
|
title: "Resource state conflict",
|
|
detail: "The resource is in a state that does not allow this operation.",
|
|
},
|
|
api.ProblemCodeVersionConflict: {
|
|
status: http.StatusPreconditionFailed,
|
|
title: "Resource version conflict",
|
|
detail: "The resource changed after it was read. Read it again before retrying.",
|
|
},
|
|
api.ProblemCodePayloadTooLarge: {
|
|
status: http.StatusRequestEntityTooLarge,
|
|
title: "Request body too large",
|
|
detail: "Reduce the request body size and retry.",
|
|
},
|
|
api.ProblemCodeUnsupportedMediaType: {
|
|
status: http.StatusUnsupportedMediaType,
|
|
title: "Unsupported media type",
|
|
detail: "Send the request body with a media type that the operation accepts.",
|
|
},
|
|
api.ProblemCodeIdempotencyKeyReused: {
|
|
status: http.StatusUnprocessableEntity,
|
|
title: "Idempotency key reused with a different request",
|
|
detail: "Use a new Idempotency-Key for a different request.",
|
|
},
|
|
api.ProblemCodePreconditionRequired: {
|
|
status: http.StatusPreconditionRequired,
|
|
title: "Precondition required",
|
|
detail: "Send If-Match with the ETag of the version you read.",
|
|
},
|
|
api.ProblemCodeRateLimited: {
|
|
status: http.StatusTooManyRequests,
|
|
title: "Too many requests",
|
|
detail: "Wait for the Retry-After interval and retry.",
|
|
},
|
|
api.ProblemCodeInternal: {
|
|
status: http.StatusInternalServerError,
|
|
title: "Internal server error",
|
|
detail: "The server could not complete the request.",
|
|
},
|
|
api.ProblemCodeNotImplemented: {
|
|
status: http.StatusNotImplemented,
|
|
title: "Operation not implemented",
|
|
detail: "This operation is not available yet.",
|
|
},
|
|
}
|
|
|
|
// New returns the problem response for code with its fixed status, title,
|
|
// and detail, and the request ID of ctx as trace_id. An unknown code yields
|
|
// the internal problem, so a caller cannot produce an undocumented status.
|
|
func New(ctx context.Context, code api.ProblemCode) *api.ProblemStatusCode {
|
|
known, ok := definitions[code]
|
|
if !ok {
|
|
code = api.ProblemCodeInternal
|
|
known = definitions[code]
|
|
}
|
|
response := api.Problem{
|
|
Type: typePrefix + string(code),
|
|
Title: known.title,
|
|
Status: known.status,
|
|
Code: code,
|
|
Detail: api.NewOptString(known.detail),
|
|
}
|
|
if requestID := kitmiddleware.GetRequestID(ctx); requestID != "" {
|
|
response.TraceID = api.NewOptString(requestID)
|
|
}
|
|
return &api.ProblemStatusCode{StatusCode: int(known.status), Response: response}
|
|
}
|
|
|
|
// Write sends problem as an application/problem+json response.
|
|
func Write(w http.ResponseWriter, problem *api.ProblemStatusCode) {
|
|
body, err := problem.Response.MarshalJSON()
|
|
if err != nil {
|
|
// The generated encoder does not fail for a Problem value; keep the
|
|
// status so the client still sees an error.
|
|
w.WriteHeader(problem.StatusCode)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", MediaType)
|
|
w.WriteHeader(problem.StatusCode)
|
|
// The status is already committed; a failed write cannot be reported.
|
|
_, _ = w.Write(body)
|
|
}
|