mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
implement Problem Details for HTTP APIs #1335
Former-commit-id: ff789b6d535080c88e05c81ab3fb7d9689801ec7
This commit is contained in:
@@ -7,12 +7,15 @@ import (
|
||||
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 fire the 500 status code`)
|
||||
ctx.HTML(`Click <a href="/my500">here</a> to pretend an HTTP error`)
|
||||
})
|
||||
|
||||
app.Get("/my500", func(ctx iris.Context) {
|
||||
@@ -24,5 +27,60 @@ func main() {
|
||||
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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user