Documentation ¶
Overview ¶
Package assert simplifies error handling for http routes using assert with status code. It exposes a middleware that works well (but not exclusively) with rkusa/web(https://github.com/rkusa/web).
Middleware usage
app := web.New() app.Use(assert.Middleware())
Asserting
assert.OK(username != "", 400, "No username given") assert.Error(err) assert.Success(err, 400, "something failed")
Index ¶
- func Error(err error)
- func Middleware(l Logger) func(http.ResponseWriter, *http.Request, http.HandlerFunc)
- func NewAssertError(statusCode int, message string, args ...interface{}) error
- func OK(condition bool, statusCode int, message string, args ...interface{})
- func Success(err error, statusCode int, message string, args ...interface{})
- func Throw(statusCode int, message string, args ...interface{})
- type Assert
- type AssertError
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(err error)
Error throws and responds with an 500 Internal Server Error if the provided error exists.
func Middleware ¶
func Middleware(l Logger) func(http.ResponseWriter, *http.Request, http.HandlerFunc)
This Middleware is required to properly handle the errors thrown using this assert package. It must be called before the asserts are used.
func NewAssertError ¶
NewAssertError creates a new assertion error.
func OK ¶
Success throws with the given statusCode and message if the provided condition evaluates to false. If message is an empty string, the default status description is used.
Types ¶
type Assert ¶
type Assert interface { OnError(func()) OK(bool, int, string, ...interface{}) Success(error, int, string, ...interface{}) Throw(int, string, ...interface{}) Error(error) }
Assert represents an encapsulation for the assertions to provide an OnError hook.
type AssertError ¶
type AssertError struct { Err error // contains filtered or unexported fields }
func (AssertError) Error ¶
func (err AssertError) Error() string