errors

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package errors defines structured errors which can be used for nesting errors with propagation of error identifiers and their messages. It also supports JSON serialization, so service to service communication can preserve error Kind.

Index

Constants

View Source
const Alphabet string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

Variables

This section is empty.

Functions

func Is

func Is(kind Kind, err error) bool

Is reports whether err is an *Error of the given Kind.

func JSON

func JSON(w http.ResponseWriter, err error, statusCode ...int)

func New

func New(args ...interface{}) error

New builds an error value from its arguments. There must be at least one argument or New panics. The type of each argument determines its meaning. If more than one argument of a given type is presented, only the last one is recorded.

The supported types are:

errors.Kind:
    The kind of the error.
*errors.Error
    The underlying error that triggered this one. If the error has
    non-empty ID and Kind fields, they are promoted as values of the
    returned one.
error:
    The underlying error that triggered this one.
string:
    Treated as an error message and assigned to the Message field.

func NewID

func NewID() string

Types

type Error

type Error struct {
	// ID is a unique error identifier.
	ID string

	// Kind of error returned to the caller.
	Kind Kind

	// Message is a description of the error.
	Message string

	// The underlying error that triggered this one, if any.
	Err error
}

func (*Error) Error

func (e *Error) Error() string

Error returns description of the error.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() (data []byte, err error)

MarshalJSON returns the JSON representation of an Error.

func (*Error) StatusCode

func (e *Error) StatusCode() int

StatusCode returns the HTTP status code corresponding to the error.

func (*Error) Temporary

func (e *Error) Temporary() bool

Temporary reports if an Error is temporary and whether the request can be retried.

func (*Error) UnmarshalJSON

func (e *Error) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON encoded Error.

type Kind

type Kind int
const (
	Unknown      Kind = iota // Unknown error.
	BadRequest               // BadRequest specifies invalid arguments or operation.
	Unauthorized             // Unauthorized request.
	Forbidden                // Forbidden operation.
	Exist                    // Exist already.
	NotFound                 // NotFound specifies that a resource does not exist.
	Timeout                  // Timeout of request.
	Internal                 // Internal error or inconsistency.
	ServiceUnavailable
)

func GetKind

func GetKind(statusCode int) Kind

GetKind returns error kind determined by the specified HTTP status code.

func (Kind) String

func (k Kind) String() string

Jump to

Keyboard shortcuts

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