-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror.go
More file actions
52 lines (45 loc) · 901 Bytes
/
error.go
File metadata and controls
52 lines (45 loc) · 901 Bytes
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
package main
import (
"encoding/json"
"errors"
"log"
)
type ErrCode int
const (
ERR_NONE ErrCode = 1 + iota
ERR_KEY_UNKNOWN = iota
ERR_ENCRYPTION = iota
ERR_TASK_INVALID = iota
ERR_NOT_ALLOWED = iota
ERR_OTHER_UNRECOVERABLE = iota
ERR_OTHER_RECOVERABLE = iota
)
type MyError struct {
Error error
Code ErrCode
}
func (me MyError) MarshalJSON() ([]byte, error) {
return json.Marshal(
struct {
Error string
Code ErrCode
}{
Error: me.Error.Error(),
Code: me.Code,
})
}
func (me *MyError) UnmarshalJSON(data []byte) error {
var s struct {
Error string
Code ErrCode
}
err := json.Unmarshal(data, &s)
me.Error = errors.New(s.Error)
me.Code = s.Code
return err
}
func FailOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}