httperror

package
v0.9.0-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2017 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package httperror provides common error handling, particularly around golang errors and responses

Index

Constants

View Source
const (
	// DefaultStatus is the response status code used when the error's code
	// is less than http.StatusBadRequest (400), i.e. when the code is not
	// an HTTP error code.
	DefaultStatus = http.StatusInternalServerError
)

Variables

This section is empty.

Functions

func Write

func Write(response http.ResponseWriter, err error) (int, error)

Write handles writing the given error to the response, taking care of the response status and any output headers. This function can be used with errors other than HTTP errors. It will provide default behavior in that case.

If a status other than DefaultStatus is desired, use WriteMessage(response, err.Error(), DesiredStatus, nil) instead of this function.

func WriteMessage

func WriteMessage(response http.ResponseWriter, message string, status int, header http.Header) (int, error)

WriteMessage handles writing full error message information out to a response. This function avoids the overhead of creating a full blown HTTP error object.

If status is not an HTTP error code, DefaultStatus is used. The header is optional, and can be nil.

Types

type Interface

type Interface interface {
	error
	String() string
	Status() int
	Header() http.Header
}

Interface represents an HTTP-specific error with additional metadata for the response.

func New

func New(message string, status int, header http.Header) Interface

New returns an error containing the given internal metadata. This constructor is appropriate for infrastructure that needs to return HTTP metadata about an error from code not directly part of an HTTP handler.

For code that has access to the http.ResponseWriter, use WriteMessage instead

Jump to

Keyboard shortcuts

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