mirror of
https://github.com/safing/portbase
synced 2025-09-02 10:40:39 +00:00
Merge pull request #145 from safing/fix/patch-set-4
Improve error handling
This commit is contained in:
commit
bb456a2953
9 changed files with 46 additions and 17 deletions
|
@ -305,6 +305,9 @@ func (e *Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
case e.ActionFunc != nil:
|
case e.ActionFunc != nil:
|
||||||
var msg string
|
var msg string
|
||||||
msg, err = e.ActionFunc(apiRequest)
|
msg, err = e.ActionFunc(apiRequest)
|
||||||
|
if !strings.HasSuffix(msg, "\n") {
|
||||||
|
msg += "\n"
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
responseData = []byte(msg)
|
responseData = []byte(msg)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,34 +8,36 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
// "github.com/pkg/bson"
|
"github.com/fxamacker/cbor/v2"
|
||||||
|
|
||||||
"github.com/safing/portbase/formats/varint"
|
"github.com/safing/portbase/formats/varint"
|
||||||
"github.com/safing/portbase/utils"
|
"github.com/safing/portbase/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// define types
|
// Types.
|
||||||
const (
|
const (
|
||||||
AUTO = 0
|
AUTO = 0
|
||||||
NONE = 1
|
NONE = 1
|
||||||
|
|
||||||
// special
|
// Special types.
|
||||||
LIST = 76 // L
|
LIST = 76 // L
|
||||||
|
|
||||||
// serialization
|
// Serialization types.
|
||||||
|
BSON = 66 // B
|
||||||
|
CBOR = 67 // C
|
||||||
|
GenCode = 71 // G
|
||||||
|
JSON = 74 // J
|
||||||
STRING = 83 // S
|
STRING = 83 // S
|
||||||
BYTES = 88 // X
|
BYTES = 88 // X
|
||||||
JSON = 74 // J
|
|
||||||
BSON = 66 // B
|
|
||||||
GenCode = 71 // G
|
|
||||||
|
|
||||||
// compression
|
// Compression types.
|
||||||
GZIP = 90 // Z
|
GZIP = 90 // Z
|
||||||
)
|
)
|
||||||
|
|
||||||
// define errors
|
// Errors.
|
||||||
var errNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
|
var (
|
||||||
var errNotImplemented = errors.New("dsd: this type is not yet implemented")
|
errNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
|
||||||
|
errNotImplemented = errors.New("dsd: this type is not yet implemented")
|
||||||
|
)
|
||||||
|
|
||||||
// Load loads an dsd structured data blob into the given interface.
|
// Load loads an dsd structured data blob into the given interface.
|
||||||
func Load(data []byte, t interface{}) (interface{}, error) {
|
func Load(data []byte, t interface{}) (interface{}, error) {
|
||||||
|
@ -75,6 +77,12 @@ func LoadAsFormat(data []byte, format uint8, t interface{}) (interface{}, error)
|
||||||
// return nil, err
|
// return nil, err
|
||||||
// }
|
// }
|
||||||
// return t, nil
|
// return t, nil
|
||||||
|
case CBOR:
|
||||||
|
err := cbor.Unmarshal(data, t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("dsd: failed to unpack cbor: %s, data: %s", err, utils.SafeFirst16Bytes(data))
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
case GenCode:
|
case GenCode:
|
||||||
genCodeStruct, ok := t.(GenCodeCompatible)
|
genCodeStruct, ok := t.(GenCodeCompatible)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -132,6 +140,11 @@ func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return nil, err
|
// return nil, err
|
||||||
// }
|
// }
|
||||||
|
case CBOR:
|
||||||
|
data, err = cbor.Marshal(t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case GenCode:
|
case GenCode:
|
||||||
genCodeStruct, ok := t.(GenCodeCompatible)
|
genCodeStruct, ok := t.(GenCodeCompatible)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -3,12 +3,11 @@ package dsd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate msgp
|
|
||||||
|
|
||||||
// SimpleTestStruct is used for testing.
|
// SimpleTestStruct is used for testing.
|
||||||
type SimpleTestStruct struct {
|
type SimpleTestStruct struct {
|
||||||
S string
|
S string
|
||||||
|
@ -26,6 +25,7 @@ type ComplexTestStruct struct {
|
||||||
UI16 uint16
|
UI16 uint16
|
||||||
UI32 uint32
|
UI32 uint32
|
||||||
UI64 uint64
|
UI64 uint64
|
||||||
|
BI *big.Int
|
||||||
S string
|
S string
|
||||||
Sp *string
|
Sp *string
|
||||||
Sa []string
|
Sa []string
|
||||||
|
@ -113,6 +113,7 @@ func TestConversion(t *testing.T) {
|
||||||
3,
|
3,
|
||||||
4,
|
4,
|
||||||
5,
|
5,
|
||||||
|
big.NewInt(6),
|
||||||
"a",
|
"a",
|
||||||
&bString,
|
&bString,
|
||||||
[]string{"c", "d", "e"},
|
[]string{"c", "d", "e"},
|
||||||
|
@ -153,7 +154,7 @@ func TestConversion(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// test all formats (complex)
|
// test all formats (complex)
|
||||||
formats := []uint8{JSON}
|
formats := []uint8{JSON, CBOR}
|
||||||
|
|
||||||
for _, format := range formats {
|
for _, format := range formats {
|
||||||
|
|
||||||
|
@ -217,6 +218,9 @@ func TestConversion(t *testing.T) {
|
||||||
if complexSubject.UI64 != co.UI64 {
|
if complexSubject.UI64 != co.UI64 {
|
||||||
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64)
|
t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64)
|
||||||
}
|
}
|
||||||
|
if complexSubject.BI.Cmp(co.BI) != 0 {
|
||||||
|
t.Errorf("Load (complex struct): struct.BI is not equal (%v != %v)", complexSubject.BI, co.BI)
|
||||||
|
}
|
||||||
if complexSubject.S != co.S {
|
if complexSubject.S != co.S {
|
||||||
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S)
|
t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S)
|
||||||
}
|
}
|
||||||
|
@ -251,7 +255,7 @@ func TestConversion(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// test all formats
|
// test all formats
|
||||||
formats = []uint8{JSON, GenCode}
|
formats = []uint8{JSON, CBOR, GenCode}
|
||||||
|
|
||||||
for _, format := range formats {
|
for _, format := range formats {
|
||||||
// simple
|
// simple
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/dgraph-io/badger v1.6.2
|
github.com/dgraph-io/badger v1.6.2
|
||||||
github.com/dgraph-io/ristretto v0.1.0 // indirect
|
github.com/dgraph-io/ristretto v0.1.0 // indirect
|
||||||
|
github.com/fxamacker/cbor/v2 v2.3.0
|
||||||
github.com/go-ole/go-ole v1.2.5 // indirect
|
github.com/go-ole/go-ole v1.2.5 // indirect
|
||||||
github.com/gofrs/uuid v4.0.0+incompatible
|
github.com/gofrs/uuid v4.0.0+incompatible
|
||||||
github.com/golang/glog v1.0.0 // indirect
|
github.com/golang/glog v1.0.0 // indirect
|
||||||
|
|
3
go.sum
3
go.sum
|
@ -79,6 +79,9 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
|
||||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
|
github.com/fxamacker/cbor v1.5.1 h1:XjQWBgdmQyqimslUh5r4tUGmoqzHmBFQOImkWGi2awg=
|
||||||
|
github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik=
|
||||||
|
github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
|
||||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||||
|
|
|
@ -43,6 +43,7 @@ func (m *Module) NewErrorMessage(taskName string, err error) *ModuleError {
|
||||||
return &ModuleError{
|
return &ModuleError{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
ModuleName: m.Name,
|
ModuleName: m.Name,
|
||||||
|
TaskName: taskName,
|
||||||
Severity: "error",
|
Severity: "error",
|
||||||
StackTrace: string(debug.Stack()),
|
StackTrace: string(debug.Stack()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,7 @@ func prepareModules() error {
|
||||||
if rep.err == ErrCleanExit {
|
if rep.err == ErrCleanExit {
|
||||||
return rep.err
|
return rep.err
|
||||||
}
|
}
|
||||||
|
rep.module.NewErrorMessage("prep module", rep.err).Report()
|
||||||
return fmt.Errorf("failed to prep module %s: %s", rep.module.Name, rep.err)
|
return fmt.Errorf("failed to prep module %s: %s", rep.module.Name, rep.err)
|
||||||
}
|
}
|
||||||
reportCnt++
|
reportCnt++
|
||||||
|
@ -198,6 +199,7 @@ func startModules() error {
|
||||||
// wait for reports
|
// wait for reports
|
||||||
rep = <-reports
|
rep = <-reports
|
||||||
if rep.err != nil {
|
if rep.err != nil {
|
||||||
|
rep.module.NewErrorMessage("start module", rep.err).Report()
|
||||||
return fmt.Errorf("modules: could not start module %s: %s", rep.module.Name, rep.err)
|
return fmt.Errorf("modules: could not start module %s: %s", rep.module.Name, rep.err)
|
||||||
}
|
}
|
||||||
reportCnt++
|
reportCnt++
|
||||||
|
|
|
@ -106,6 +106,7 @@ func stopModules() error {
|
||||||
rep = <-reports
|
rep = <-reports
|
||||||
if rep.err != nil {
|
if rep.err != nil {
|
||||||
lastErr = rep.err
|
lastErr = rep.err
|
||||||
|
rep.module.NewErrorMessage("stop module", rep.err).Report()
|
||||||
log.Warningf("modules: could not stop module %s: %s", rep.module.Name, rep.err)
|
log.Warningf("modules: could not stop module %s: %s", rep.module.Name, rep.err)
|
||||||
}
|
}
|
||||||
reportCnt++
|
reportCnt++
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/info"
|
"github.com/safing/portbase/info"
|
||||||
|
@ -155,7 +156,7 @@ func (di *Info) AddLastReportedModuleError() {
|
||||||
}
|
}
|
||||||
|
|
||||||
di.AddSection(
|
di.AddSection(
|
||||||
"Module Error",
|
fmt.Sprintf("%s Module Error", strings.Title(me.ModuleName)),
|
||||||
UseCodeSection,
|
UseCodeSection,
|
||||||
me.Format(),
|
me.Format(),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue