Problem
Content-Type: "application/problem+json" or "application/problem+xml"
Iris has builtin support for the Problem Details for HTTP APIs.
The Context.Problem method sends a JSON or XML problem response. Behaves exactly like Context.JSON but with default ProblemOptions.JSON indent of two spaces (" ") and a response content type of "application/problem+json" instead.
1
Problem(v interface{}, opts ...ProblemOptions) (int, error)
Copied!
The ProblemOptions looks like that:
1
// ProblemOptions the optional settings when server replies with a Problem.
2
// See `Context.Problem` method and `Problem` type for more details.
3
type ProblemOptions struct {
4
// JSON are the optional JSON renderer options.
5
JSON JSON
6
7
// RenderXML set to true if want to render as XML doc.
8
// See `XML` option field too.
9
RenderXML bool
10
// XML are the optional XML renderer options.
11
// Affect only when `RenderXML` field is set to true.
12
XML XML
13
14
// RetryAfter sets the Retry-After response header.
15
// https://tools.ietf.org/html/rfc7231#section-7.1.3
16
// The value can be one of those:
17
// time.Time
18
// time.Duration for seconds
19
// int64, int, float64 for seconds
20
// string for duration string or for datetime string.
21
//
22
// Examples:
23
// time.Now().Add(5 * time.Minute),
24
// 300 * time.Second,
25
// "5m",
26
// 300
27
RetryAfter interface{}
28
// A function that, if specified, can dynamically set
29
// retry-after based on the request. Useful for ProblemOptions reusability.
30
// Should return time.Time, time.Duration, int64, int, float64 or string.
31
//
32
// Overrides the RetryAfter field.
33
RetryAfterFunc func(Context) interface{}
34
}
Copied!
Use the options.RenderXML and XML fields to change this behavior and send a response of content type "application/problem+xml" instead.
1
func newProductProblem(productName, detail string) iris.Problem {
2
return iris.NewProblem().
3
// The type URI, if relative it automatically convert to absolute.
4
Type("/product-error").
5
// The title, if empty then it gets it from the status code.
6
Title("Product validation problem").
7
// Any optional details.
8
Detail(detail).
9
// The status error code, required.
10
Status(iris.StatusBadRequest).
11
// Any custom key-value pair.
12
Key("productName", productName)
13
// Optional cause of the problem, chain of Problems.
14
// .Cause(other iris.Problem)
15
}
16
17
func fireProblem(ctx iris.Context) {
18
response := newProductProblem("product name", "problem details")
19
options := iris.ProblemOptions{
20
JSON: iris.JSON{
21
Indent: " ",
22
},
23
RetryAfter: 300,
24
}
25
26
ctx.Problem(response, options)
27
}
Copied!
Outputs "application/problem+json"
1
{
2
"type": "https://host.domain/product-error",
3
"status": 400,
4
"title": "Product validation problem",
5
"detail": "problem error details",
6
"productName": "product name"
7
}
Copied!
When RenderXML is set to true then the response will look be rendered as XML instead.
Outputs "application/problem+xml"
1
<Problem>
2
<Type>https://host.domain/product-error</Type>
3
<Status>400</Status>
4
<Title>Product validation problem</Title>
5
<Detail>problem error details</Detail>
6
<ProductName>product name</ProductName>
7
</Problem>
Copied!
Last modified 1yr ago
Export as PDF
Copy link