From 3bbfb11a8921ad3c5eb49f1e841af9431f15ff7a Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 12 Aug 2019 11:15:47 +0300 Subject: [PATCH] add doc for the new Problem type --- Routing-error-handlers.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Routing-error-handlers.md b/Routing-error-handlers.md index 7b5159b..5de7643 100644 --- a/Routing-error-handlers.md +++ b/Routing-error-handlers.md @@ -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).