errors

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package errors defines errors that can be transported safely across service boundaries without exposing their internal causes.

Index

Constants

View Source
const (
	CodeOK              = 0
	CodeInvalidArgument = 10001
	CodeInternal        = 90001
	CodeTimeout         = 90002
	// CodeMax is the largest business code representable by JGO's standard
	// protobuf int32 response field.
	CodeMax = 1<<31 - 1
)
View Source
const (
	MessageInternal = "internal server error"
	MessageTimeout  = "request timeout"
)

Variables

This section is empty.

Functions

func PublicValues

func PublicValues(err error) (code int, message string, httpStatus int)

PublicValues extracts client-safe values. Unknown errors always become a generic internal error, so their text cannot leak into API responses.

Types

type Catalog added in v0.4.0

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

Catalog validates and resolves a finite set of business error codes.

func MergeCatalogs added in v0.4.0

func MergeCatalogs(catalogs ...*Catalog) (*Catalog, error)

MergeCatalogs builds one process catalog from service-local and shared catalogs. Duplicate codes or names across module/repository boundaries are rejected by the same rules as NewCatalog.

func MustCatalog added in v0.4.0

func MustCatalog(definitions ...Definition) *Catalog

func MustMergeCatalogs added in v0.4.0

func MustMergeCatalogs(catalogs ...*Catalog) *Catalog

func NewCatalog added in v0.4.0

func NewCatalog(definitions ...Definition) (*Catalog, error)

func (*Catalog) Definitions added in v0.4.0

func (c *Catalog) Definitions() []Definition

Definitions returns a stable copy suitable for governance tooling and composing catalogs supplied by shared Go modules.

func (*Catalog) FromCode added in v0.4.0

func (c *Catalog) FromCode(code int, message string) *Error

FromCode converts a downstream RPC business code into an HTTP-aware Error. Unknown codes preserve their public code/message but deliberately map to 500.

func (*Catalog) HTTPStatus added in v0.4.0

func (c *Catalog) HTTPStatus(code int) int

HTTPStatus returns 500 for unknown codes so unmapped downstream business failures never masquerade as a successful HTTP response.

func (*Catalog) LookupCode added in v0.4.0

func (c *Catalog) LookupCode(code int) (Definition, bool)

func (*Catalog) LookupName added in v0.4.0

func (c *Catalog) LookupName(name string) (Definition, bool)

type Definition added in v0.4.0

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

Definition is one stable, transport-safe business error declaration.

func Define added in v0.4.0

func Define(code int, name, message string, httpStatus int) Definition

func (Definition) Code added in v0.4.0

func (d Definition) Code() int

func (Definition) HTTPStatus added in v0.4.0

func (d Definition) HTTPStatus() int

func (Definition) Message added in v0.4.0

func (d Definition) Message() string

func (Definition) Name added in v0.4.0

func (d Definition) Name() string

func (Definition) New added in v0.4.0

func (d Definition) New() *Error

func (Definition) Wrap added in v0.4.0

func (d Definition) Wrap(cause error) *Error

type Error

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

Error is a public service error with an optional private cause.

func New

func New(code int, message string, opts ...Option) *Error

New creates a service error. Invalid public fields are replaced by safe internal-error defaults.

func Wrap

func Wrap(cause error, code int, message string, opts ...Option) *Error

Wrap creates a service error while preserving an internal cause for logs and errors.Is/errors.As. The cause is never returned by MessageOf.

func (*Error) Code

func (e *Error) Code() int

Code returns the public business error code.

func (*Error) Error

func (e *Error) Error() string

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus returns the HTTP status associated with the error.

func (*Error) Message

func (e *Error) Message() string

Message returns the public, client-safe message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Option

type Option func(*Error)

Option configures a service Error.

func WithHTTPStatus

func WithHTTPStatus(status int) Option

WithHTTPStatus associates an HTTP status with a service error.

Jump to

Keyboard shortcuts

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