mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 03:01:03 +01:00
8e83959c61
Former-commit-id: ff789b6d535080c88e05c81ab3fb7d9689801ec7
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/kataras/iris"
|
|
)
|
|
|
|
func main() {
|
|
app := iris.New()
|
|
|
|
// Catch a specific error code.
|
|
app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) {
|
|
ctx.HTML("Message: <b>" + ctx.Values().GetString("message") + "</b>")
|
|
})
|
|
|
|
// Catch all error codes [app.OnAnyErrorCode...]
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
ctx.HTML(`Click <a href="/my500">here</a> to pretend an HTTP error`)
|
|
})
|
|
|
|
app.Get("/my500", func(ctx iris.Context) {
|
|
ctx.Values().Set("message", "this is the error message")
|
|
ctx.StatusCode(500)
|
|
})
|
|
|
|
app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) {
|
|
ctx.Writef("Hello %s", ctx.Params().Get("firstname"))
|
|
})
|
|
|
|
app.Get("/product-problem", problemExample)
|
|
|
|
app.Get("/product-error", func(ctx iris.Context) {
|
|
ctx.Writef("explain the error")
|
|
})
|
|
|
|
// http://localhost:8080
|
|
// http://localhost:8080/my500
|
|
// http://localhost:8080/u/gerasimos
|
|
// http://localhost:8080/product-problem
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|
|
|
|
func newProductProblem(productName, detail string) iris.Problem {
|
|
return iris.NewProblem().
|
|
// 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(iris.NewProblem().Type("/error").Title("cause of the problem").Status(400))
|
|
}
|
|
|
|
func problemExample(ctx iris.Context) {
|
|
/*
|
|
p := iris.NewProblem().
|
|
Type("/validation-error").
|
|
Title("Your request parameters didn't validate").
|
|
Detail("Optional details about the error.").
|
|
Status(iris.StatusBadRequest).
|
|
Key("customField1", customValue1)
|
|
Key("customField2", customValue2)
|
|
ctx.Problem(p)
|
|
|
|
// OR
|
|
ctx.Problem(iris.Problem{
|
|
"type": "/validation-error",
|
|
"title": "Your request parameters didn't validate",
|
|
"detail": "Optional details about the error.",
|
|
"status": iris.StatusBadRequest,
|
|
"customField1": customValue1,
|
|
"customField2": customValue2,
|
|
})
|
|
|
|
// OR
|
|
*/
|
|
|
|
// Response like JSON but with indent of " " and
|
|
// content type of "application/problem+json"
|
|
ctx.Problem(newProductProblem("product name", "problem error details"))
|
|
}
|