This repository has been archived by the owner on Mar 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
147 lines (121 loc) · 3.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package log
import (
"encoding/json"
"reflect"
"github.com/branch-app/branch-mono-go/helpers"
"github.com/imdario/mergo"
)
// E implements the official Branch Error structure
type E struct {
Code string `json:"code"`
Meta M `json:"meta,omitempty"`
Reasons []E `json:"reasons,omitempty"`
}
// M is an alias type for map[string]interface{}
type M map[string]interface{}
// Error returns the code of the Branch Error.
func (e E) Error() string {
return e.Code
}
func newBranchError(code string, meta M, reasons []interface{}) *E {
err := &E{
Code: code,
Meta: meta,
Reasons: make([]E, len(reasons)),
}
for i, r := range reasons {
err.Reasons[i] = Coerce(r)
}
return err
}
// Coerce attempts to coerce a Branch Error out of any object.
// - `E` types are just returned as-is
// - strings are json unmarshalled into a map[string]interface{} and recursive
// - bytes are parsed into a string and recursive
// - other primitives are parsed into
func Coerce(v interface{}) E {
if v != nil {
// If the interface is a pointer, make it not so
rv := reflect.ValueOf(v)
rt := reflect.TypeOf(v)
if rv.Kind() == reflect.Ptr {
return Coerce(reflect.Indirect(rv).Interface())
}
// If the interface is an empty array, todo
if (rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array) && rv.Len() == 0 {
return E{
Code: "unknown",
Meta: M{
"data": "empty_array",
},
}
}
// Check if interface is a E
if err, ok := v.(E); ok {
return err
}
// Check if interface is a go error
if _, ok := reflect.Zero(rt).Interface().(error); ok {
e, _ := rv.Interface().(error)
return E{Code: helpers.StrToSnakeTrimmed(e.Error(), 35)}
}
// Check if interface is a string
if str, ok := v.(string); ok {
return Coerce([]byte(str))
}
// Check if interface is a byte array/slice
if bytes, ok := v.([]byte); ok {
var d M
if json.Unmarshal(bytes, &d) == nil {
cErr := E{}
if code, ok := d["code"].(string); ok {
cErr.Code = helpers.StrToSnakeTrimmed(code, 35)
} else {
cErr.Code = "unknown"
}
if reasons, ok := d["reasons"].([]E); ok {
cErr.Reasons = reasons
}
if meta, ok := d["meta"].(map[string]interface{}); ok {
cErr.Meta = M(meta)
} else {
cErr.Meta = d
}
// Check if we need to remove the `code` from meta.
if _, ok := cErr.Meta["code"]; ok {
delete(cErr.Meta, "code")
}
return cErr
}
// The body isn't a valid json object - take the first 150 bytes and log
if len(bytes) > 150 {
bytes = bytes[0:150]
}
return E{
Code: "unknown",
Meta: M{"data": string(bytes)},
}
}
// this should never happen
return E{
Code: "unable_to_coerce_error",
Meta: M{
"type": rt,
"value": rv,
},
}
}
// this should never happen
return E{
Code: "error_was_nil",
Meta: M{
"info": "🤔",
},
}
}
// CoerceWithMeta runs Coerce on v, and then merges the metadata into the error response.
func CoerceWithMeta(v interface{}, meta M) E {
err := Coerce(v)
mergo.Merge(&err.Meta, meta)
return err
}