jsonhttp

package
v0.0.0-...-9d78121 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: LGPL-3.0 Imports: 3 Imported by: 4

Documentation

Overview

Copyright 2015 Canonical Ltd. Licensed under the LGPLv3, see LICENCE file for details. The jsonhttp package provides general functions for returning JSON responses to HTTP requests. It is agnostic about the specific form of any returned errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleErrors

func HandleErrors(errToResp ErrorToResponse) func(handle ErrorHandler) http.Handler

HandleErrors returns a function that can be used to convert an ErrorHandler into an http.Handler. The given errToResp parameter is used to convert any non-nil error returned by handle to the response in the HTTP body.

func HandleJSON

func HandleJSON(errToResp ErrorToResponse) func(handle JSONHandler) http.Handler

HandleJSON returns a function that can be used to convert an JSONHandler into an http.Handler. The given errToResp parameter is used to convert any non-nil error returned by handle to the response in the HTTP body If it returns a nil value, the original error is returned as a JSON string.

func WriteError

func WriteError(errToResp ErrorToResponse) func(w http.ResponseWriter, err error)

WriteError returns a function that can be used to write an error to a ResponseWriter and set the HTTP status code. The errToResp parameter is used to determine the actual error value and status to write.

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, val interface{}) error

WriteJSON writes the given value to the ResponseWriter and sets the HTTP status to the given code.

Types

type ErrorHandler

type ErrorHandler func(http.ResponseWriter, *http.Request) error

ErrorHandler is like http.Handler except it returns an error which may be returned as the error body of the response. An ErrorHandler function should not itself write to the ResponseWriter if it returns an error.

type ErrorToResponse

type ErrorToResponse func(err error) (httpStatus int, errorBody interface{})

ErrorToResponse represents a function that can convert a Go error into a form that can be returned as a JSON body from an HTTP request. The httpStatus value reports the desired HTTP status.

type JSONHandler

type JSONHandler func(http.Header, *http.Request) (interface{}, error)

JSONHandler is like http.Handler except that it returns a body (to be converted to JSON) and an error. The Header parameter can be used to set custom header on the response.

Jump to

Keyboard shortcuts

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