problemdetails

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2020 License: MIT Imports: 4 Imported by: 0

README

Golang Problem Details

Build Status MIT license GoDoc Go Report Card CodeFactor Coverage Status

Problem details implementation (https://tools.ietf.org/html/rfc7807) package for go.

go get github.com/mvmaasakkers/go-problemdetails

How to use

The ProblemDetails struct can be used as error because it implements the error interface. The ProblemType interface can be used to create predefined ProblemDetails with extensions and also implements the error interface.

The struct is setup to be used by the json and xml marshaler from the stdlib and will marshal into application/problem+json or application/problem+xml compliant data as defined in the RFC 7807.

To generate a ProblemDetails based on just a HTTP Status Code you can create one using NewHTTP(statusCode int):

problemDetails := problemdetails.NewHTTP(http.StatusNotFound)

This will generate a ProblemDetails struct that marshals as follows:

{
  "type": "about:blank",
  "title": "Not Found",
  "status": 404
}
<problem xmlns="urn:ietf:rfc:7807">
    <type>about:blank</type>
    <title>Not Found</title>
    <status>404</status>
</problem>

or use the more verbose New(statusCode int, problemType, title, detail, instance string):

problemDetails := problemdetails.New(http.StatusNotFound, "https://example.net/problem/object_not_found", "Object not found", "Object with id 1234 was not found, another id should be given.", "https://api.example.net/objects/1234")

This will generate a ProblemDetails struct that marshals as follows:

{
  "type": "https://example.net/problem/object_not_found",
  "title": "Object not found",
  "status": 404,
  "detail": "Object with id 1234 was not found, another id should be given.",
  "instance": "https://api.example.net/objects/1234"
}
<problem xmlns="urn:ietf:rfc:7807">
    <type>https://example.net/problem/object_not_found</type>
    <title>Object not found</title>
    <status>404</status>
    <detail>Object with id 1234 was not found, another id should be given.</detail>
    <instance>https://api.example.net/objects/1234</instance>
</problem>

Http helpers

For ease of use there are two output handlers available. ProblemDetails.ServeJSON for JSON and ProblemDetails.ServeXML for XML.

A shorthand for generating a 404 statuscode in Problem Details JSON to the ResponseWriter you can:

problemdetails.NewHTTP(http.StatusNotFound).ServeJSON(w, r)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ProblemDetails

type ProblemDetails struct {
	XMLName xml.Name `json:"-" xml:"urn:ietf:rfc:7807 problem"`
	// Type is a URI reference [RFC3986] that identifies the
	// problem type. This specification encourages that, when
	// dereferenced, it provide human-readable documentation for the
	// problem type (e.g., using HTML [W3C.REC-html5-20141028]).  When
	// this member is not present, its value is assumed to be
	// "about:blank".
	Type string `json:"type" xml:"type"`
	// Title is a short, human-readable summary of the problem
	// type.  It SHOULD NOT change from occurrence to occurrence of the
	// problem, except for purposes of localization (e.g., using
	// proactive content negotiation; see [RFC7231], Section 3.4).
	Title string `json:"title" xml:"title"`
	// Status is the HTTP status code ([RFC7231], Section 6)
	// generated by the origin server for this occurrence of the problem.
	Status int `json:"status,omitempty" xml:"status,omitempty"`
	// Detail is a human-readable explanation specific to this
	// occurrence of the problem.
	// If present, it ought to focus on helping the client
	// correct the problem, rather than giving debugging information.
	Detail string `json:"detail,omitempty" xml:"detail,omitempty"`
	// Instance is a URI reference that identifies the specific
	// occurrence of the problem.  It may or may not yield further
	// information if dereferenced.
	Instance string `json:"instance,omitempty" xml:"instance,omitempty"`
}

ProblemDetails is the struct definition of a problem details object

func New

func New(statusCode int, problemType, title, detail, instance string) *ProblemDetails

New creates a new ProblemDetails error

func NewHTTP

func NewHTTP(statusCode int) *ProblemDetails

NewHTTP creates a new ProblemDetails error based just the HTTP Status Code

func (*ProblemDetails) Error

func (pd *ProblemDetails) Error() string

Error implements the error interface

func (*ProblemDetails) ServeJSON

func (p *ProblemDetails) ServeJSON(w http.ResponseWriter, r *http.Request) error

ServeJSON will output Problem Details json to the response writer

func (*ProblemDetails) ServeXML

func (p *ProblemDetails) ServeXML(w http.ResponseWriter, r *http.Request) error

ServeXML will output Problem Details xml to the response writer

type ProblemType

type ProblemType interface {
	Error() string
}

ProblemType is an interface for problem type definitions. A ProblemType also implements the error interface

Jump to

Keyboard shortcuts

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