mirror of
https://github.com/safing/portbase
synced 2025-04-23 18:59:08 +00:00
Merge pull request #197 from safing/fix/error-handling-api-modules
Improve error handling in api and modules
This commit is contained in:
commit
0d13bca496
3 changed files with 18 additions and 3 deletions
|
@ -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))
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue