-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherror.go
49 lines (41 loc) · 1.04 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
package bunq
import (
"encoding/json"
"fmt"
"io"
"strings"
)
// Error represents an error returned by the bunq API.
type Error struct {
ErrorDescription string
ErrorDescriptionTranslated string
}
// Errors is an array of Error structs.
type Errors []Error
func (e Errors) Error() string {
errs := make([]string, len(e))
for i, err := range e {
errs[i] = err.ErrorDescription
}
return strings.Join(errs, ", ")
}
func decodeError(r io.Reader) error {
var apiError struct {
Error []struct {
ErrorDescription string `json:"error_description"`
ErrorDescriptionTranslated string `json:"error_description_translated"`
} `json:"Error"`
}
err := json.NewDecoder(r).Decode(&apiError)
if err != nil {
return fmt.Errorf("could not decode errors from json: %v", err)
}
var errors Errors
for i := range apiError.Error {
errors = append(errors, Error{
ErrorDescription: apiError.Error[i].ErrorDescription,
ErrorDescriptionTranslated: apiError.Error[i].ErrorDescriptionTranslated,
})
}
return errors
}