Help us shape the future of Go! Take the 2026 Go Developer Survey

errs

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package errs defines the project's typed error envelope.

Every service handler returning an error eventually maps it onto an HTTP status, a gRPC code, or a CLI exit. Doing that mapping once, here, keeps boundary translation consistent and prevents every service from inventing its own private "ErrNotFound" type.

Usage:

if user == nil {
    return errs.New(errs.CodeNotFound, "user not found")
}

At the HTTP boundary:

switch errs.CodeOf(err) {
case errs.CodeNotFound:    c.Status(http.StatusNotFound)
case errs.CodeInvalidInput: c.Status(http.StatusBadRequest)
...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code string

Code is a stable identifier for a class of failures. Choose codes that describe the kind of error ("not_found"), not the specific cause — the message field carries the cause-specific detail.

const (
	CodeNotFound     Code = "not_found"
	CodeInvalidInput Code = "invalid_input"
	CodeUnauthorized Code = "unauthorized"
	CodeForbidden    Code = "forbidden"
	CodeConflict     Code = "conflict"
	CodeInternal     Code = "internal"
	CodeUnavailable  Code = "unavailable"
)

func CodeOf

func CodeOf(err error) Code

CodeOf extracts the Code of err. If err is not an *Error, returns CodeInternal — callers at HTTP/gRPC boundaries can then map it to a generic 500 / Unknown without special-casing.

type Error

type Error struct {
	Code    Code
	Message string
	// contains filtered or unexported fields
}

Error is the project's canonical typed error. It implements the standard error interface and wraps an underlying cause so errors.Is and errors.As still work end-to-end.

func New

func New(code Code, message string) *Error

New returns an Error with no underlying cause.

func Wrap

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

Wrap returns an Error that wraps cause. Use this when translating an underlying error (database, RPC, parsing) into the project's code set.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap exposes the underlying cause for errors.Is / errors.As.

Jump to

Keyboard shortcuts

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