forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
100 lines (84 loc) · 1.57 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package cmd
import (
"encoding/json"
"errors"
)
type Class int
//go:generate enumer -type Class -trimprefix Class -transform=lower -text
const (
_ Class = iota
ClassConfigFile
ClassMeter
ClassCharger
ClassVehicle
ClassTariff
ClassCircuit
ClassSite
ClassMqtt
ClassDatabase
ClassModbusProxy
ClassEEBus
ClassJavascript
ClassGo
ClassHEMS
ClassInflux
ClassMessenger
ClassSponsorship
)
// FatalError is an error that can be marshaled
type FatalError struct {
err error
}
func (e *FatalError) Error() string {
return e.err.Error()
}
func (e FatalError) MarshalJSON() ([]byte, error) {
if je, ok := e.err.(json.Marshaler); ok {
return je.MarshalJSON()
}
return json.Marshal(struct {
Error string `json:"error"`
}{
Error: e.err.Error(),
})
}
// DeviceError indicates the specific device that failed
type DeviceError struct {
Name string
err error
}
func (e *DeviceError) Error() string {
return e.err.Error()
}
// ClassError indicates the class of devices that failed
type ClassError struct {
Class Class
err error
}
func (e *ClassError) Error() string {
return e.err.Error()
}
func (e ClassError) MarshalJSON() ([]byte, error) {
res := struct {
Class string `json:"class"`
Device string `json:"device,omitempty"`
Error string `json:"error"`
}{
Class: e.Class.String(),
Error: e.err.Error(),
}
var de *DeviceError
if errors.As(e.err, &de) {
res.Device = de.Name
}
return json.Marshal(res)
}
func wrapErrorWithClass(class Class, err error) error {
if err == nil {
return nil
}
return &ClassError{
Class: class,
err: err,
}
}