-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patherror.go
44 lines (34 loc) · 837 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
package getstream
import (
"time"
)
// Credits to https://github.com/hyperworks/go-getstream for the error handling.
// Error is a getstream error
type Error struct {
Code int `json:"code"`
StatusCode int `json:"status_code"`
Detail string `json:"detail"`
RawDuration string `json:"duration"`
Exception string `json:"exception"`
}
var _ error = &Error{}
// Duration is the time it took for the request to be handled
func (e *Error) Duration() time.Duration {
result, err := time.ParseDuration(e.RawDuration)
if err != nil {
return time.Duration(0)
}
return result
}
func (e *Error) Error() string {
str := e.Exception
if e.RawDuration != "" {
if duration := e.Duration(); duration > 0 {
str += " (" + duration.String() + ")"
}
}
if e.Detail != "" {
str += ": " + e.Detail
}
return str
}