Skip to content

Commit

Permalink
added error event hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Dec 2, 2022
1 parent 23fbfab commit 02f7263
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
```go
app.OnBeforeBootstrap()
app.OnAfterBootstrap()
app.OnBeforeApiError()
app.OnAfterApiError()
app.OnRealtimeDisconnectRequest()
app.OnRealtimeBeforeMessageSend()
app.OnRealtimeAfterMessageSend()
Expand Down
26 changes: 17 additions & 9 deletions apis/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ---------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions core/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]{},
Expand Down Expand Up @@ -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
// -------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type ServeEvent struct {
Router *echo.Echo
}

type ApiErrorEvent struct {
HttpContext echo.Context
Error error
}

// -------------------------------------------------------------------
// Model DAO events data
// -------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions tests/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ func (scenario *ApiScenario) Test(t *testing.T) {
}
}

// to minimize the breaking changes we always expect the error
// events to be called on API error
if res.StatusCode >= 400 {
if scenario.ExpectedEvents == nil {
scenario.ExpectedEvents = map[string]int{}
}
if _, ok := scenario.ExpectedEvents["OnBeforeApiError"]; !ok {
scenario.ExpectedEvents["OnBeforeApiError"] = 1
}
if _, ok := scenario.ExpectedEvents["OnAfterApiError"]; !ok {
scenario.ExpectedEvents["OnAfterApiError"] = 1
}
}

if len(testApp.EventCalls) > len(scenario.ExpectedEvents) {
t.Errorf("[%s] Expected events %v, got %v", prefix, scenario.ExpectedEvents, testApp.EventCalls)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
TestMailer: &TestMailer{},
}

t.OnBeforeApiError().Add(func(e *core.ApiErrorEvent) error {
t.EventCalls["OnBeforeApiError"]++
return nil
})

t.OnAfterApiError().Add(func(e *core.ApiErrorEvent) error {
t.EventCalls["OnAfterApiError"]++
return nil
})

t.OnModelBeforeCreate().Add(func(e *core.ModelEvent) error {
t.EventCalls["OnModelBeforeCreate"]++
return nil
Expand Down

0 comments on commit 02f7263

Please sign in to comment.