Files
lincodex/backend/internal/adapter/primary/http/v1/handler.go
T

52 lines
2.1 KiB
Go

package v1
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.
//
// 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 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
// through v1.NewHandler that asserts a non-501 response, because renaming an
// operationId would otherwise silently fall back to the embedded 501 method.
type Handler struct {
api.UnimplementedHandler
health *HealthHandler
problems *problem.Responder
}
var _ api.Handler = (*Handler)(nil)
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)
}