add doc for the new Problem type

Gerasimos (Makis) Maropoulos 2019-08-12 11:15:47 +03:00
parent 0002a162cc
commit 3bbfb11a89
No known key found for this signature in database
GPG Key ID: F169457BBDA4ACF4

@ -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).