From d90d14ce028f5b55fe9c06090191b122e7507238 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 12 Dec 2022 14:07:30 +0100 Subject: [PATCH] Improve error handling in api and modules --- api/database.go | 14 +++++++++++--- api/router.go | 4 ++++ modules/modules.go | 3 +++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/api/database.go b/api/database.go index 5df98be9..a6d78097 100644 --- a/api/database.go +++ b/api/database.go @@ -103,6 +103,10 @@ func startDatabaseAPI(w http.ResponseWriter, r *http.Request) { } func (api *DatabaseAPI) handler(context.Context) error { + defer func() { + _ = api.shutdown(nil) + }() + // 123|get| // 123|ok|| // 123|error| @@ -206,6 +210,10 @@ func (api *DatabaseAPI) handler(context.Context) error { } func (api *DatabaseAPI) writer(ctx context.Context) error { + defer func() { + _ = api.shutdown(nil) + }() + var data []byte var err error @@ -214,12 +222,12 @@ func (api *DatabaseAPI) writer(ctx context.Context) error { // prioritize direct writes case data = <-api.sendQueue: if len(data) == 0 { - return api.shutdown(nil) + return nil } case <-ctx.Done(): - return api.shutdown(nil) + return nil case <-api.shutdownSignal: - return api.shutdown(nil) + return nil } // log.Tracef("api: sending %s", string(*msg)) diff --git a/api/router.go b/api/router.go index 6cc9ae45..c12d20b4 100644 --- a/api/router.go +++ b/api/router.go @@ -258,6 +258,10 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error { // Format panics in handler. defer func() { if panicValue := recover(); panicValue != nil { + // Report failure via module system. + me := module.NewPanicError("api request", "custom", panicValue) + me.Report() + // Respond with a server error. if devMode() { http.Error( lrw, diff --git a/modules/modules.go b/modules/modules.go index 8c2b20a8..5f35d52d 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -304,6 +304,9 @@ func (m *Module) stopAllTasks(reports chan *report, stopComplete chan struct{}) m.Unlock() m.notifyOfChange() + // Resolve any errors still on the module. + m.Resolve("") + // send report reports <- &report{ module: m,