Gerasimos (Makis) Maropoulos 2019-08-12 11:40:49 +03:00
parent 3bbfb11a89
commit 78efe51e00
No known key found for this signature in database
GPG Key ID: F169457BBDA4ACF4

@ -42,19 +42,26 @@ func index(ctx iris.Context) {
## The Problem type ## The Problem type
Iris has a builtin support for the [Problem Details for HTTP APIs](https://tools.ietf.org/html/rfc7807). Iris has 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 The `Context.Problem` method sends a response like `Context.JSON` does but with indent of " " and
content type of "application/problem+json". a content type of "application/problem+json" instead.
```go ```go
func newProductProblem(productName, detail string) iris.Problem { func newProductProblem(productName, detail string) iris.Problem {
return iris.NewProblem(). return iris.NewProblem().
// The type URI, if relative it automatically convert to absolute.
Type("/product-error"). Type("/product-error").
// The title, if empty then it gets it from the status code.
Title("Product validation problem"). Title("Product validation problem").
// Any optional details.
Detail(detail). Detail(detail).
// The status error code, required.
Status(iris.StatusBadRequest). Status(iris.StatusBadRequest).
// Any custom key-value pair.
Key("productName", productName) Key("productName", productName)
// Optional cause of the problem, chain of Problems.
// .Cause(other iris.Problem)
} }
func fireProblem(ctx iris.Context) { func fireProblem(ctx iris.Context) {
@ -62,4 +69,16 @@ func fireProblem(ctx iris.Context) {
} }
``` ```
**Output**
```json
{
"type": "https://host.domain/product-error",
"status": 400,
"title": "Product validation problem",
"detail": "problem error details",
"productName": "product name"
}
```
Full example can be found at [_examples/routing/http-errors](https://github.com/kataras/iris/blob/master/_examples/routing/http-errors/main.go). Full example can be found at [_examples/routing/http-errors](https://github.com/kataras/iris/blob/master/_examples/routing/http-errors/main.go).