1
- # problems
2
- Problems is an RFC-7807 and RFC-9457 compliant library for describing HTTP
3
- errors. For more information see [ RFC-9457] ( https://tools.ietf.org/html/rfc9457 ) ,
4
- and it's predecessor [ RFC-7807] ( https://tools.ietf.org/html/rfc7807 ) .
1
+ # Problems
2
+
3
+ Problems is an RFC-7807 and RFC-9457 compliant library for describing HTTP errors.
4
+ For more information see [ RFC-9457 ] ( https://tools.ietf.org/html/rfc9457 ) , and it's predecessor [ RFC-7807] ( https://tools.ietf.org/html/rfc7807 ) .
5
5
6
6
[ ![ Build Status] ( https://travis-ci.org/moogar0880/problems.svg?branch=master )] ( https://travis-ci.org/moogar0880/problems )
7
7
[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/moogar0880/problems )] ( https://goreportcard.com/report/github.com/moogar0880/problems )
8
8
[ ![ GoDoc] ( https://godoc.org/github.com/moogar0880/problems?status.svg )] ( https://godoc.org/github.com/moogar0880/problems )
9
9
10
10
## Usage
11
+
11
12
The problems library exposes an assortment of types to aid HTTP service authors
12
13
in defining and using HTTP Problem detail resources.
13
14
14
15
### Predefined Errors
16
+
15
17
You can define basic Problem details up front by using the ` NewStatusProblem `
16
18
function
17
19
@@ -39,17 +41,18 @@ Which, when served over HTTP as JSON will look like the following:
39
41
```
40
42
41
43
### Detailed Errors
44
+
42
45
New errors can also be created a head of time, or on the fly like so:
43
46
44
47
``` go
45
48
package main
46
49
47
50
import " github.com/moogar0880/problems"
48
51
49
- func NoSuchUser () *problems .DefaultProblem {
50
- nosuch := problems.NewStatusProblem (404 )
51
- nosuch.Detail = " Sorry, that user does not exist."
52
- return nosuch
52
+ func NoSuchUser () *problems .Problem {
53
+ nosuch := problems.NewStatusProblem (404 )
54
+ nosuch.Detail = " Sorry, that user does not exist."
55
+ return nosuch
53
56
}
54
57
```
55
58
@@ -65,6 +68,7 @@ Which, when served over HTTP as JSON will look like the following:
65
68
```
66
69
67
70
### Extended Errors
71
+
68
72
The specification for these HTTP problems was designed to allow for arbitrary
69
73
expansion of the problem resources. This can be accomplished through this
70
74
library by either embedding the ` Problem ` struct in your extension type:
@@ -75,7 +79,7 @@ package main
75
79
import " github.com/moogar0880/problems"
76
80
77
81
type CreditProblem struct {
78
- problems.Problem
82
+ problems.Problem
79
83
80
84
Balance float64 ` json:"balance"`
81
85
Accounts []string ` json:"accounts"`
@@ -100,24 +104,24 @@ Or by using the `problems.ExtendedProblem` type:
100
104
package main
101
105
102
106
import (
103
- " net/http"
107
+ " net/http"
104
108
105
- " github.com/moogar0880/problems"
109
+ " github.com/moogar0880/problems"
106
110
)
107
111
108
112
type CreditProblemExt struct {
109
- Balance float64 ` json:"balance"`
110
- Accounts []string ` json:"accounts"`
113
+ Balance float64 ` json:"balance"`
114
+ Accounts []string ` json:"accounts"`
111
115
}
112
116
113
117
func main () {
114
- problems.NewExt [CreditProblemExt]().
115
- WithStatus (http.StatusForbidden ).
116
- WithDetail (" Your account does not have sufficient funds to complete this transaction" ).
117
- WithExtension (CreditProblemExt{
118
+ problems.NewExt [CreditProblemExt]().
119
+ WithStatus (http.StatusForbidden ).
120
+ WithDetail (" Your account does not have sufficient funds to complete this transaction" ).
121
+ WithExtension (CreditProblemExt{
118
122
Balance: 30 ,
119
123
Accounts: []string {" /account/12345" , " /account/67890" },
120
- })
124
+ })
121
125
}
122
126
```
123
127
@@ -129,13 +133,14 @@ Which, when served over HTTP as JSON will look like the following:
129
133
"title" : " Unauthorized" ,
130
134
"status" : 401 ,
131
135
"extensions" : {
132
- "balance" : 30 ,
133
- "accounts" : [" /account/12345" , " /account/67890" ]
136
+ "balance" : 30 ,
137
+ "accounts" : [" /account/12345" , " /account/67890" ]
134
138
}
135
139
}
136
140
```
137
141
138
142
## Serving Problems
143
+
139
144
Additionally, RFC-7807 defines two new media types for problem resources,
140
145
` application/problem+json" ` and ` application/problem+xml ` . This library defines
141
146
those media types as the constants ` ProblemMediaType ` and
@@ -149,18 +154,18 @@ functioning `HandlerFunc` that will server that error.
149
154
package main
150
155
151
156
import (
152
- " net/http"
157
+ " net/http"
153
158
154
159
" github.com/moogar0880/problems"
155
160
)
156
161
157
162
var Unauthorized = problems.NewStatusProblem (401 )
158
163
159
164
func main () {
160
- mux := http.NewServeMux ()
161
- mux.HandleFunc (" /secrets" , problems.StatusProblemHandler (Unauthorized))
165
+ mux := http.NewServeMux ()
166
+ mux.HandleFunc (" /secrets" , problems.ProblemHandler (Unauthorized))
162
167
163
- server := http.Server {Handler: mux, Addr: " :8080" }
164
- server.ListenAndServe ()
168
+ server := http.Server {Handler: mux, Addr: " :8080" }
169
+ server.ListenAndServe ()
165
170
}
166
171
```
0 commit comments