ez

package module
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 5 Imported by: 48

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 (
	ECONFLICT          = "conflict"           // action cannot be performed
	EINTERNAL          = "internal"           // internal error
	EINVALID           = "invalid"            // validation failed
	ENOTFOUND          = "not_found"          // entity does not exist
	ENOTAUTHORIZED     = "not_authorized"     // requester does not have permissions to perform action
	ENOTAUTHENTICATED  = "not_authenticated"  // requester is not authenticated
	ERESOURCEEXHAUSTED = "resource_exhausted" // the resource has been exhausted
	ENOTIMPLEMENTED    = "not_implemented"    // the operation has not been implemented
	EUNAVAILABLE       = "unavailable"        // the system or operation is not available
)

Application error codes

Variables

This section is empty.

Functions

func ErrorCode

func ErrorCode(err error) string

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

func ErrorMessage

func ErrorMessage(err error) string

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

func ErrorStacktrace added in v1.1.2

func ErrorStacktrace(err error)

ErrorStacktrace prints a human-redable stacktrace of all nested errors.

func ErrorToGRPCCode added in v1.0.1

func ErrorToGRPCCode(err error) codes.Code

ErrorToGRPCCode converts an standar application error code to a GRPC error code

func ErrorToHTTPStatus added in v1.1.0

func ErrorToHTTPStatus(err error) int

ErrorToHTTPStatus converts an standar application error code to a HTTP status

func GRPCCodeToError added in v1.1.0

func GRPCCodeToError(c codes.Code) string

GRPCCodeToError converts a GRPC error code to a standar application error code

func HTTPStatusToError added in v1.1.0

func HTTPStatusToError(status int) string

HTTPStatusToError converts a HTTP error code to a standar application error code

Types

type Error

type Error struct {
	// Machine readable code
	Code string `json:"code"`
	// Human readable message
	Message string `json:"message"`
	// Logical operation
	Op string `json:"op"`
	// Nested error
	Err error `json:"err"`
}

Error defines a standar application error

func New

func New(op, code, message string, err error) *Error

New creates and returns a new error

func NewFromGRPC added in v1.0.1

func NewFromGRPC(op string, err error) *Error

NewFromGRPC wraps a GRPC Error into a standar application error

func Wrap

func Wrap(op 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) String added in v1.1.2

func (e *Error) String() string

String returns a simplified string representation of the error message

Jump to

Keyboard shortcuts

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