errors

package
v0.0.0-...-ce8de81 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 5 Imported by: 60

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

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