Version: v0.0.0-...-3131eb7 Latest Latest

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

Go to latest
Published: Feb 13, 2019 License: Apache-2.0 Imports: 6 Imported by: 0




This section is empty.


View Source
var BadRequest = P{
	Type:   "bad_request",
	Title:  "Bad Request",
	Status: http.StatusBadRequest,
	Detail: "The request you sent was invalid in some way",

BadRequest is a well-known problem type. Use it as a shortcut in your actions.

View Source
var NotFound = P{
	Type:   "not_found",
	Title:  "Resource Missing",
	Status: http.StatusNotFound,
	Detail: "The resource at the url requested was not found.  This is usually " +
		"occurs for one of two reasons:  The url requested is not valid, or no " +
		"data in our database could be found with the parameters provided.",

NotFound is a well-known problem type. Use it as a shortcut in your actions

View Source
var ServerError = P{
	Type:   "server_error",
	Title:  "Internal Server Error",
	Status: http.StatusInternalServerError,
	Detail: "An error occurred while processing this request.  This is usually due " +
		"to a bug within the server software.  Trying this request again may " +
		"succeed if the bug is transient, otherwise please report this issue " +
		"to the issue tracker at:" +
		" Please include this response in your issue.",

ServerError is a well-known problem type. Use it as a shortcut.


func Inflate

func Inflate(p *P)

Inflate sets some basic parameters on the problem, mostly the type for now

func RegisterError

func RegisterError(err error, p P)

RegisterError records an error -> P mapping, allowing the app to register specific errors that may occur in other packages to be rendered as a specific P instance.

For example, you might want to render any sql.ErrNoRows errors as a problem.NotFound, and you would do so by calling:

problem.RegisterError(sql.ErrNoRows, problem.NotFound) in you application initialization sequence

func Render

func Render(ctx context.Context, w http.ResponseWriter, p interface{})

Render writes a http response to `w`, compliant with the "Problem Details for HTTP APIs" RFC:

`p` is the problem, which may be either a concrete P struct, an implementor of the `HasProblem` interface, or an error. Any other value for `p` will panic.


type HasProblem

type HasProblem interface {
	Problem() P

HasProblem types can be transformed into a problem. Implement it for custom errors.

type P

type P struct {
	Type     string                 `json:"type"`
	Title    string                 `json:"title"`
	Status   int                    `json:"status"`
	Detail   string                 `json:"detail,omitempty"`
	Instance string                 `json:"instance,omitempty"`
	Extras   map[string]interface{} `json:"extras,omitempty"`

P is a struct that represents an error response to be rendered to a connected client.

func MakeInvalidFieldProblem

func MakeInvalidFieldProblem(name string, reason error) *P

MakeInvalidFieldProblem is a helper function to make a BadRequest with extras

func (P) Error

func (p P) Error() string

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL