mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-13 02:42:14 +01:00
wip refactoring: better logging and error systems
This commit is contained in:
43
internal/app/api/pkg/middleware/error.go
Normal file
43
internal/app/api/pkg/middleware/error.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
// Error helps handling errors (if any).
|
||||
func Error() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
ctx := context.MustCastFromEchoContext(c)
|
||||
err := next(ctx)
|
||||
if err == nil {
|
||||
// so far so good!
|
||||
return nil
|
||||
}
|
||||
// we log the initial error before returning
|
||||
// the HTTP error.
|
||||
logger := ctx.StandardLogger()
|
||||
logger.Error(err.Error())
|
||||
// handle our custom HTTP error.
|
||||
var httpErr error
|
||||
errCode := standarderror.Code(err)
|
||||
errMessage := standarderror.Message(err)
|
||||
switch errCode {
|
||||
case standarderror.Invalid:
|
||||
httpErr = echo.NewHTTPError(http.StatusBadRequest, errMessage)
|
||||
case standarderror.Timeout:
|
||||
httpErr = echo.NewHTTPError(http.StatusRequestTimeout, errMessage)
|
||||
default:
|
||||
httpErr = echo.NewHTTPError(http.StatusInternalServerError, errMessage)
|
||||
}
|
||||
// required to have a correct status code
|
||||
// in the logs.
|
||||
ctx.Error(httpErr)
|
||||
return httpErr
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user