apierror

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

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(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(format string, a ...any) *InternalServerError

type NotFound

type NotFound struct {
	APIError
}

func NewNotFound

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

func NewNotFoundJob

func NewNotFoundJob(jobID int64) *NotFound

func NewNotFoundQueue

func NewNotFoundQueue(name string) *NotFound

func NewNotFoundWorkflow

func NewNotFoundWorkflow(id string) *NotFound

type ServiceUnavailable

type ServiceUnavailable struct {
	APIError
}

func NewServiceUnavailable

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

Jump to

Keyboard shortcuts

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