errors

package
v0.0.0-...-98b9f36 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package errors provides a nice way of handling http errors

Examples: To create an error:

err := errors.New(http.StatusBadRequest, "Something went wrong")

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRouteNotFound happens when no route was matched
	ErrRouteNotFound = New(http.StatusNotFound, "no API found with those values")
	// ErrInvalidID represents an invalid identifier
	ErrInvalidID = New(http.StatusBadRequest, "please provide a valid ID")
)

Functions

func Handler

func Handler(w http.ResponseWriter, r *http.Request, err interface{})

Handler marshals an error to JSON, automatically escaping HTML and setting the Content-Type as application/json.

func NotFound

func NotFound(w http.ResponseWriter, r *http.Request)

NotFound handler is called when no route is matched

func RecoveryHandler

func RecoveryHandler(w http.ResponseWriter, r *http.Request, err interface{})

RecoveryHandler handler is used when a panic happens

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

Types

type Error

type Error struct {
	Code    int    `json:"-"`
	Message string `json:"error"`
}

Error is a custom error that implements the `error` interface. When creating errors you should provide a code (could be and http status code) and a message, this way we can handle the errors in a centralized place.

func New

func New(code int, message string) *Error

New creates a new instance of Error

func (*Error) Error

func (e *Error) Error() string

Jump to

Keyboard shortcuts

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