16
16
17
17
Our problem details response body and headers will be look like this:
18
18
``` go
19
- // Response body
19
+ // Response body
20
20
21
21
{
22
- " status" : 400 , // The HTTP status code generated on the problem occurrence
23
- " title" : " bad-request" , // A short human-readable problem summary
24
- " detail" : " We have a bad request in our endpoint" , // A human-readable explanation for what exactly happened
25
- " type" : " https://httpstatuses.io/400" , // URI reference to identify the problem type
26
- " instance" : " /sample1" , // URI reference of the occurrence
27
- " stackTrace" : " some more trace for error" , // More trace information error for what exactly happened
22
+ " status" : 400 , // The HTTP status code generated on the problem occurrence
23
+ " title" : " bad-request" , // A short human-readable problem summary
24
+ " detail" : " We have a bad request in our endpoint" , // A human-readable explanation for what exactly happened
25
+ " type" : " https://httpstatuses.io/400" , // URI reference to identify the problem type
26
+ " instance" : " /sample1" , // URI reference of the occurrence
27
+ " stackTrace" : " some more trace for error" , // More trace information error for what exactly happened
28
28
}
29
29
```
30
30
``` go
31
- // Response headers
31
+ // Response headers
32
32
33
- content-type : application/problem+json
34
- date: Thu ,29 Sep 2022 14 :07 :23 GMT
33
+ content-type : application/problem+json
34
+ date: Thu ,29 Sep 2022 14 :07 :23 GMT
35
35
```
36
36
There are some samples for using this package on top of Echo [ here] ( ./sample/cmd/echo/main.go ) and for Gin [ here] ( ./sample/cmd/gin/main.go ) .
37
37
@@ -69,18 +69,18 @@ In this sample we map status code `StatusBadGateway` to `StatusUnauthorized` bas
69
69
``` go
70
70
// handle specific status code to problem details error
71
71
func sample1 (c echo .Context ) error {
72
- err := errors.New (" We have a specific status code error in our endpoint" )
73
- return echo.NewHTTPError (http.StatusBadGateway , err)
72
+ err := errors.New (" We have a specific status code error in our endpoint" )
73
+ return echo.NewHTTPError (http.StatusBadGateway , err)
74
74
}
75
75
```
76
76
``` go
77
77
// problem details handler config
78
78
problem.MapStatus (http.StatusBadGateway , func () problem.ProblemDetailErr {
79
- return &problem.ProblemDetail {
80
- Status: http.StatusUnauthorized ,
81
- Title: " unauthorized" ,
82
- Detail: error .Error (),
83
- }
79
+ return &problem.ProblemDetail {
80
+ Status: http.StatusUnauthorized ,
81
+ Title: " unauthorized" ,
82
+ Detail: error .Error (),
83
+ }
84
84
})
85
85
```
86
86
#### Map Custom Type Error:
@@ -90,18 +90,18 @@ In this sample we map custom error type to problem details error.
90
90
``` go
91
91
// handle custom type error to problem details error
92
92
func sample2 (c echo .Context ) error {
93
- err := errors.New (" We have a custom type error in our endpoint" )
93
+ err := errors.New (" We have a custom type error in our endpoint" )
94
94
return custom_errors.BadRequestError {InternalError: err}
95
95
}
96
96
```
97
97
``` go
98
98
// problem details handler config
99
99
problem.Map [custom_errors.BadRequestError ](func () problem.ProblemDetailErr {
100
- return &problem.ProblemDetail {
101
- Status: http.StatusBadRequest ,
102
- Title: " bad request" ,
103
- Detail: error .Error (),
104
- }
100
+ return &problem.ProblemDetail {
101
+ Status: http.StatusBadRequest ,
102
+ Title: " bad request" ,
103
+ Detail: error .Error (),
104
+ }
105
105
})
106
106
```
107
107
@@ -134,18 +134,18 @@ In this sample we map status code `StatusBadGateway` to `StatusUnauthorized` bas
134
134
``` go
135
135
// handle specific status code to problem details error
136
136
func sample1 (c *gin .Context ) {
137
- err := errors.New (" We have a specific status code error in our endpoint" )
138
- _ = c.AbortWithError (http.StatusBadGateway , err)
137
+ err := errors.New (" We have a specific status code error in our endpoint" )
138
+ _ = c.AbortWithError (http.StatusBadGateway , err)
139
139
}
140
140
```
141
141
``` go
142
142
// problem details handler config
143
143
problem.MapStatus (http.StatusBadGateway , func () problem.ProblemDetailErr {
144
- return &problem.ProblemDetail {
145
- Status: http.StatusUnauthorized ,
146
- Title: " unauthorized" ,
147
- Detail: err.Error (),
148
- }
144
+ return &problem.ProblemDetail {
145
+ Status: http.StatusUnauthorized ,
146
+ Title: " unauthorized" ,
147
+ Detail: err.Error (),
148
+ }
149
149
})
150
150
```
151
151
#### Map Custom Type Error:
@@ -155,20 +155,19 @@ In this sample we map custom error type to problem details error.
155
155
``` go
156
156
// handle custom type error to problem details error
157
157
func sample2 (c *gin .Context ) {
158
-
159
- err := errors.New (" We have a custom type error in our endpoint" )
158
+ err := errors.New (" We have a custom type error in our endpoint" )
160
159
customBadRequestError := custom_errors.BadRequestError {InternalError: err}
161
160
_ = c.Error (customBadRequestError)
162
161
}
163
162
```
164
163
``` go
165
164
// problem details handler config
166
165
problem.Map [custom_errors.BadRequestError ](func () problem.ProblemDetailErr {
167
- return &problem.ProblemDetail {
168
- Status: http.StatusBadRequest ,
169
- Title: " bad request" ,
170
- Detail: err.Error (),
171
- }
166
+ return &problem.ProblemDetail {
167
+ Status: http.StatusBadRequest ,
168
+ Title: " bad request" ,
169
+ Detail: err.Error (),
170
+ }
172
171
})
173
172
```
174
173
@@ -178,23 +177,23 @@ We support custom problem details error for create more flexibility response err
178
177
``` go
179
178
// custom problem details
180
179
type CustomProblemDetail struct {
181
- problem.ProblemDetailErr
182
- Description string ` json:"description,omitempty"`
183
- AdditionalInfo string ` json:"additionalInfo,omitempty"`
180
+ problem.ProblemDetailErr
181
+ Description string ` json:"description,omitempty"`
182
+ AdditionalInfo string ` json:"additionalInfo,omitempty"`
184
183
}
185
184
```
186
185
``` go
187
186
// problem details handler config
188
187
problem.Map [custom_errors.ConflictError ](func () problem.ProblemDetailErr {
189
- return &custom_problems.CustomProblemDetail {
190
- ProblemDetailErr: &problem.ProblemDetail {
191
- Status: http.StatusConflict ,
192
- Title: " conflict" ,
193
- Detail: error .Error (),
194
- },
195
- AdditionalInfo: " some additional info..." ,
196
- Description: " some description..." ,
197
- }
188
+ return &custom_problems.CustomProblemDetail {
189
+ ProblemDetailErr: &problem.ProblemDetail {
190
+ Status: http.StatusConflict ,
191
+ Title: " conflict" ,
192
+ Detail: error .Error (),
193
+ },
194
+ AdditionalInfo: " some additional info..." ,
195
+ Description: " some description..." ,
196
+ }
198
197
})
199
198
```
200
199
0 commit comments