Documentation ¶
Overview ¶
it is an example to show the best practices for errors
Index ¶
- Variables
- func AssertTrue(b bool)
- func AssertTruef(b bool, format string, args ...interface{})
- func Check(err error)
- func Check2(_ interface{}, err error)
- func IsNil(val interface{}) bool
- func Wrap(err error) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Error
- func (err *Error) Error() string
- func (err Error) From(e error) *Error
- func (err Error) GoString() string
- func (err *Error) Status() int
- func (err Error) String() string
- func (err Error) WithCode(code int) *Error
- func (err Error) WithMsg(msgs ...string) *Error
- func (err Error) WithMsgf(format string, args ...interface{}) *Error
- func (err Error) WithStack(skip ...int) *Error
- type HTTPError
- type WaitGroupWrapper
Constants ¶
This section is empty.
Variables ¶
var ( Err = &Error{Code: http.StatusInternalServerError, Err: "Error"} // https://golang.org/pkg/net/http/#pkg-constants ErrBadRequest = Err.WithCode(http.StatusBadRequest) ErrPaymentRequired = Err.WithCode(http.StatusPaymentRequired) ErrForbidden = Err.WithCode(http.StatusForbidden) ErrNotFound = Err.WithCode(http.StatusNotFound) ErrMethodNotAllowed = Err.WithCode(http.StatusMethodNotAllowed) ErrNotAcceptable = Err.WithCode(http.StatusNotAcceptable) ErrProxyAuthRequired = Err.WithCode(http.StatusProxyAuthRequired) ErrRequestTimeout = Err.WithCode(http.StatusRequestTimeout) ErrConflict = Err.WithCode(http.StatusConflict) ErrGone = Err.WithCode(http.StatusGone) ErrLengthRequired = Err.WithCode(http.StatusLengthRequired) ErrPreconditionFailed = Err.WithCode(http.StatusPreconditionFailed) ErrRequestEntityTooLarge = Err.WithCode(http.StatusRequestEntityTooLarge) ErrRequestURITooLong = Err.WithCode(http.StatusRequestURITooLong) ErrUnsupportedMediaType = Err.WithCode(http.StatusUnsupportedMediaType) ErrRequestedRangeNotSatisfiable = Err.WithCode(http.StatusRequestedRangeNotSatisfiable) ErrExpectationFailed = Err.WithCode(http.StatusExpectationFailed) ErrTeapot = Err.WithCode(http.StatusTeapot) ErrUnprocessableEntity = Err.WithCode(http.StatusUnprocessableEntity) ErrLocked = Err.WithCode(http.StatusLocked) ErrFailedDependency = Err.WithCode(http.StatusFailedDependency) ErrUpgradeRequired = Err.WithCode(http.StatusUpgradeRequired) ErrPreconditionRequired = Err.WithCode(http.StatusPreconditionRequired) ErrTooManyRequests = Err.WithCode(http.StatusTooManyRequests) ErrRequestHeaderFieldsTooLarge = Err.WithCode(http.StatusRequestHeaderFieldsTooLarge) ErrInternalServerError = Err.WithCode(http.StatusInternalServerError) ErrNotImplemented = Err.WithCode(http.StatusNotImplemented) ErrBadGateway = Err.WithCode(http.StatusBadGateway) ErrGatewayTimeout = Err.WithCode(http.StatusGatewayTimeout) ErrHTTPVersionNotSupported = Err.WithCode(http.StatusHTTPVersionNotSupported) ErrVariantAlsoNegotiates = Err.WithCode(http.StatusVariantAlsoNegotiates) ErrInsufficientStorage = Err.WithCode(http.StatusInsufficientStorage) ErrLoopDetected = Err.WithCode(http.StatusLoopDetected) ErrNotExtended = Err.WithCode(http.StatusNotExtended) ErrNetworkAuthenticationRequired = Err.WithCode(http.StatusNetworkAuthenticationRequired) )
Predefined errors
Functions ¶
func AssertTrue ¶
func AssertTrue(b bool)
AssertTrue asserts that b is true. Otherwise, it would log fatal.
func AssertTruef ¶
AssertTruef is AssertTrue with extra info.
func Check2 ¶
func Check2(_ interface{}, err error)
Check2 acts as convenience wrapper around Check, using the 2nd argument as error.
Types ¶
type Error ¶
type Error struct { Code int `json:"-"` Err string `json:"error"` Msg string `json:"message"` Data interface{} `json:"data,omitempty"` Stack string `json:"-"` }
Error represents a numeric error with optional meta. It can be used in middleware as a return result.
func ErrorWithStack ¶
ErrorWithStack create a error with stacktrace
func (Error) From ¶
From returns a copy of err with given error. It will try to merge the given error. If the given error is a *Error instance, it will be returned without copy.
err := gear.ErrBadRequest.From(errors.New("invalid email")) err := gear.Err.From(someErr)
func (Error) WithCode ¶
WithCode returns a copy of err with given code.
BadRequestErr := gear.Err.WithCode(400)
func (Error) WithMsg ¶
WithMsg returns a copy of err with given new messages.
err := gear.Err.WithMsg() // just clone err := gear.ErrBadRequest.WithMsg("invalid email") // 400 Bad Request error with message invalid email"
type HTTPError ¶
type HTTPError interface { // Error returns error's message. Error() string // Status returns error's http status code. Status() int }
func ParseError ¶
ParseError parse a error, textproto.Error or HTTPError to HTTPError
type WaitGroupWrapper ¶
func (*WaitGroupWrapper) Wrap ¶
func (w *WaitGroupWrapper) Wrap(cb func())