ez

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 2 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
	ErrorCodeInvalid           = 3  // validation failed
	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
	ErrorCodeUnimplemented     = 12 // the operation has not been implemented
	ErrorCodeInternal          = 13 // internal error
	ErrorCodeUnavailable       = 14 // the system or operation is not available
	ErrorCodeNOTAUTHENTICATED  = 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.

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, operation string, err error) *Error

New creates and returns a new error

func Wrap

func Wrap(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 (ptr *Error) GetCode() ErrorCode

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 HttpException

type HttpException interface {
	GetCode() int
}

type HttpExceptionBuilder

type HttpExceptionBuilder struct {
	Code    int
	Message string
}

func (*HttpExceptionBuilder) Error

func (ptr *HttpExceptionBuilder) Error() string

func (*HttpExceptionBuilder) GetCode

func (ptr *HttpExceptionBuilder) GetCode() int

type HttpExceptionBuilderWithError

type HttpExceptionBuilderWithError struct {
	HttpExceptionBuilder
	Err error
}

func (*HttpExceptionBuilderWithError) Error

func (*HttpExceptionBuilderWithError) GetCode

func (ptr *HttpExceptionBuilderWithError) GetCode() int

Jump to

Keyboard shortcuts

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