Documentation ¶
Index ¶
- Variables
- func AreEqual(err1, err2 error) bool
- func Assert(t *testing.T, expected interface{}, actual error, msgAndArgs ...interface{}) bool
- func AssertNil(t *testing.T, actual error, msgAndArgs ...interface{}) bool
- func DefaultStdOutLogger(msg string, args ...interface{})
- func InstanceOf(err error, template Template) bool
- func ToRequest(r RequestAborter, err error) bool
- type APIError
- type Error
- type ErrorType
- type RequestAborter
- type Template
- func (t Template) API(httpCode, errCode int) Template
- func (t Template) Args(args ...interface{}) Template
- func (t Template) ErrCode(code int) Template
- func (t Template) GetType() ErrorType
- func (t Template) HTTPCode(code int) Template
- func (t Template) Make() Error
- func (t Template) MakeTraced(depth int) Error
- func (t Template) Msg(msg string, args ...interface{}) Template
- func (t Template) NoTrace() Template
- func (t Template) Safe() Template
- func (t Template) Tag(tag string) Template
- func (t Template) TagInt(tag string, value int) Template
- func (t Template) TagStr(tag, value string) Template
- func (t Template) Trace() Template
- func (t Template) Track() Template
- func (t Template) Untrack() Template
- type TypedError
Constants ¶
This section is empty.
Variables ¶
var ( // PrintUnsafeErrors controls wether unsafe (technical) error messages should be visible to the user in response messages. PrintUnsafeErrors = false // Logger is called to print errors and stack traces to log. Logger = DefaultStdOutLogger )
var ( // GenericError represents a generic error with stack trace. GenericError = New("An error occured").Trace() // ConfigurationError is an error that is caused by an invalid configuration. ConfigurationError = New("The specified configuration is not valid") // ArgumentError denotes a missing or invalid argument. ArgumentError = New("An invalid argument has been supplied") )
var ( // GenericSafeErrorMessage denotes the message replacement when exposing unsafe errors via API. GenericSafeErrorMessage string )
Functions ¶
func AreEqual ¶
AreEqual returns true if the type of both errors is the same regardless of the specific error message. Also returns true if both errors are nil.
func Assert ¶
Assert performs test assertions to ensure error equality. The expected error can be of type error or errors.Template.
func DefaultStdOutLogger ¶
func DefaultStdOutLogger(msg string, args ...interface{})
DefaultStdOutLogger prints all error messages to StdOut.
func InstanceOf ¶
InstanceOf returns true if the given error is an instance of the given template. A nil error always returns false.
func ToRequest ¶
func ToRequest(r RequestAborter, err error) bool
ToRequest writes the given error to a HTTP request and returns true if err was not nil.
Types ¶
type APIError ¶
type APIError struct { ResponseCode int `json:"-"` ErrorCode int `json:"code"` Message string `json:"message"` }
APIError represents a generic error repsonse object with code and message.
func DefaultAPI ¶
DefaultAPI returns a new APIError object using the default http and error codes.
func (APIError) ToRequest ¶
func (err APIError) ToRequest(r RequestAborter)
ToRequest writes this APIError object to a HTTP request and aborts pipeline execution.
type Error ¶
type Error interface { error TypedError fmt.Stringer SafeString() string GetID() string GetStackTrace() string // Untrack disables id and stack trace printing for this error. Untrack() Error // NoTrace disables stack trace printing. NoTrace() Error // Msg returns a new Error object and replaces the error message. You can supply all formatting args later using Args() to skip formatting in this call. Msg(msg string, args ...interface{}) Error // Args returns a new Error object with filled placeholders. A safe message remains safe. Args(args ...interface{}) Error // Cause adds the given error as cause. It's error message will be appended to the output. Cause(err error) Error // StrCause adds a detailed error message as cause. StrCause(str string, args ...interface{}) Error // Expand creates a copy of this error with given message and sets the current error as cause. Expand(msg string, args ...interface{}) Error // ExpandSafe creates a copy of this error with given message and sets the current error as cause. The expanded message is marked as safe. ExpandSafe(msg string, args ...interface{}) Error // Tag adds a named tag to the error. Tag(tag string) Error // IsTagged returns whether the error contains a named tag. Tags with attached value are not captured by this method. IsTagged(tag string) bool // TagStr adds a named tag with string value to the error. TagStr(tag, value string) Error // GetTagStr returns a string tag or false, if no tag is set. GetTagStr(tag string) (string, bool) // TagInt adds a named tag with integer value to the error. TagInt(tag string, value int) Error // GetTagInt returns an integer tag or false, if no tag is set. GetTagInt(tag string) (int, bool) // Equals returns true when the error types are equal (ignoring the explicit error message). Equals(other error) bool // Is returns trhe when the error is an instance of the given template. Is(template Template) bool // HTTPCode sets the http response code. HTTPCode(code int) Error // ErrCode sets the api error code. ErrCode(code int) Error // Safe marks the error as safe for printing to end-user. Safe() Error // API returns the corresponding APIError object. API() APIError // ToRequest writes the APIError message representation to a HTTP request and aborts pipeline execution. ToRequest(r RequestAborter) // ToRequestAndLog calls ToRequest(r) and ToLog(...except). ToRequestAndLog(r RequestAborter, except ...TypedError) // ToRequestAndLog calls ToRequest(r) and ForceLog(...except). ToRequestAndForceLog(r RequestAborter, except ...TypedError) // ToLog writes the error message with debug data to the log. ToLog(except ...TypedError) // ForceLog writes the error message (and also untracked ones) with debug data to the log. ForceLog(except ...TypedError) }
Error is used as base error type in the whole application. Use Wrap(error) to encapsulate errors from third-party code.
type ErrorType ¶
type ErrorType string
ErrorType represents the base type of an error regardless of the specific error message.
type RequestAborter ¶
type RequestAborter interface {
AbortWithStatusJSON(int, interface{})
}
RequestAborter defines the required functionality to abort an HTTP request and is compatible with *gin.Context.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template represents an error template that can be instatiated to an error using Make().
func (Template) API ¶
API untracks the error, marks it as safe and update the error and response codes.
func (Template) Make ¶
Make instatiates an error using this template. A call to this method generates a new ID and StackTrace from the calling location if tracked and traced.
func (Template) MakeTraced ¶
MakeTraced instatiates an error using this template. A call to this method tracks and traces the error and generates a new ID and StackTrace from the calling location. Use the depth parameter to skip a certain number of stack frames in the trace.
func (Template) Msg ¶
Msg replaces the error message. You can supply all formatting args later using Args() to skip formatting in this call.
func (Template) TagInt ¶ added in v1.1.0
TagInt adds a named tag with integer value to the template.
type TypedError ¶
type TypedError interface { // GetType returns the type of the error that is used for comparison. GetType() ErrorType }
TypedError represents errors and templates that define an error type.