feat(api): define public problem errors, pagination, and retry contract

This commit is contained in:
shira committed 2026-10-05 22:09:36 +03:00
1 parent b2f6e87bfe
commit 565ca94bbb
86 files changed
+8200 -154

No files matched your search

@@ -4,6 +4,7 @@ import (
"context"
"lincodex/backend/internal/adapter/primary/http/api"
"lincodex/backend/internal/adapter/primary/http/problem"
)
// Handler is the composite implementation of the generated api.Handler.
@@ -11,17 +12,17 @@ import (
// The embedded api.UnimplementedHandler keeps the backend compiling after the
// contract gains an operation that has no business implementation yet. Such an
// operation returns ht.ErrNotImplemented without side effects. The generated
// server passes that error to its configured ErrorHandler, and the default
// ogenerrors.DefaultErrorHandler maps it to HTTP 501. When the contract defines
// a default error response, the generated server still sends
// ht.ErrNotImplemented to ErrorHandler before calling NewError. The embedded
// NewError, however, returns a zero status code that the generated encoder
// sends as HTTP 200, so the composite must then define NewError explicitly.
// That NewError must answer *ogenerrors.SecurityError with 401 or 403, which
// the generated server passes to it directly, and answer every other error
// with 500 and a sanitized message, never err.Error() of an internal error.
// Errors of the generated status-code error type (*<Schema>StatusCode) are
// encoded before NewError is called, so it does not need to handle them.
// server passes that error to the ErrorHandler configured in the router, which
// answers 501 with code not_implemented, before it would call NewError.
//
// The contract's common default response makes ogen add NewError to
// api.Handler. The embedded api.UnimplementedHandler.NewError returns a zero
// status code that the generated encoder sends as HTTP 200, so Handler defines
// NewError explicitly: *ogenerrors.SecurityError, which the generated server
// passes to it directly, becomes 401, and every other error becomes 500 with a
// fixed body and is logged here, never exposing err.Error(). Errors of the
// generated *api.ProblemStatusCode type are encoded before NewError is called,
// so operations return them for expected failures such as 403 or 404.
//
// Each implemented operation gets an explicit method here that delegates to
// the handler owning it. Each implemented operation also gets a router test
@@ -30,15 +31,21 @@ import (
type Handler struct {
api.UnimplementedHandler
health *HealthHandler
health *HealthHandler
problems *problem.Responder
}
var _ api.Handler = (*Handler)(nil)
func NewHandler(health *HealthHandler) *Handler {
return &Handler{health: health}
func NewHandler(health *HealthHandler, problems *problem.Responder) *Handler {
return &Handler{health: health, problems: problems}
}
func (handler *Handler) GetHealthz(ctx context.Context) (*api.HealthResponse, error) {
return handler.health.GetHealthz(ctx)
}
// NewError implements api.Handler for errors returned by operations.
func (handler *Handler) NewError(ctx context.Context, err error) *api.ProblemStatusCode {
return handler.problems.HandlerError(ctx, err)
}
@@ -3,9 +3,12 @@ package v1
import (
"context"
"errors"
"log/slog"
"net/http"
"reflect"
"testing"
"lincodex/backend/internal/adapter/primary/http/problem"
)
// Once the contract defines a default error response, ogen adds NewError to
@@ -13,7 +16,7 @@ import (
// status code, which the generated encoder sends as HTTP 200. A handler error
// would then look like a success, so the composite must define its own NewError.
func TestHandlerNewErrorDoesNotReportSuccess(t *testing.T) {
newError := reflect.ValueOf(NewHandler(nil)).MethodByName("NewError")
newError := reflect.ValueOf(NewHandler(nil, problem.NewResponder(slog.New(slog.DiscardHandler)))).MethodByName("NewError")
if !newError.IsValid() {
t.Skip("the contract has no default error response, so the generated server has no NewError")
}