apierror

package
v0.0.0-...-be6ab89 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package apierror contains a variety of marshalable API errors that adhere to a unified error response convention.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithInternalError

func WithInternalError[TAPIError Interface](apiErr TAPIError, internalErr error) TAPIError

WithInternalError is a convenience function for assigning an internal error to the given API error and returning it.

Types

type APIError

type APIError struct {
	// InternalError is an additional error that might be associated with the
	// API error. It's not returned in the API error response, but is logged in
	// API endpoint execution to provide extra information for operators.
	InternalError error `json:"-"`

	// Message is a descriptive, human-friendly message indicating what went
	// wrong. Try to make error messages as actionable as possible to help the
	// caller easily fix what went wrong.
	Message string `json:"message"`

	// StatusCode is the API error's HTTP status code. It's not marshaled to
	// JSON, but determines how the error is written to a response.
	StatusCode int `json:"-"`
}

APIError is a struct that's embedded on a more specific API error struct (as seen below), and which provides a JSON serialization and a wait to conveniently write itself to an HTTP response.

APIErrorInterface should be used with errors.As instead of this struct.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) GetInternalError

func (e *APIError) GetInternalError() error

func (*APIError) SetInternalError

func (e *APIError) SetInternalError(internalErr error)

func (*APIError) Write

func (e *APIError) Write(ctx context.Context, logger *slog.Logger, w http.ResponseWriter)

Write writes the API error to an HTTP response, writing to the given logger in case of a problem.

type BadRequest

type BadRequest struct {
	APIError
}

func NewBadRequest

func NewBadRequest(message string) *BadRequest

func NewBadRequestf

func NewBadRequestf(format string, a ...any) *BadRequest

type Interface

type Interface interface {
	Error() string
	GetInternalError() error
	SetInternalError(internalErr error)
	Write(ctx context.Context, logger *slog.Logger, w http.ResponseWriter)
}

Interface is an interface to an API error. This is needed for use with errors.As because APIError itself is embedded on another error struct, and won't be usable as an errors.As target.

type InternalServerError

type InternalServerError struct {
	APIError
}

func NewInternalServerError

func NewInternalServerError(message string) *InternalServerError

func NewInternalServerErrorf

func NewInternalServerErrorf(format string, a ...any) *InternalServerError

type NotFound

type NotFound struct {
	APIError
}

func NewNotFound

func NewNotFound(message string) *NotFound

func NewNotFoundf

func NewNotFoundf(format string, a ...any) *NotFound

type RequestEntityTooLarge

type RequestEntityTooLarge struct {
	APIError
}

func NewRequestEntityTooLarge

func NewRequestEntityTooLarge(message string) *RequestEntityTooLarge

type ServiceUnavailable

type ServiceUnavailable struct {
	APIError
}

func NewServiceUnavailable

func NewServiceUnavailable(message string) *ServiceUnavailable

func NewServiceUnavailablef

func NewServiceUnavailablef(format string, a ...any) *ServiceUnavailable

type Unauthorized

type Unauthorized struct {
	APIError
}

func NewUnauthorized

func NewUnauthorized(format string, a ...any) *Unauthorized

Jump to

Keyboard shortcuts

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