mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add doc for the new Problem type
parent
0002a162cc
commit
3bbfb11a89
|
@ -2,7 +2,7 @@
|
|||
|
||||
You can define your own handlers when a specific http error code occurs.
|
||||
|
||||
> Error codes are the http status codes that are bigger than or equal to 400, like 404 not found and 500 internal server.
|
||||
Error codes are the http status codes that are bigger than or equal to 400, like 404 not found and 500 internal server error.
|
||||
|
||||
Example code:
|
||||
|
||||
|
@ -37,3 +37,29 @@ func index(ctx iris.Context) {
|
|||
ctx.View("index.html")
|
||||
}
|
||||
```
|
||||
|
||||
> Learn more about [[View]].
|
||||
|
||||
## The Problem type
|
||||
|
||||
Iris has a builtin support for the [Problem Details for HTTP APIs](https://tools.ietf.org/html/rfc7807).
|
||||
|
||||
The `Context.Problem` sends a response like JSON but with indent of " " and
|
||||
content type of "application/problem+json".
|
||||
|
||||
```go
|
||||
func newProductProblem(productName, detail string) iris.Problem {
|
||||
return iris.NewProblem().
|
||||
Type("/product-error").
|
||||
Title("Product validation problem").
|
||||
Detail(detail).
|
||||
Status(iris.StatusBadRequest).
|
||||
Key("productName", productName)
|
||||
}
|
||||
|
||||
func fireProblem(ctx iris.Context) {
|
||||
ctx.Problem(newProductProblem("product name", "problem error details"))
|
||||
}
|
||||
```
|
||||
|
||||
Full example can be found at [_examples/routing/http-errors](https://github.com/kataras/iris/blob/master/_examples/routing/http-errors/main.go).
|
||||
|
|
Loading…
Reference in New Issue
Block a user