mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +00:00
Improve error handling in api and modules
This commit is contained in:
parent
72288a45d7
commit
d90d14ce02
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 {
|
func (api *DatabaseAPI) handler(context.Context) error {
|
||||||
|
defer func() {
|
||||||
|
_ = api.shutdown(nil)
|
||||||
|
}()
|
||||||
|
|
||||||
// 123|get|<key>
|
// 123|get|<key>
|
||||||
// 123|ok|<key>|<data>
|
// 123|ok|<key>|<data>
|
||||||
// 123|error|<message>
|
// 123|error|<message>
|
||||||
|
@ -206,6 +210,10 @@ func (api *DatabaseAPI) handler(context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *DatabaseAPI) writer(ctx context.Context) error {
|
func (api *DatabaseAPI) writer(ctx context.Context) error {
|
||||||
|
defer func() {
|
||||||
|
_ = api.shutdown(nil)
|
||||||
|
}()
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -214,12 +222,12 @@ func (api *DatabaseAPI) writer(ctx context.Context) error {
|
||||||
// prioritize direct writes
|
// prioritize direct writes
|
||||||
case data = <-api.sendQueue:
|
case data = <-api.sendQueue:
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return api.shutdown(nil)
|
return nil
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return api.shutdown(nil)
|
return nil
|
||||||
case <-api.shutdownSignal:
|
case <-api.shutdownSignal:
|
||||||
return api.shutdown(nil)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Tracef("api: sending %s", string(*msg))
|
// 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.
|
// Format panics in handler.
|
||||||
defer func() {
|
defer func() {
|
||||||
if panicValue := recover(); panicValue != nil {
|
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() {
|
if devMode() {
|
||||||
http.Error(
|
http.Error(
|
||||||
lrw,
|
lrw,
|
||||||
|
|
|
@ -304,6 +304,9 @@ func (m *Module) stopAllTasks(reports chan *report, stopComplete chan struct{})
|
||||||
m.Unlock()
|
m.Unlock()
|
||||||
m.notifyOfChange()
|
m.notifyOfChange()
|
||||||
|
|
||||||
|
// Resolve any errors still on the module.
|
||||||
|
m.Resolve("")
|
||||||
|
|
||||||
// send report
|
// send report
|
||||||
reports <- &report{
|
reports <- &report{
|
||||||
module: m,
|
module: m,
|
||||||
|
|
Loading…
Add table
Reference in a new issue