Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultMessage = "An internal error has occurred. Please contact technical support."
DefaultMessage is the string used as default response for GetMessage.
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 ¶
Errorf is equivalent to fmt.Errorf, but allows clients to import only this package for all error handling.
func GetArrJSON ¶
GetArrJSON returns the json byte of the converted array of errors.
Example: [{"code":"internal","message":"Internal server error.","op":"userService.FindUserByID"}]
func GetMessage ¶
GetMessage returns the human-readable message of the error, if available. Otherwise returns a generic error message.
func Is ¶
Is reports whether err is an *Error of the given Code. If err is nil then Is returns false.
func Match ¶
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.
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.
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 ¶
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" }
]