errors

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 5 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package errors is the canonical error model for gox services: an error carries a Code (mirroring gRPC status codes), a public-safe message, and optional details, and maps to an HTTP status and a JSON envelope in one documented way.

Services do not have to replace their own sentinel errors: Map applies caller-supplied Mapper functions at the boundary so existing domain errors can be translated to codes without rewriting them.

Index

Constants

View Source
const StatusClientClosedRequest = 499

StatusClientClosedRequest is the non-standard status nginx uses when the client went away; it is what a canceled context maps to.

Variables

This section is empty.

Functions

func HTTPStatus

func HTTPStatus(err error) int

HTTPStatus returns the HTTP status err renders as.

func Map

func Map(err error, mappers ...Mapper) error

Map applies mappers in order and returns as soon as one yields an *Error. nil and errors that already are *Error pass through untouched.

Types

type Code

type Code int

Code classifies an error. The numbering mirrors gRPC status codes.

const (
	CodeOK                 Code = 0
	CodeCanceled           Code = 1
	CodeUnknown            Code = 2
	CodeInvalidArgument    Code = 3
	CodeDeadlineExceeded   Code = 4
	CodeNotFound           Code = 5
	CodeAlreadyExists      Code = 6
	CodePermissionDenied   Code = 7
	CodeResourceExhausted  Code = 8
	CodeFailedPrecondition Code = 9
	CodeAborted            Code = 10
	CodeOutOfRange         Code = 11
	CodeUnimplemented      Code = 12
	CodeInternal           Code = 13
	CodeUnavailable        Code = 14
	CodeDataLoss           Code = 15
	CodeUnauthenticated    Code = 16
)

Codes. Zero is OK so that CodeOf(nil) reads naturally.

func CodeOf

func CodeOf(err error) Code

CodeOf returns the code of err, looking through wrapping. nil is OK, context errors get their own codes, and anything else is Unknown.

func ParseCode

func ParseCode(name string) (code Code, ok bool)

ParseCode returns the Code for a name from an envelope ("not_found"). ok is false for names it does not know.

func (Code) HTTPStatus

func (c Code) HTTPStatus() int

HTTPStatus returns the HTTP status the code renders as.

func (Code) String

func (c Code) String() string

String returns the snake_case name used in the JSON envelope.

type Envelope

type Envelope struct {
	Code      string         `json:"code"`
	Message   string         `json:"message"`
	RequestID string         `json:"request_id,omitempty"`
	Details   map[string]any `json:"details,omitempty"`
}

Envelope is the JSON body every gox error response carries.

func ToEnvelope

func ToEnvelope(err error, requestID string) (int, Envelope)

ToEnvelope renders err as an HTTP status and a public-safe envelope. Errors that are not *Error never leak their text: they become a generic message for their code.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is a coded error with a public-safe message and an optional cause.

func Aborted

func Aborted(format string, args ...any) *Error

Aborted creates an error with CodeAborted.

func AlreadyExists

func AlreadyExists(format string, args ...any) *Error

AlreadyExists creates an error with CodeAlreadyExists.

func Canceled

func Canceled(format string, args ...any) *Error

Canceled creates an error with CodeCanceled.

func DataLoss

func DataLoss(format string, args ...any) *Error

DataLoss creates an error with CodeDataLoss.

func DeadlineExceeded

func DeadlineExceeded(format string, args ...any) *Error

DeadlineExceeded creates an error with CodeDeadlineExceeded.

func FailedPrecondition

func FailedPrecondition(format string, args ...any) *Error

FailedPrecondition creates an error with CodeFailedPrecondition.

func Internal

func Internal(format string, args ...any) *Error

Internal creates an error with CodeInternal.

func InvalidArgument

func InvalidArgument(format string, args ...any) *Error

InvalidArgument creates an error with CodeInvalidArgument.

func New

func New(code Code, msg string) *Error

New creates an Error with a code and a public-safe message.

func Newf

func Newf(code Code, format string, args ...any) *Error

Newf creates an Error with a code and a formatted public-safe message.

func NotFound

func NotFound(format string, args ...any) *Error

NotFound creates an error with CodeNotFound.

func OutOfRange

func OutOfRange(format string, args ...any) *Error

OutOfRange creates an error with CodeOutOfRange.

func PermissionDenied

func PermissionDenied(format string, args ...any) *Error

PermissionDenied creates an error with CodePermissionDenied.

func ResourceExhausted

func ResourceExhausted(format string, args ...any) *Error

ResourceExhausted creates an error with CodeResourceExhausted.

func Unauthenticated

func Unauthenticated(format string, args ...any) *Error

Unauthenticated creates an error with CodeUnauthenticated.

func Unavailable

func Unavailable(format string, args ...any) *Error

Unavailable creates an error with CodeUnavailable.

func Unimplemented

func Unimplemented(format string, args ...any) *Error

Unimplemented creates an error with CodeUnimplemented.

func Unknown

func Unknown(format string, args ...any) *Error

Unknown creates an error with CodeUnknown.

func Wrap

func Wrap(cause error, code Code, msg string) *Error

Wrap creates an Error that keeps cause for errors.Is/As and logs, while msg is what clients see.

func (*Error) Code

func (e *Error) Code() Code

Code returns the error's code.

func (*Error) Details

func (e *Error) Details() map[string]any

Details returns a copy of the details map (nil when there are none).

func (*Error) Error

func (e *Error) Error() string

Error renders the message and, when present, the cause. This form is for logs; use Message for anything client-facing.

func (*Error) Message

func (e *Error) Message() string

Message returns the public-safe message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the cause, if any.

func (*Error) WithDetail

func (e *Error) WithDetail(key string, value any) *Error

WithDetail returns a copy of the error with one more detail attached. Details are public: they end up in the JSON envelope.

func (*Error) WithHTTPStatus

func (e *Error) WithHTTPStatus(status int) *Error

WithHTTPStatus returns a copy that renders with a specific HTTP status instead of the code's default, for the few HTTP-only cases such as 413. The code and envelope are unchanged.

type Mapper

type Mapper func(error) error

Mapper translates a service's own errors into *Error at the boundary. A mapper returns its input unchanged when it does not recognize it.

Jump to

Keyboard shortcuts

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