httperrors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2019 License: MIT Imports: 3 Imported by: 6

README

httperrors Build Status Go Report Card GoDoc

Description

This package introduces a new error type that combines an HTTP status code and a message. It helps with two things:

  1. Other packages/modules that make HTTP requests like go-request can return this error type in case an HTTP error code was returned. The consumer of the package can then check for the type httperrors.HTTPError and react depending on the status code or pass on the error as is.
  2. When working with frameworks like Echo this error type can be returned in the HTTP handler and handled in the general error handler of the framework. For example you could log all 5xx errors but not invalid requests (400). Of course the error type provided by the framework could also be used for this. But the separate error type allows for a framework independent request client while still being able to just "pass on" HTTP errors if needed.

The httperrors.HTTPError type implements the error interface but also includes a WriteJSON(w http.ResponseWriter) method that allows to convert the error into an HTTP response. It will set the HTTP status code and write the message as JSON to the response body like this:

{
  "message": "missing field: username"
}

Example

Creating an HTTPError
func myHandler(c framework.Context) error {
	if c.FormValue("user") == "" {
		return httperrors.New(http.StatusBadRequest, "missing field: username")
	}
	
	// ...
}
Handling an HTTPError
func generalErrorHandler(err error, c framework.Context) {
	httpError, ok := err.(httperrors.HTTPError)
	// Alternativly a type switch can be used to identify the HTTPError.
	if ok {
	  httpError.WriteJSON(c.ResponseWriter)
	  return
	}

	// Handle other error types here ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPError

type HTTPError struct {
	StatusCode int         `json:"-"`
	Message    interface{} `json:"message"`
}

HTTPError represents an http error that occurred while handling a request

func New

func New(statusCode int, message interface{}) *HTTPError

New creates a new HTTPError instance message can be an error, the error message is then used as message the message is optional but multiple messages are not supported

func (*HTTPError) Error

func (he *HTTPError) Error() string

Error makes HTTPError compatible with the error interface.

func (*HTTPError) WriteJSON

func (he *HTTPError) WriteJSON(w http.ResponseWriter) error

WriteJSON allows to write the http error to a ResponseWriter for implemantation of the WritableError interface

type WritableError

type WritableError interface {
	error
	WriteJSON(w http.ResponseWriter) error
}

WritableError is an interface for errors that can be written to http responses as JSON

Jump to

Keyboard shortcuts

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