errorx

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 4 Imported by: 0

README

errorx

errorx contains gogox's error package with additional features

How to Use

errorx.Error implements the error interface. There are some notably additional features:

Code

errorx.Error.Code is intended to be used as unique identifier of each gogox's error. Below is the example of intended use:

err := service.DoSomething()

if err != nil {
  // Parse error to gogox error. It will return gogox error if successful, otherwise return not ok.
  gogoxErr, ok := errorx.ParseError(err)
  if !ok {
    // as it is not errorx's error, assume that it is the underlying error from dependencies that is not expected.
    // hence we can safely assume it is internal error.
    gogoxErr := errorx.New(errorx.CodeInternal, err.Error())
  }

  switch(gogoxErr.Code) {
  case "your.error.code.not_found":
    w.WriteHeader(404)
  case "your.other.error.code.unauthorized":
    w.WriteHeader(401)
  case "user.invalid.name":
    w.WriteHeader(422)
  default:
    w.WriteHeader(500)
  }
}
LogError

LogError is intended to be use for logging. You can store more verbose error message (like including the user_id or email) for easier debugging process in your observability platform.

If not provided, logMessage is automatically built from Code and Message.

StackTrace

For each New and Wrap (and their respective variants like Newf and Wrapf) call, it will store the stack caller. Use PrintStackTrace to print the stack trace.

Details

You can add more Details for your gogox Error. Details are commonly used to store field information. Example: Invalid parameter of name or email field.

Documentation

Index

Constants

View Source
const (
	// CodeInternal defines common internal error code
	CodeInternal = "common.internal"
	// CodeNotFound defines common not found error code
	CodeNotFound = "common.not_found"
	// CodeUnauthorized defines common unauthorized error code
	CodeUnauthorized = "common.unauthorized"
	// CodeInvalidParameter defines common invalid parameter error code
	CodeInvalidParameter = "common.invalid_parameter"
	// CodeTimeout defines common timeout error code
	CodeTimeout = "common.timeout"
	// CodeAlreadyExists defines common entity already exists error code
	CodeAlreadyExists = "common.already_exist"
	// CodeForbidden defines common forbidden access error code
	CodeForbidden = "common.forbidden"
	// CodeUnimplemented defines common forbidden access error code
	CodeUnimplemented = "common.unimplemented"
)

Variables

Functions

This section is empty.

Types

type Details

type Details struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

Details defines error details of gogox's error

type Error

type Error struct {
	Code    string     `json:"code"`
	Message string     `json:"message"`
	Details []*Details `json:"details"`
	// contains filtered or unexported fields
}

Error defines standard error of gogox

Code is intended to be a unique identifier of each gogox error. You should use Code to determine your error handling. Message is intended to be a user-friendly error message. Details is intended to store additional information about the error, commonly used for 4xx or user induced error (such as invalid parameter). logMessage is intended to store log error message, typically contains more sensitive information that is needed by end user. DO NOT use logMessage for API response

func ErrInternal added in v0.2.0

func ErrInternal(msg string) *Error

ErrInternal returns generic gogox error with CodeInternal

func ErrInvalidParameter added in v0.2.0

func ErrInvalidParameter(msg string) *Error

ErrInvalidParameter returns generic gogox error with CodeInvalidParameter

func ErrNotFound added in v0.2.0

func ErrNotFound(msg string) *Error

ErrNotFound returns generic gogox error with CodeNotFound

func ErrUnauthorized added in v0.2.0

func ErrUnauthorized(msg string) *Error

ErrUnauthorized returns generic gogox error with CodeUnauthorized

func New

func New(code, message string) *Error

New creates new gogox's error logMessage is automatically built from code and message stack is populated in each initialization

func NewWithLog

func NewWithLog(code, message, logMessage string) *Error

NewWithLog creates new gogox's error with overriden logMessage

func Newf

func Newf(code, message string, args ...interface{}) *Error

Newf creates new gogox's error but with message formatting.

func NewfWithLog

func NewfWithLog(code, message, logMessage string, args ...interface{}) *Error

NewfWithLog creates new gogox's error with overriden logMessage and message formatting

func Parse added in v0.2.0

func Parse(err error) (*Error, bool)

Parse provides sugar syntax for parsing err to Error instance

func ParseAndWrap added in v0.2.0

func ParseAndWrap(err error, msg string) *Error

ParseAndWrap provides sugar syntax for parsing err to Error instance. if the parsing fail, automatically wrap the error into internal error with msg. if the parsing successful, return the original error.

func Wrap

func Wrap(cause error, code, message string) *Error

Wrap create new gogox's error with cause

func WrapWithLog

func WrapWithLog(cause error, code, message, logMessage string) *Error

WrapWithLog create new gogox's error with cause and log message

func Wrapf

func Wrapf(cause error, code, message string, args ...interface{}) *Error

Wrapf create new gogox's error with cause and message formatting

func WrapfWithLog

func WrapfWithLog(cause error, code, message, logMessage string, args ...interface{}) *Error

WrapfWithLog create new gogox's error with cause, logMessage and message formatting

func (*Error) AddDetails

func (e *Error) AddDetails(details ...*Details)

AddDetails add error details

func (*Error) Error

func (e *Error) Error() string

Error implements error interface

func (*Error) LogError

func (e *Error) LogError() string

LogError returns message for debugging, concating cause(s)'s logMessage if any.

func (*Error) PrintStackTrace

func (e *Error) PrintStackTrace() string

PrintStackTrace prints stack trace from error stack.

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

Jump to

Keyboard shortcuts

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