你可以定义自己的处理器来处理特定 http 错误。

错误码是 大于等于400的Http 状态码,例如 404 not found500 internal server

示例代码:

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main(){
  4. app := iris.New()
  5. app.OnErrorCode(iris.StatusNotFound, notFound)
  6. app.OnErrorCode(iris.StatusInternalServerError, internalServerError)
  7. // to register a handler for all "error"
  8. // status codes(kataras/iris/context.StatusCodeNotSuccessful)
  9. // defaults to < 200 || >= 400:
  10. // app.OnAnyErrorCode(handler)
  11. app.Get("/", index)
  12. app.Run(iris.Addr(":8080"))
  13. }
  14. func notFound(ctx iris.Context) {
  15. // when 404 then render the template
  16. // $views_dir/errors/404.html
  17. ctx.View("errors/404.html")
  18. }
  19. func internalServerError(ctx iris.Context) {
  20. ctx.WriteString("Oups something went wrong, try again")
  21. }
  22. func index(ctx iris.Context) {
  23. ctx.View("index.html")
  24. }

更多内容查看 视图(View) 章节。

问题类型(The Problem type)

Iris 内建支持 HTTP APIs 的错误详情。

Context.Problem 编写一个 JSON 或者 XML 问题响应,行为完全类似 Context.JSON,但是默认 ProblemOptions.JSON 的缩进是 " ",响应的 Content-typeapplication/problem+json

使用 options.RenderXMLXML 字段来改变他的行为,用 application/problem+xml 的文本类型替代。

  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. func fireProblem(ctx iris.Context) {
  17. // Response like JSON but with indent of " " and
  18. // content type of "application/problem+json"
  19. ctx.Problem(newProductProblem("product name", "problem details"),
  20. iris.ProblemOptions{
  21. // Optional JSON renderer settings.
  22. JSON: iris.JSON{
  23. Indent: " ",
  24. },
  25. // OR
  26. // Render as XML:
  27. // RenderXML: true,
  28. // XML: iris.XML{Indent: " "},
  29. // Sets the "Retry-After" response header.
  30. //
  31. // Can accept:
  32. // time.Time for HTTP-Date,
  33. // time.Duration, int64, float64, int for seconds
  34. // or string for date or duration.
  35. // Examples:
  36. // time.Now().Add(5 * time.Minute),
  37. // 300 * time.Second,
  38. // "5m",
  39. //
  40. RetryAfter: 300,
  41. // A function that, if specified, can dynamically set
  42. // retry-after based on the request.
  43. // Useful for ProblemOptions reusability.
  44. // Overrides the RetryAfter field.
  45. //
  46. // RetryAfterFunc: func(iris.Context) interface{} { [...] }
  47. })
  48. }

输出 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. }

RenderXML 设置为 true 的时候,响应将被渲染为 xml

输出 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>