-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
108 lines (93 loc) · 2.98 KB
/
main.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
package main
import (
"github.com/kataras/iris"
)
func main() {
app := iris.New()
// Catch a specific error code.
app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) {
ctx.HTML("Message: <b>" + ctx.Values().GetString("message") + "</b>")
})
// Catch all error codes [app.OnAnyErrorCode...]
app.Get("/", func(ctx iris.Context) {
ctx.HTML(`Click <a href="/my500">here</a> to pretend an HTTP error`)
})
app.Get("/my500", func(ctx iris.Context) {
ctx.Values().Set("message", "this is the error message")
ctx.StatusCode(500)
})
app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) {
ctx.Writef("Hello %s", ctx.Params().Get("firstname"))
})
app.Get("/product-problem", problemExample)
app.Get("/product-error", func(ctx iris.Context) {
ctx.Writef("explain the error")
})
// http://localhost:8080
// http://localhost:8080/my500
// http://localhost:8080/u/gerasimos
// http://localhost:8080/product-problem
app.Run(iris.Addr(":8080"))
}
func newProductProblem(productName, detail string) iris.Problem {
return iris.NewProblem().
// The type URI, if relative it automatically convert to absolute.
Type("/product-error").
// The title, if empty then it gets it from the status code.
Title("Product validation problem").
// Any optional details.
Detail(detail).
// The status error code, required.
Status(iris.StatusBadRequest).
// Any custom key-value pair.
Key("productName", productName)
// Optional cause of the problem, chain of Problems.
// Cause(iris.NewProblem().Type("/error").Title("cause of the problem").Status(400))
}
func problemExample(ctx iris.Context) {
/*
p := iris.NewProblem().
Type("/validation-error").
Title("Your request parameters didn't validate").
Detail("Optional details about the error.").
Status(iris.StatusBadRequest).
Key("customField1", customValue1)
Key("customField2", customValue2)
ctx.Problem(p)
// OR
ctx.Problem(iris.Problem{
"type": "/validation-error",
"title": "Your request parameters didn't validate",
"detail": "Optional details about the error.",
"status": iris.StatusBadRequest,
"customField1": customValue1,
"customField2": customValue2,
})
// OR
*/
// Response like JSON but with indent of " " and
// content type of "application/problem+json"
ctx.Problem(newProductProblem("product name", "problem error details"), iris.ProblemOptions{
// Optional JSON renderer settings.
JSON: iris.JSON{
Indent: " ",
},
// Sets the "Retry-After" response header.
//
// Can accept:
// time.Time for HTTP-Date,
// time.Duration, int64, float64, int for seconds
// or string for date or duration.
// Examples:
// time.Now().Add(5 * time.Minute),
// 300 * time.Second,
// "5m",
//
RetryAfter: 300,
// A function that, if specified, can dynamically set
// retry-after based on the request. Useful for ProblemOptions reusability.
// Overrides the RetryAfter field.
//
// RetryAfterFunc: func(iris.Context) interface{} { [...] }
})
}