forked from hostwithquantum/golexoffice
/
errors.go
110 lines (97 loc) · 2.65 KB
/
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
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
package golexoffice
import "strings"
// source: https://developers.lexoffice.io/docs/#error-codes-legacy-error-response
// files, profile, contacts
//
// {
// "requestId":"3fb21ee4-ad26-4e2f-82af-a1197af02d08",
// "IssueList":[
// {"i18nKey":"invalid_value","source":"company and person","type":"validation_failure"},
// {"i18nKey":"missing_entity","source":"company.name","type":"validation_failure"}
// ]
// }
type LegacyErrorResponse struct {
RequestID string `json:"requestId"`
IssueList []struct {
Key string `json:"i18nKey"`
Source string `json:"source"`
Type string `json:"type"`
} `json:"IssueList"`
}
func (e LegacyErrorResponse) Error() string {
return e.String()
}
func (e LegacyErrorResponse) String() string {
builder := &strings.Builder{}
for i, issue := range e.IssueList {
builder.WriteString(issue.Key)
builder.WriteString(": ")
builder.WriteString(issue.Source)
if issue.Type != "" {
builder.WriteString(" (")
builder.WriteString(issue.Type)
builder.WriteString(")")
}
if i < len(e.IssueList)-1 {
builder.WriteString(", ")
}
}
return builder.String()
}
// source: https://developers.lexoffice.io/docs/#error-codes-regular-error-response
// event-subscription, invoices
//
// {
// "timestamp": "2017-05-11T17:12:31.233+02:00",
// "status": 406,
// "error": "Not Acceptable",
// "path": "/v1/invoices",
// "traceId": "90d78d0777be",
// "message": "Validation failed for request. Please see details list for specific causes.",
// "details": [
// {
// "violation": "NOTNULL",
// "field": "lineItems[0].unitPrice.taxRatePercentage",
// "message": "darf nicht leer sein"
// }
// ]
// }
type ErrorResponse struct {
Timestamp Date `json:"timestamp"`
Status int `json:"status"`
ErrorString string `json:"error"`
Path string `json:"path"`
TraceID string `json:"traceId"`
Message string `json:"message"`
Details []struct {
Violation string `json:"violation"`
Field string `json:"field"`
Message string `json:"message"`
} `json:"details"`
}
func (e ErrorResponse) Error() string {
return e.String()
}
func (e ErrorResponse) String() string {
builder := &strings.Builder{}
builder.WriteString(e.Message)
if len(e.Details) == 0 {
return builder.String()
}
builder.WriteString(" (")
for i, detail := range e.Details {
builder.WriteString(detail.Field)
builder.WriteString(": ")
builder.WriteString(detail.Message)
if detail.Violation != "" {
builder.WriteString(" (")
builder.WriteString(detail.Violation)
builder.WriteString(")")
}
if i < len(e.Details)-1 {
builder.WriteString(", ")
}
}
builder.WriteString(")")
return builder.String()
}