forked from jianyuan/go-sentry
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
50 lines (43 loc) · 892 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
package sentry
import (
"encoding/json"
"fmt"
)
// APIError represents a Sentry API Error response.
// Should look like:
//
// type apiError struct {
// Detail string `json:"detail"`
// }
type APIError struct {
f interface{} // unknown
}
func (e *APIError) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &e.f); err != nil {
e.f = string(b)
}
return nil
}
func (e *APIError) MarshalJSON() ([]byte, error) {
return json.Marshal(e.f)
}
func (e APIError) Detail() string {
switch v := e.f.(type) {
case map[string]interface{}:
if len(v) == 1 {
if detail, ok := v["detail"].(string); ok {
return detail
}
}
return fmt.Sprintf("%v", v)
default:
return fmt.Sprintf("%v", v)
}
}
func (e APIError) Error() string {
return fmt.Sprintf("sentry: %s", e.Detail())
}
// Empty returns true if empty.
func (e APIError) Empty() bool {
return e.f == nil
}