Skip to content

Commit aa72400

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents cb34893 + af53e92 commit aa72400

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

README.md

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616
1717
Our problem details response body and headers will be look like this:
1818
```go
19-
// Response body
19+
// Response body
2020

2121
{
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
2828
}
2929
```
3030
```go
31-
// Response headers
31+
// Response headers
3232

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
3535
```
3636
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).
3737

@@ -69,18 +69,18 @@ In this sample we map status code `StatusBadGateway` to `StatusUnauthorized` bas
6969
```go
7070
// handle specific status code to problem details error
7171
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)
7474
}
7575
```
7676
```go
7777
// problem details handler config
7878
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+
}
8484
})
8585
```
8686
#### Map Custom Type Error:
@@ -90,18 +90,18 @@ In this sample we map custom error type to problem details error.
9090
```go
9191
// handle custom type error to problem details error
9292
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")
9494
return custom_errors.BadRequestError{InternalError: err}
9595
}
9696
```
9797
```go
9898
// problem details handler config
9999
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+
}
105105
})
106106
```
107107

@@ -134,18 +134,18 @@ In this sample we map status code `StatusBadGateway` to `StatusUnauthorized` bas
134134
```go
135135
// handle specific status code to problem details error
136136
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)
139139
}
140140
```
141141
```go
142142
// problem details handler config
143143
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+
}
149149
})
150150
```
151151
#### Map Custom Type Error:
@@ -155,20 +155,19 @@ In this sample we map custom error type to problem details error.
155155
```go
156156
// handle custom type error to problem details error
157157
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")
160159
customBadRequestError := custom_errors.BadRequestError{InternalError: err}
161160
_ = c.Error(customBadRequestError)
162161
}
163162
```
164163
```go
165164
// problem details handler config
166165
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+
}
172171
})
173172
```
174173

@@ -178,23 +177,23 @@ We support custom problem details error for create more flexibility response err
178177
```go
179178
// custom problem details
180179
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"`
184183
}
185184
```
186185
```go
187186
// problem details handler config
188187
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+
}
198197
})
199198
```
200199

0 commit comments

Comments
 (0)