Skip to content

🤗 re-write the example of ErrorHandler from type assertion to errors.As #1938

@trim21

Description

@trim21

This is more a suggestion than a question.

fiber/app.go

Lines 75 to 85 in 43aa457

// returned from any handlers in the stack
// cfg := fiber.Config{}
// cfg.ErrorHandler = func(c *Ctx, err error) error {
// code := StatusInternalServerError
// if e, ok := err.(*Error); ok {
// code = e.Code
// }
// c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
// return c.Status(code).SendString(err.Error())
// }
// app := fiber.New(cfg)

this would be better to use errors.As to fit go error handling style

// ErrorHandler defines a function that will process all errors
// returned from any handlers in the stack
//  cfg := fiber.Config{}
//  cfg.ErrorHandler = func(c *Ctx, err error) error {
//   code := StatusInternalServerError
//   var e *fiber.Err
//   if errors.As(err, &e) {
//     code = e.Code
//   }
//   c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
//   return c.Status(code).SendString(err.Error())
//  }
//  app := fiber.New(cfg)
type ErrorHandler = func(*Ctx, error) error

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions