assert

package module
v0.0.0-...-71ecc4c Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: MIT Imports: 4 Imported by: 0

README

assert

Simplified error handling for http routes using assert with status code that works well (but not exclusively) with rkusa/web.

Build Status GoDoc

Example

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")

License

MIT

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

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

func NewAssertError(statusCode int, message string, args ...interface{}) error

NewAssertError creates a new assertion error.

func OK

func OK(condition bool, statusCode int, message string, args ...interface{})

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.

func Success

func Success(err error, statusCode int, message string, args ...interface{})

Success throws with the given statusCode and message if the provided error exists. If message is an empty string, the default status description is used.

func Throw

func Throw(statusCode int, message string, args ...interface{})

Build and directly throw an error using the provided status code and message.

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.

func New

func New() Assert

Create a new assertion encapsulation.

type AssertError

type AssertError struct {
	Err error
	// contains filtered or unexported fields
}

func (AssertError) Error

func (err AssertError) Error() string

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Jump to

Keyboard shortcuts

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