problem

package
v0.0.0-...-fb9541f Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ServerError is a well-known problem type. Use it as a shortcut.
	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: https://github.com/paydex-core/paydex-go/issues." +
			" Please include this response in your issue.",
	}

	// NotFound is a well-known problem type.  Use it as a shortcut in your actions
	NotFound = P{
		Type:   "not_found",
		Title:  "Resource Missing",
		Status: http.StatusNotFound,
		Detail: "The resource at the url requested was not found.  This 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.",
	}

	// BadRequest is a well-known problem type.  Use it as a shortcut
	// in your actions.
	BadRequest = P{
		Type:   "bad_request",
		Title:  "Bad Request",
		Status: http.StatusBadRequest,
		Detail: "The request you sent was invalid in some way.",
	}
)
View Source
var (
	ServiceHost = "https://paydex.org/horizon-errors/"
)

Functions

func IsKnownError

func IsKnownError(err error) error

IsKnownError maps an error to a list of known errors

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 RegisterHost

func RegisterHost(host string)

RegisterHost registers the service host url. It is used to prepend the host url to the error type. If you don't wish to prepend anything to the error type, register host as an empty string. The default service host points to `https://paydex.org/horizon-errors/`.

func RegisterReportFunc

func RegisterReportFunc(fn ReportFunc)

RegisterReportFunc registers the report function that you want to use to report errors. Once reportFn is initialzied, it will be used to report unexpected errors.

func Render

func Render(ctx context.Context, w http.ResponseWriter, err error)

Render writes a http response to `w`, compliant with the "Problem Details for HTTP APIs" RFC: https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-00

func UnRegisterErrors

func UnRegisterErrors()

UnRegisterErrors removes all registered errors

Types

type P

type P struct {
	Type   string                 `json:"type"`
	Title  string                 `json:"title"`
	Status int                    `json:"status"`
	Detail string                 `json:"detail,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

type ReportFunc

type ReportFunc func(context.Context, error)

ReportFunc is a function type used to report unexpected errors.

Jump to

Keyboard shortcuts

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