http

package
v5.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPStatusForCode

func HTTPStatusForCode(code ErrorCode) int

HTTPStatusForCode returns the HTTP status code that corresponds to an ErrorCode. Unmapped codes (including ErrNothingSpecific and any server-side failure such as ErrTalkingToDatabase, ErrTalkingToSearchProvider, ErrSecretGeneration, or ErrEncryptionIssue) resolve to http.StatusInternalServerError.

func RegisterHTTPErrorMapper

func RegisterHTTPErrorMapper(m HTTPErrorMapper)

RegisterHTTPErrorMapper registers a domain-specific error mapper. Domains call this from init() to contribute their error mappings.

Types

type APIError

type APIError struct {
	Message string    `json:"message"`
	Code    ErrorCode `json:"code"`
	// contains filtered or unexported fields
}

APIError represents a response we might send to the User in the event of an error.

func (*APIError) AsError

func (e *APIError) AsError() error

AsError returns the error message.

func (*APIError) Error

func (e *APIError) Error() string

Error returns the error message.

type APIResponse

type APIResponse[T any] struct {
	Data       T                     `json:"data,omitempty"`
	Pagination *filtering.Pagination `json:"pagination,omitempty"`
	Error      *APIError             `json:"error,omitempty"`
	Details    ResponseDetails       `json:"details"`
	// contains filtered or unexported fields
}

APIResponse represents a response we might send to the user.

func NewAPIErrorResponse

func NewAPIErrorResponse(issue string, code ErrorCode, details ResponseDetails) *APIResponse[any]

NewAPIErrorResponse returns a new APIResponse with an error field.

func ToAPIResponse

func ToAPIResponse(err error) (int, *APIResponse[any])

ToAPIResponse maps a handler error to the HTTP status and response envelope that should be sent to the client. It combines ToAPIError (error -> code + safe message) with HTTPStatusForCode (code -> status), so callers get everything needed to write a consistent error response in one call. A nil error resolves to 200 with an empty envelope, though callers typically only invoke this on a non-nil error.

type ErrorCode

type ErrorCode string

ErrorCode is a string code identifying specific error conditions in API responses.

const (
	// ErrNothingSpecific is a catch-all error code for when we just need one.
	ErrNothingSpecific ErrorCode = "E100"
	// ErrFetchingSessionContextData is returned when we fail to fetch session context data.
	ErrFetchingSessionContextData ErrorCode = "E101"
	// ErrDecodingRequestInput is returned when we fail to decode request input.
	ErrDecodingRequestInput ErrorCode = "E102"
	// ErrValidatingRequestInput is returned when the user provides invalid input.
	ErrValidatingRequestInput ErrorCode = "E103"
	// ErrDataNotFound is returned when we fail to find data in the database.
	ErrDataNotFound ErrorCode = "E104"
	// ErrTalkingToDatabase is returned when we fail to interact with a database.
	ErrTalkingToDatabase ErrorCode = "E105"
	// ErrMisbehavingDependency is returned when we fail to interact with a third party.
	ErrMisbehavingDependency ErrorCode = "E106"
	// ErrTalkingToSearchProvider is returned when we fail to interact with the search provider.
	ErrTalkingToSearchProvider ErrorCode = "E107"
	// ErrSecretGeneration is returned when we fail to generate a secret.
	ErrSecretGeneration ErrorCode = "E108"
	// ErrUserIsBanned is returned when a user is banned.
	ErrUserIsBanned ErrorCode = "E109"
	// ErrUserIsNotAuthorized is returned when a user is not authorized.
	ErrUserIsNotAuthorized ErrorCode = "E110"
	// ErrEncryptionIssue is returned when encryption fails in the service.
	ErrEncryptionIssue ErrorCode = "E111"
	// ErrCircuitBroken is returned when a service is circuit broken.
	ErrCircuitBroken ErrorCode = "E112"
)

func ToAPIError

func ToAPIError(err error) (code ErrorCode, msg string)

ToAPIError maps known sentinel errors to ErrorCode and a safe user-facing message. It tries PlatformMapper first, then each registered domain mapper. Returns (code, message). Unknown errors fall back to the neutral ErrNothingSpecific and "an error occurred" — never a domain-specific code (e.g. a payment panic must not report "database").

type HTTPErrorMapper

type HTTPErrorMapper interface {
	Map(err error) (code ErrorCode, msg string, ok bool)
}

HTTPErrorMapper maps domain errors to (ErrorCode, message). ok=false means no match.

var PlatformMapper HTTPErrorMapper = platformMapper{}

PlatformMapper maps platform-level errors to HTTP error codes and messages. It does not depend on any domain.

type ResponseDetails

type ResponseDetails struct {
	CurrentAccountID string `json:"currentAccountID"`
	TraceID          string `json:"traceID"`
	// contains filtered or unexported fields
}

ResponseDetails represents details about the response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL