Documentation
¶
Index ¶
- Variables
- func E(args ...interface{}) error
- func HTTPErrorHandler(w http.ResponseWriter, lgr zerolog.Logger, err error)
- func HTTPStatusCodeFromError(err error) int
- func KindIs(kind Kind, err error) bool
- func Match(err1, err2 error) bool
- type Code
- type Error
- type HTTPErrResponse
- type Kind
- type Parameter
- type Realm
- type ServiceError
- type UserName
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
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 ¶
HTTPStatusCodeFromError translate error to an http status code
func KindIs ¶
KindIs reports whether err is an *Error of the given Kind. If err is nil then KindIs 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.
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 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.
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 // It is used when an authenticated user trying to access the resource // but not permitted to do so Unauthorized )
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 ValidationErrors ¶
type ValidationErrors []error
func (*ValidationErrors) Append ¶
func (v *ValidationErrors) Append(args ...interface{})
func (ValidationErrors) Error ¶
func (v ValidationErrors) Error() string