mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: custom error handler with automatic logging
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
func (api *API) CustomErrorHandler(err error, context echo.Context) {
|
||||
if context.Response().Committed {
|
||||
return
|
||||
}
|
||||
|
||||
custom, ok := err.(types.Error)
|
||||
|
||||
if ok {
|
||||
cause, ok := custom.Cause.(error)
|
||||
|
||||
if ok {
|
||||
api.Logger.Error(cause.Error())
|
||||
}
|
||||
|
||||
context.JSON(custom.Code, map[string][]string{"errors": custom.Messages})
|
||||
}
|
||||
|
||||
herr, ok := err.(*echo.HTTPError)
|
||||
|
||||
if ok {
|
||||
message, ok := herr.Message.(string)
|
||||
|
||||
if ok {
|
||||
context.JSON(herr.Code, map[string][]string{"errors": {message}})
|
||||
}
|
||||
}
|
||||
|
||||
api.instance.DefaultHTTPErrorHandler(err, context)
|
||||
}
|
||||
Reference in New Issue
Block a user