Skip to content

Return error in fiber-middleware.ErrorHandler  #22

@illiafox

Description

@illiafox

It seems that the whole fiber-middleware package was fully copied from gin-middleware.

Unlike gin, fiber explicitly returns errors in handlers and does not have neither ctx.Abort() or ctx.Error() method

// fiber.go

// Handler defines a function to serve HTTP requests.
type Handler = func(*Ctx) error

fibermiddleware.Options has ErrorHandler field, but it does not return error as it was designed for gin.Context.Abort() or gin.Context.Error() method call inside.

https://github.com/deepmap/oapi-codegen/blob/ad62d73d8d22aa251d06b490b259790919899eab/pkg/fiber-middleware/oapi_validate.go#L49-L50

https://github.com/deepmap/oapi-codegen/blob/ad62d73d8d22aa251d06b490b259790919899eab/pkg/fiber-middleware/oapi_validate.go#L75-L85

ErrorHandler should

  1. Pass error instead of string so that developers could use errors.As/Is.
  2. Return error in order to handle it by fiber middlewares

Code example:

type ErrorHandler func(c *fiber.Ctx, err error, statusCode int) error

...

err := ValidateRequestFromContext(c, router, options)
if err != nil {
	if options != nil && options.ErrorHandler != nil {
		return options.ErrorHandler(c, err, http.StatusBadRequest)
	} else {
		return fiber.NewError(http.StatusBadRequest, err.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