-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
84 lines (69 loc) · 2.18 KB
/
Copy patherrors.go
File metadata and controls
84 lines (69 loc) · 2.18 KB
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
package errors
import "fmt"
type errorCategory string
const (
ErrCategoryGeneric = errorCategory("GENERIC")
ErrCategoryDataAccess = errorCategory("DATA_ACCESS")
)
type errorCodeGeneric string
func (c errorCodeGeneric) code() string { return string(c) }
func (c errorCodeGeneric) String() string { return c.code() }
const (
ErrCodeGenericAlreadyExists = errorCodeGeneric("ALREADY_EXISTS")
ErrCodeGenericInvalidArgument = errorCodeGeneric("INVALID_ARGUMENT")
ErrCodeGenericInternal = errorCodeGeneric("INTERNAL")
ErrCodeGenericNotFound = errorCodeGeneric("NOT_FOUND")
)
type errorCodeDataAccess string
func (c errorCodeDataAccess) code() string { return string(c) }
func (c errorCodeDataAccess) String() string { return c.code() }
const (
ErrCodeDataAccessInsertFailed = errorCodeDataAccess("INSERT_FAILED")
ErrCodeDataAccessSelectFailed = errorCodeDataAccess("SELECT_FAILED")
ErrCodeDataAccessDeleteFailed = errorCodeDataAccess("DELETE_FAILED")
ErrCodeDataAccessUpdateFailed = errorCodeDataAccess("UPDATE_FAILED")
)
type Error struct {
Category errorCategory `json:"category"`
Code Code `json:"code"`
Message string `json:"message"`
Detail string `json:"detail"`
Extra map[string]interface{} `json:"extra"`
}
func (e Error) Error() string {
return fmt.Sprintf(e.Message)
}
type Code interface {
code() string
String() string
}
func Generic(code errorCodeGeneric, msg string, detail string, extra ...map[string]interface{}) Error {
return Error{
Category: ErrCategoryGeneric,
Code: code,
Message: msg,
Detail: detail,
Extra: mergeMaps(extra...),
}
}
func DataAccess(code errorCodeDataAccess, msg string, detail string, extra ...map[string]interface{}) Error {
return Error{
Category: ErrCategoryDataAccess,
Code: code,
Message: msg,
Detail: detail,
Extra: mergeMaps(extra...),
}
}
func mergeMaps(extras ...map[string]interface{}) map[string]interface{} {
var merged map[string]interface{}
if len(extras) != 0 {
merged = make(map[string]interface{})
for _, extra := range extras {
for k, v := range extra {
merged[k] = v
}
}
}
return merged
}