Merge pull request from safing/fix/error-handling-api-modules

Improve error handling in api and modules
This commit is contained in:
Daniel Hovie 2022-12-12 15:01:16 +01:00 committed by GitHub
commit 0d13bca496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -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>
@ -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))

View file

@ -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,

View file

@ -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,