Skip to content

Commit

Permalink
Improve error handling in api and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Dec 12, 2022
1 parent 72288a4 commit d90d14c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
14 changes: 11 additions & 3 deletions api/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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|<key>
// 123|ok|<key>|<data>
// 123|error|<message>
Expand Down Expand Up @@ -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

Expand All @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d90d14c

Please sign in to comment.