-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
41 lines (35 loc) · 935 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
package types
import (
"fmt"
"github.com/go-logfmt/logfmt"
)
// Error returned by the API.
type Error struct {
Msg string `json:"error"`
Detail *string `json:"detail,omitempty"`
Index *uint `json:"index,omitempty"`
QuotaLimit *uint `json:"quotaLimit,omitempty"`
QuotaRemaining *uint `json:"quotaRemaining,omitempty"`
}
func (e *Error) Error() string {
keyvals := []any{}
if e.Detail != nil {
keyvals = append(keyvals, "detail", *e.Detail)
}
if e.Index != nil {
keyvals = append(keyvals, "index", *e.Index)
}
if e.QuotaLimit != nil {
keyvals = append(keyvals, "quota_limit", *e.QuotaLimit)
}
if e.QuotaRemaining != nil {
keyvals = append(keyvals, "quota_remaining", *e.QuotaRemaining)
}
if len(keyvals) != 0 {
extra, err := logfmt.MarshalKeyvals(keyvals...)
if err == nil && len(extra) != 0 {
return fmt.Sprintf("%s: %s", e.Msg, extra)
}
}
return e.Msg
}