-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
77 lines (63 loc) · 1.47 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
package domain
import (
"encoding/json"
"net/http"
)
type Error interface {
error
Domain() string
Aggregate() string
}
type domainError struct {
code int
message string
domain string
aggregate string
metadata map[string]interface{}
}
func (e *domainError) MarshalJSON() ([]byte, error) {
return json.Marshal(e.message)
}
func (e *domainError) Error() string {
return e.message
}
// StatusCode should return the http status code used to determine the error
func (e *domainError) StatusCode() int {
return e.code
}
func (e *domainError) Domain() string {
return e.domain
}
func (e *domainError) Aggregate() string {
return e.aggregate
}
func (e *domainError) Metadata() map[string]interface{} {
return e.metadata
}
func (e *domainError) With(key string, value interface{}) Error {
metadata := make(map[string]interface{}, len(e.metadata)+1)
for k, v := range e.metadata {
metadata[k] = v
}
metadata[key] = value
return &domainError{
code: e.code,
message: e.message,
domain: e.domain,
aggregate: e.aggregate,
metadata: metadata,
}
}
func NewError(message string, domain string) Error {
return NewErrorWithCode(message, domain, http.StatusBadRequest)
}
func NewErrorWithCode(message string, domain string, code int) Error {
return &domainError{
code: code,
message: message,
domain: domain,
}
}
func NewNotFoundError(message string, domain string) Error {
return NewErrorWithCode(message, domain, http.StatusNotFound)
}