-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
61 lines (46 loc) · 994 Bytes
/
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
package chirp
import (
"fmt"
"reflect"
)
// Error of parsing field
type Error struct {
part string
field reflect.StructField
source string
cause error
}
// Error implements error interface
func (err Error) Error() string {
return fmt.Sprintf("%s[%s](%s): %s", err.part, err.Tag(), err.source, err.cause)
}
// Part of request with error (path | query | body)
func (err Error) Part() string {
return err.part
}
// Field return field reflector
func (err Error) Field() reflect.StructField {
return err.field
}
// Tag name or field name
//
// If error has been occurred on json unmarshaling it return "*"
func (err Error) Tag() string {
if len(err.field.Name) == 0 {
return "*"
}
part := err.part
if part == "body" {
part = "json"
}
tag, _ := tagOrFieldName(err.field, part)
return tag
}
// Source of parse
func (err Error) Source() string {
return err.source
}
// Cause implement errors.Causer interface
func (err Error) Cause() error {
return err.cause
}