-
Notifications
You must be signed in to change notification settings - Fork 0
/
problemdetails.go
65 lines (59 loc) · 2.35 KB
/
problemdetails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package problemdetails
import (
"encoding/xml"
"net/http"
"strings"
)
const defaultProblemType = "about:blank"
// ProblemDetails is the struct definition of a problem details object as defined by
// the RFC7807.
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. It is empty by default and
// it will not be marshalled if empty.
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. It is empty by default and
// it will not be marshalled if empty.
Instance string `json:"instance,omitempty" xml:"instance,omitempty"`
}
// New creates a new ProblemDetails
func New(problemType string, title string, statusCode int, detail string, instance string) *ProblemDetails {
if strings.TrimSpace(problemType) == "" {
return &ProblemDetails{
Type: defaultProblemType,
Title: http.StatusText(statusCode),
Status: statusCode,
Detail: detail,
Instance: instance,
}
}
return &ProblemDetails{
Type: problemType,
Title: title,
Status: statusCode,
Detail: detail,
Instance: instance,
}
}
// FromHTTPStatus creates a new ProblemDetails based on the HTTP status code provided
func FromHTTPStatus(statusCode int) *ProblemDetails {
return New("", "", statusCode, "", "")
}