errors

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Is     = errors.Is
	As     = errors.As
	New    = errors.New
	Errorf = fmt.Errorf
)
View Source
var (
	NotFound = Error{
		Code:    "not_found",
		Status:  http.StatusNotFound,
		Message: "Resource not found",
	}

	Conflict = Error{
		Code:    "conflict",
		Status:  http.StatusConflict,
		Message: "A conflicting state exists",
	}

	Forbidden = Error{
		Code:    "forbidden",
		Status:  http.StatusForbidden,
		Message: "You are not authorized",
	}
	Throttled = Error{
		Code:    "throttled",
		Status:  http.StatusTooManyRequests,
		Message: "You are doing way too much",
	}

	MissingAuth = Error{
		Code:    "missing_auth",
		Status:  http.StatusUnauthorized,
		Message: "You are not authenticated",
	}

	Unsupported = Error{
		Code:    "unsupported",
		Status:  http.StatusUnprocessableEntity,
		Message: "Requested action is not supported",
	}

	InvalidInput = Error{
		Code:    "invalid_input",
		Status:  http.StatusBadRequest,
		Message: "Your request is not valid",
	}

	InternalIssue = Error{
		Code:    "internal_issue",
		Status:  http.StatusInternalServerError,
		Message: "Oops, something went wrong",
	}
)

Functions

func OneOf

func OneOf(err error, others []error) bool

OneOf checks (in-order) if err is one of the given errors.

Types

type Error

type Error struct {
	Code      string         `json:"code"`
	Cause     error          `json:"cause,omitempty"`
	Status    int            `json:"status"`
	Attribs   map[string]any `json:"attribs,omitempty"`
	Message   string         `json:"message"`
	DebugHint string         `json:"debug_hint,omitempty"`
}

Error represents an error value with all the relevant context.

func E

func E(err error) Error

E converts any given error to the Error type. Unknown are converted to ErrInternal.

func (Error) CausedBy

func (err Error) CausedBy(e error) Error

CausedBy returns a clone of the error with `e` set as the cause.

func (Error) Coded

func (err Error) Coded(code string, attribs ...map[string]any) Error

Coded returns a clone of the original error with the given code.

func (Error) Error

func (err Error) Error() string

Error represents technical description of the error.

func (Error) Hintf

func (err Error) Hintf(format string, args ...any) Error

Hintf returns a clone of the error with a debug hint.

func (Error) Is

func (err Error) Is(other error) bool

Is checks if 'other' is of type Error and has the same code. See https://blog.golang.org/go1.13-errors.

func (Error) Msgf added in v0.4.0

func (err Error) Msgf(format string, args ...any) Error

Msgf returns a clone of the error with a user-friendly message.

Jump to

Keyboard shortcuts

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