ez

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: MIT Imports: 3 Imported by: 3

README

ez

Package ez provides an opinionated, simple way to manage errors in Golang. Based on this awesome post.

Features

  • Extremely simple to use.

  • Allows nesting errors, so you can create a stacktrace.

  • Can receive your own Application error codes.

Usage

Import the library:

import "github.com/vanclief/ez"

Creating an error:

const op = "TestNew"
err := ez.New(op, ECONFLICT, "An error message", nil)

Creating nested errors (used when the err is not an ez.Error):

const op = "TestNew"
err1 := errors.New("emit macho dwarf: elf header corrupted")
err2 := ez.New(op, ECONFLICT, "Elf header should not be corrupted", err1)

Creating wrapped errors:

const op = "OriginalOp"
err1 := ez.New(op, EINTERNAL, "Nested error", nil)

const newOp = "NewOp"
err2 := ez.Wrap(newOp, err1)

Return the string interpretation of an error:

const op = "TestError"
err := ez.New(op, EINTERNAL, "An internal error", nil)

err.Error() >> "TestError: <internal> An internal error"

Return the code of the root error:

const op = "TestErrorCode"
err := ez.New(op, EINVALID, "An invalid error", nil)

ez.ErrorCode(err) >> "invalid"

Return the human readable code:

const op = "TestErrorMessage"
err := ez.New(op, ENOTFOUND, "A not found error", nil)

ez.ErrorMessage(err) >> "A not found error"

Documentation

Index

Constants

View Source
const (
	ErrorCodeOk                 = 0  //Not an error; returned on success.
	ErrorCodeCancelled          = 1  //The operation was cancelled, typically by the caller.
	ErrorCodeUnknown            = 2  //Unknown error. For example, this error may be returned when a Status value received from another address space belongs to an error space that is not known in this address space. Also errors raised by APIs that do not return enough error information may be converted to this error.
	ErrorCodeInvalidArgument    = 3  // validation failed
	ErrorCodeDeadlineExceeded   = 4  // deadline exceeded
	ErrorCodeNotFound           = 5  // entity does not exist
	ErrorCodeConflict           = 6  // action cannot be performed
	ErrorCodeNotAuthorized      = 7  // requester does not have permissions to perform action
	ErrorCodeResourceExhausted  = 8  // the resource has been exhausted
	ErrorCodeFailedPrecondition = 9  // operation was rejected because the system is not in a state required for the operation's execution
	ErrorCodeAborted            = 10 // operation was aborted
	ErrorCodeOutOfRange         = 11 // operation was attempted past the valid range
	ErrorCodeUnimplemented      = 12 // the operation has not been implemented
	ErrorCodeInternal           = 13 // internal error
	ErrorCodeUnavailable        = 14 // the system or operation is not available
	ErrorCodeDataLoss           = 15 // unrecoverable data loss or corruption
	ErrorCodeUnauthenticated    = 16 // requester is not authenticated
)

Application error codes

Variables

This section is empty.

Functions

func ErrorMessageFromError

func ErrorMessageFromError(err error) string

ErrorMessage returns the human-readable message of the error, if available. Otherwise returns a generic error message.

func OperationFromError added in v0.2.0

func OperationFromError(err error) string

Types

type Error

type Error struct {
	Code      ErrorCode `json:"code"`
	Message   string    `json:"message"`
	Operation string    `json:"operation"`
	Err       error     `json:"err"`
}

func New

func New(code ErrorCode, message string, opts ...ErrorOption) *Error

New creates and returns a new error

func Wrap

func Wrap(err error) *Error

Wrap returns a new error that contains the passed error but with a different operation, useful for creating stacktraces

func WrapWithOperation added in v0.2.0

func WrapWithOperation(operation string, err error) *Error

Wrap returns a new error that contains the passed error but with a different operation, useful for creating stacktraces

func (*Error) Error

func (e *Error) Error() string

Error returns the string representation of the error message.

func (*Error) GetCode

func (e *Error) GetCode() ErrorCode

func (*Error) GetHttpStatusCode added in v0.2.0

func (e *Error) GetHttpStatusCode() int

func (*Error) String

func (e *Error) String() string

String returns a simplified string representation of the error message

type ErrorCode

type ErrorCode int

func ErrorCodeFromError

func ErrorCodeFromError(err error) ErrorCode

ErrorCode returns the code of the root error, if available. Otherwise returns ErrorCodeInternal.

func (ErrorCode) String

func (code ErrorCode) String() string

type ErrorInterface

type ErrorInterface interface {
	GetCode() ErrorCode
	Error() string
}

type ErrorOption added in v0.2.0

type ErrorOption func(*ErrorOptions)

func WithError added in v0.2.0

func WithError(err error) ErrorOption

func WithOperation added in v0.2.0

func WithOperation(operation string) ErrorOption

type ErrorOptions added in v0.2.0

type ErrorOptions struct {
	Operation string
	Error     error
}

Jump to

Keyboard shortcuts

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