generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
81 lines (70 loc) · 1.66 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
package vocode
import (
"encoding/json"
"fmt"
)
type APIError struct {
ParamError *APIParamError
GenError *APIGenError
UnexpecedError json.RawMessage
}
func (e *APIError) Error() string {
if e.ParamError != nil {
return e.ParamError.Error()
}
if e.GenError != nil {
return e.GenError.Error()
}
if len(e.UnexpecedError) > 0 {
return string(e.UnexpecedError)
}
return "unknown error"
}
func (e *APIError) UnmarshalJSON(data []byte) error {
var paramError APIParamError
err := json.Unmarshal(data, ¶mError)
if err == nil && len(paramError.Detail) > 0 {
e.ParamError = ¶mError
return nil
}
var genError APIGenError
err = json.Unmarshal(data, &genError)
if err == nil && genError.Detail != "" {
e.GenError = &genError
return nil
}
e.UnexpecedError = data
return fmt.Errorf("unexpected API error JSON: %s", string(data))
}
// APIParamError is returned when API params are invalid.
type APIParamError struct {
Detail []struct {
Type string `json:"type"`
Loc []interface{} `json:"loc"`
Msg string `json:"msg"`
Input string `json:"input,omitempty"`
Ctx *struct {
Error string `json:"error"`
} `json:"ctx,omitempty"`
} `json:"detail"`
}
// Error implements error interface.
func (e APIParamError) Error() string {
b, err := json.Marshal(e)
if err != nil {
return "unknown error"
}
return string(b)
}
// APIGenError is returned when a generic API error is returned.
type APIGenError struct {
Detail string `json:"detail"`
}
// Error implements error interface.
func (e APIGenError) Error() string {
b, err := json.Marshal(e)
if err != nil {
return "unknown error"
}
return string(b)
}