-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
54 lines (42 loc) · 833 Bytes
/
errors.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
package validate
import (
"encoding/json"
"errors"
)
var ErrInvalidID = errors.New("id is not in its proper form")
type ErrorResponse struct {
Error string `json:"error"`
Fields string `json:"fields,omitempty"`
}
type RequestError struct {
Err error
Status int
Fields error
}
func NewRequestError(err error, status int) error {
return &RequestError{err, status, nil}
}
func (e *RequestError) Error() string {
return e.Err.Error()
}
type FieldError struct {
Field string `json:"field"`
Error string `json:"error"`
}
type FieldErrors []FieldError
func (fe *FieldErrors) Error() string {
d, err := json.Marshal(fe)
if err != nil {
return err.Error()
}
return string(d)
}
func Cause(err error) error {
root := err
for {
if err = errors.Unwrap(root); err == nil {
return root
}
root = err
}
}