-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoc_test.go
109 lines (100 loc) · 2.78 KB
/
doc_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package problems_test
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/moogar0880/problems"
)
func ExampleNewStatusProblem() {
notFound := problems.NewStatusProblem(404)
b, _ := json.MarshalIndent(notFound, "", " ")
fmt.Println(string(b))
// Output: {
// "type": "about:blank",
// "title": "Not Found",
// "status": 404
// }
}
func ExampleNewStatusProblem_detailed() {
notFound := problems.NewStatusProblem(404)
notFound.Detail = "The item you've requested either does not exist or has been deleted."
b, _ := json.MarshalIndent(notFound, "", " ")
fmt.Println(string(b))
// Output: {
// "type": "about:blank",
// "title": "Not Found",
// "status": 404,
// "detail": "The item you've requested either does not exist or has been deleted."
// }
}
func ExampleFromError() {
err := func() error {
// Some fallible function.
return errors.New("something bad happened")
}()
internalServerError := problems.FromError(err).WithStatus(http.StatusInternalServerError)
b, _ := json.MarshalIndent(internalServerError, "", " ")
fmt.Println(string(b))
// Output: {
// "type": "about:blank",
// "title": "Internal Server Error",
// "status": 500,
// "detail": "something bad happened"
// }
}
func ExampleExtendedProblem() {
type CreditProblemExt struct {
Balance float64 `json:"balance"`
Accounts []string `json:"accounts"`
}
problem := problems.NewExt[CreditProblemExt]().
WithStatus(http.StatusForbidden).
WithDetail("You do not have sufficient funds to complete this transaction.").
WithExtension(CreditProblemExt{
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
})
b, _ := json.MarshalIndent(problem, "", " ")
fmt.Println(string(b))
// Output: {
// "type": "about:blank",
// "title": "Forbidden",
// "status": 403,
// "detail": "You do not have sufficient funds to complete this transaction.",
// "extensions": {
// "balance": 30,
// "accounts": [
// "/account/12345",
// "/account/67890"
// ]
// }
// }
}
func ExampleExtendedProblem_embedding() {
type CreditProblem struct {
problems.Problem
Balance float64 `json:"balance"`
Accounts []string `json:"accounts"`
}
problem := &CreditProblem{
Problem: *problems.New().
WithStatus(http.StatusForbidden).
WithDetail("You do not have sufficient funds to complete this transaction."),
Balance: 30,
Accounts: []string{"/account/12345", "/account/67890"},
}
b, _ := json.MarshalIndent(problem, "", " ")
fmt.Println(string(b))
// Output: {
// "type": "about:blank",
// "title": "Forbidden",
// "status": 403,
// "detail": "You do not have sufficient funds to complete this transaction.",
// "balance": 30,
// "accounts": [
// "/account/12345",
// "/account/67890"
// ]
// }
}