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
+10
View File
@@ -92,6 +92,16 @@ type App interface {
// allowing you to adjust its options and attach new routes.
OnBeforeServe() *hook.Hook[*ServeEvent]
// OnBeforeApiError hook is triggered right before sending an error API
// response to the client, allowing you to further modify the error data
// or to return a completely different API response (using [hook.StopPropagation]).
OnBeforeApiError() *hook.Hook[*ApiErrorEvent]
// OnAfterApiError hook is triggered right after sending an error API
// response to the client.
// It could be used to log the final API error in external services.
OnAfterApiError() *hook.Hook[*ApiErrorEvent]
// ---------------------------------------------------------------
// Dao event hooks
// ---------------------------------------------------------------
+12
View File
@@ -43,6 +43,8 @@ type BaseApp struct {
onBeforeBootstrap *hook.Hook[*BootstrapEvent]
onAfterBootstrap *hook.Hook[*BootstrapEvent]
onBeforeServe *hook.Hook[*ServeEvent]
onBeforeApiError *hook.Hook[*ApiErrorEvent]
onAfterApiError *hook.Hook[*ApiErrorEvent]
// dao event hooks
onModelBeforeCreate *hook.Hook[*ModelEvent]
@@ -135,6 +137,8 @@ func NewBaseApp(dataDir string, encryptionEnv string, isDebug bool) *BaseApp {
onBeforeBootstrap: &hook.Hook[*BootstrapEvent]{},
onAfterBootstrap: &hook.Hook[*BootstrapEvent]{},
onBeforeServe: &hook.Hook[*ServeEvent]{},
onBeforeApiError: &hook.Hook[*ApiErrorEvent]{},
onAfterApiError: &hook.Hook[*ApiErrorEvent]{},
// dao event hooks
onModelBeforeCreate: &hook.Hook[*ModelEvent]{},
@@ -405,6 +409,14 @@ func (app *BaseApp) OnBeforeServe() *hook.Hook[*ServeEvent] {
return app.onBeforeServe
}
func (app *BaseApp) OnBeforeApiError() *hook.Hook[*ApiErrorEvent] {
return app.onBeforeApiError
}
func (app *BaseApp) OnAfterApiError() *hook.Hook[*ApiErrorEvent] {
return app.onAfterApiError
}
// -------------------------------------------------------------------
// Dao event hooks
// -------------------------------------------------------------------
+5
View File
@@ -25,6 +25,11 @@ type ServeEvent struct {
Router *echo.Echo
}
type ApiErrorEvent struct {
HttpContext echo.Context
Error error
}
// -------------------------------------------------------------------
// Model DAO events data
// -------------------------------------------------------------------