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
- func HTTPStatus(err error) int
- func Map(err error, mappers ...Mapper) error
- type Code
- type Envelope
- type Error
- func Aborted(format string, args ...any) *Error
- func AlreadyExists(format string, args ...any) *Error
- func Canceled(format string, args ...any) *Error
- func DataLoss(format string, args ...any) *Error
- func DeadlineExceeded(format string, args ...any) *Error
- func FailedPrecondition(format string, args ...any) *Error
- func Internal(format string, args ...any) *Error
- func InvalidArgument(format string, args ...any) *Error
- func New(code Code, msg string) *Error
- func Newf(code Code, format string, args ...any) *Error
- func NotFound(format string, args ...any) *Error
- func OutOfRange(format string, args ...any) *Error
- func PermissionDenied(format string, args ...any) *Error
- func ResourceExhausted(format string, args ...any) *Error
- func Unauthenticated(format string, args ...any) *Error
- func Unavailable(format string, args ...any) *Error
- func Unimplemented(format string, args ...any) *Error
- func Unknown(format string, args ...any) *Error
- func Wrap(cause error, code Code, msg string) *Error
- type Mapper
Constants ¶
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 ¶
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 CodeDataLoss Code = 15 CodeUnauthenticated Code = 16 )
Codes. Zero is OK so that CodeOf(nil) reads naturally.
func CodeOf ¶
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 ¶
ParseCode returns the Code for a name from an envelope ("not_found"). ok is false for names it does not know.
func (Code) HTTPStatus ¶
HTTPStatus returns the HTTP status the code renders as.
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.
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 AlreadyExists ¶
AlreadyExists creates an error with CodeAlreadyExists.
func DeadlineExceeded ¶
DeadlineExceeded creates an error with CodeDeadlineExceeded.
func FailedPrecondition ¶
FailedPrecondition creates an error with CodeFailedPrecondition.
func InvalidArgument ¶
InvalidArgument creates an error with CodeInvalidArgument.
func OutOfRange ¶
OutOfRange creates an error with CodeOutOfRange.
func PermissionDenied ¶
PermissionDenied creates an error with CodePermissionDenied.
func ResourceExhausted ¶
ResourceExhausted creates an error with CodeResourceExhausted.
func Unauthenticated ¶
Unauthenticated creates an error with CodeUnauthenticated.
func Unavailable ¶
Unavailable creates an error with CodeUnavailable.
func Unimplemented ¶
Unimplemented creates an error with CodeUnimplemented.
func Wrap ¶
Wrap creates an Error that keeps cause for errors.Is/As and logs, while msg is what clients see.
func (*Error) Error ¶
Error renders the message and, when present, the cause. This form is for logs; use Message for anything client-facing.
func (*Error) WithDetail ¶
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 ¶
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.