mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
parent
3bbfb11a89
commit
78efe51e00
|
@ -42,19 +42,26 @@ func index(ctx iris.Context) {
|
|||
|
||||
## 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
|
||||
content type of "application/problem+json".
|
||||
The `Context.Problem` method sends a response like `Context.JSON` does but with indent of " " and
|
||||
a content type of "application/problem+json" instead.
|
||||
|
||||
```go
|
||||
func newProductProblem(productName, detail string) iris.Problem {
|
||||
return iris.NewProblem().
|
||||
Type("/product-error").
|
||||
// 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(other iris.Problem)
|
||||
}
|
||||
|
||||
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).
|
||||
|
|
Loading…
Reference in New Issue
Block a user