errorx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2023 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMessage = "An internal error has occurred. Please contact technical support."

DefaultMessage is the string used as default response for GetMessage.

View Source
var Separator = ":\n\t"

Separator is the string used to separate nested errors. By default, to make errors easier on the eye, nested errors are indented on a new line. A server may instead choose to keep each error on a single line by modifying the separator string, perhaps to ":: " or "-> ".

Functions

func E

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

E builds an error value from its arguments. There must be at least one argument or E 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 types are:

 errorx.Op
	    The operation being performed, usually the method
		being invoked (Get, Put, etc.).
	string
		Treated as an error message and assigned to the Message.
	errorx.Code
		The class of error, such as permission failure.
	error
		The underlying error that triggered this one.

If the error is printed, only those items that have been set to non-zero values will appear in the result.

If Code is not specified or Unknown, we set it to the Code of the underlying error.

If Message is not filled, we set it to the Message of the underlying error.

func Errorf

func Errorf(format string, args ...interface{}) error

Errorf is equivalent to fmt.Errorf, but allows clients to import only this package for all error handling.

func GetArrJSON

func GetArrJSON(input error) []byte

GetArrJSON returns the json byte of the converted array of errors.

Example: [{"code":"internal","message":"Internal server error.","op":"userService.FindUserByID"}]

func GetMessage

func GetMessage(err error) string

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

func Is

func Is(code Code, err error) bool

Is reports whether err is an *Error of the given Code. If err is nil then Is returns false.

func Match

func Match(err1, err2 error) bool

Match compares its two error arguments. It can be used to check for expected errors in tests. Both arguments must have underlying type *Error or Match will return false. Otherwise it returns true if every non-zero element of the first error is equal to the corresponding element of the second. If the Err field is a *Error, Match recurs on that field; otherwise it compares the strings returned by the Error methods. Elements that are in the second argument but not present in the first are ignored.

Example:

Match(errors.E(errorx.Permission, "message"), err)
tests whether err is an Error with Code=Permission and Message=message.

func Str

func Str(text string) error

Str returns an error that formats as the given text. It is intended to be used as the error-typed argument to the E function.

Types

type Code

type Code string

Code defines the kind of error this is, mostly for use by systems that must act differently depending on the error.

const (
	Unknown    Code = ""           // Unclassified or unknown error.
	Permission Code = "permission" // Permission denied.
	Internal   Code = "internal"   // Internal error or inconsistency.
	Conflict   Code = "conflict"   // Action cannot be performed.
	Invalid    Code = "invalid"    // Validation failed.
	NotFound   Code = "not_found"  // Entity does not exist.
	Gateway    Code = "gateway"    // Gateway or third party service return error.

)

Application error codes.

func GetCode

func GetCode(err error) Code

GetCode returns the code of the root error, if available. Otherwise returns Internal.

type Error

type Error struct {
	// Machine-readable error code.
	Code Code `json:"code,omitempty"`

	// Human-readable message.
	Message string `json:"message,omitempty"`

	// Logical operation and nested error.
	Op  Op    `json:"op,omitempty"`
	Err error `json:"-"`
}

Error defines a standard application error.

func GetArr

func GetArr(input error) []Error

GetArr returns the error as the converted array of errors.

Example: [

{
  "code": "internal",
  "message": "Internal server error.",
  "op": "userService.FindUserByID"
},
{
  "code": "gateway",
  "message": "Gateway server error.",
  "op": "accountGateway.FindUserByID"
},
{
  "message": "Unknown error.",
  "op": "io.Write"
}

]

func (*Error) Error

func (e *Error) Error() string

Error returns the string representation of the error message.

Example:

userService.FindUserByID: <internal> Internal server error.:
    accountGateway.FindUserByID: <gateway> Gateway server error.:
    io.Write: Unknown error.

type Op

type Op string

Op describes an operation, usually as the package and method, such as "userService.FindUserByID".

func GetOps

func GetOps(err error) []Op

GetOps returns the "stack" of operations for each generated error.

Jump to

Keyboard shortcuts

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