1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

NEW (HOT) FEATURE: Add custom error handlers on path type parameters error

This commit is contained in:
Gerasimos (Makis) Maropoulos
2021-02-14 15:31:27 +02:00
parent 567c06702f
commit 5990e7f432
7 changed files with 140 additions and 69 deletions

View File

@@ -152,6 +152,15 @@ func main() {
// +------------------------+
// UUIDv4 (and v1) path parameter validation.
// Optionally, set custom handler on path parameter type error:
app.Macros().Get("uuid").HandleError(func(ctx iris.Context, err error) {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(iris.Map{
"error": err.Error(),
"message": "invalid path parameter",
})
})
// http://localhost:8080/user/bb4f33e4-dc08-40d8-9f2b-e8b2bb615c0e -> OK
// http://localhost:8080/user/dsadsa-invalid-uuid -> NOT FOUND
app.Get("/user/{id:uuid}", func(ctx iris.Context) {