errs

package
v0.0.0-...-f616f9a Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUndefined = errors.New("undefined error")

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:

errs.UserName
	The username of the user attempting the operation.
errs.Kind
	The class of error, such as permission failure.
errs.Code
	The code for a human-readable purpose about the error.
errs.Parameter
	The parameter represent the parameter related with the error.
string
	Treated as an error message and assigned to the
	Err field after a call to errors.New.
error
	The underlying error that triggered this one, if the error not contains stack,
	we will wrap it

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

If Kind is not specified or Other, we set it to the Kind of the underlying error.

func HTTPErrorHandler

func HTTPErrorHandler(w http.ResponseWriter, lgr zerolog.Logger, err error)

HTTPErrorHandler is a pre-defined http error handler, it will translate given error structured response it also support to log given error

func HTTPStatusCodeFromError

func HTTPStatusCodeFromError(err error) int

HTTPStatusCodeFromError translate error to an http status code

func KindIs

func KindIs(kind Kind, err error) bool

KindIs reports whether err is an *Error of the given Kind. If err is nil then KindIs 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.

For example,

	Match(errs.E(errors.Permission, errs.UserName("john@doe.com")), err)
 tests whether err is an Error with Kind=Permission and User=john@doe.com.

Types

type Code

type Code string

Code is a human-readable, short representation of the error

type Error

type Error struct {
	// User is the username of the user attempting the operation.
	User UserName

	// Kind is the class of error, such as permission failure,
	// or "Other" if its class is unknown or irrelevant.
	Kind Kind

	// Code is a human-readable, short representation of the error
	Code Code

	// Param represents the parameter related to the error.
	Param Parameter

	// Realm is a description of a protected area, used in the WWW-Authenticate header.
	Realm Realm

	// The underlying error that triggered this one, if any.
	Err error
}

Error is the type that implements the error interface. It contains a number of fields, each of different type. An Error value may leave some values unset.

func (*Error) As

func (w *Error) As(target interface{}) bool

As is method to satisfy errors.As interface

func (*Error) Cause

func (e *Error) Cause() error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

func (*Error) Is

func (e *Error) Is(target error) bool

Is is method to satisfy errors.Is interface

func (Error) Unwrap

func (e Error) Unwrap() error

type HTTPErrResponse

type HTTPErrResponse struct {
	Error  *ServiceError  `json:"error,omitempty"`
	Errors []ServiceError `json:"errors,omitempty"`
}

HTTPErrResponse is used as the Response Body

type Kind

type Kind uint8

Kind defines the kind of error this is

const (
	Other          Kind = iota // Unclassified error. This value is not printed in the error message.
	IO                         // External I/O error such as network failure
	Private                    // Information withheld
	Internal                   // Internal error or inconsistency
	Database                   // Database error
	Exist                      // Resource already exist
	NotExist                   // Resource does not exists
	Invalid                    // Invalid operation for this type of item
	Validation                 // Input validation error
	InvalidRequest             // Invalid request

	// Unauthenticated error will response with http.StatusUnauthorized (401) with empty body
	Unauthenticated

	// Unauthorized error will response with http.StatusForbidden (403) with empty body
	// It is used when an authenticated user trying to access the resource
	// but not permitted to do so
	Unauthorized
)

func (Kind) String

func (k Kind) String() string

type Parameter

type Parameter string

Parameter represents the parameter related to the error.

type Realm

type Realm string

Realm is a description of a protected area, used in the WWW-Authenticate header. Realm should be set when error Kind is Unauthenticated. If left unset, Realm will be set to the default set by the "restricted" method

var DefaultRealm Realm = "restricted"

type ServiceError

type ServiceError struct {
	Kind    string `json:"kind,omitempty"`
	Code    string `json:"code,omitempty"`
	Param   string `json:"param,omitempty"`
	Message string `json:"message,omitempty"`
}

ServiceError has fields for Service errors. All fields with no data will be omitted

type UserName

type UserName string

UserName is a string representing a user

type ValidationErrors

type ValidationErrors []error

func (*ValidationErrors) Append

func (v *ValidationErrors) Append(args ...interface{})

func (ValidationErrors) Error

func (v ValidationErrors) Error() string

Jump to

Keyboard shortcuts

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