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: 9 modules, 60 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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