1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-06 17:39:57 +02:00

added error event hooks

This commit is contained in:
Gani Georgiev
2022-12-02 16:36:15 +02:00
parent 23fbfab63a
commit 02f72638b8
7 changed files with 70 additions and 9 deletions

View File

@@ -64,19 +64,27 @@ func InitApi(app core.App) (*echo.Echo, error) {
apiErr = NewBadRequestError("", err)
}
// Send response
var cErr error
if c.Request().Method == http.MethodHead {
// @see https://github.com/labstack/echo/issues/608
cErr = c.NoContent(apiErr.Code)
} else {
cErr = c.JSON(apiErr.Code, apiErr)
event := &core.ApiErrorEvent{
HttpContext: c,
Error: apiErr,
}
hookErr := app.OnBeforeApiError().Trigger(event, func(e *core.ApiErrorEvent) error {
// Send response
if e.HttpContext.Request().Method == http.MethodHead {
// @see https://github.com/labstack/echo/issues/608
return e.HttpContext.NoContent(apiErr.Code)
}
return e.HttpContext.JSON(apiErr.Code, apiErr)
})
// truly rare case; eg. client already disconnected
if cErr != nil && app.IsDebug() {
log.Println(cErr)
if hookErr != nil && app.IsDebug() {
log.Println(hookErr)
}
app.OnAfterApiError().Trigger(event)
}
// admin ui routes