forked from luno/luno-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
39 lines (31 loc) · 793 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
package luno
import (
"errors"
"fmt"
)
// Error is a Luno API error.
type Error struct {
// Code can be used to identify errors even if the error message is
// localised.
Code string `json:"error_code"`
// Message may be localised for authenticated API calls.
Message string `json:"error"`
}
func (e Error) Error() string {
return fmt.Sprintf("%s (%s)", e.Message, e.Code)
}
func (e Error) ErrCode() string {
return e.Code
}
// IsErrorCode returns whether an error is identifiable by a given code. This can be used to handle luno.Client errors.
// Any other errors will cause this to return false.
func IsErrorCode(err error, code string) bool {
if err == nil {
return false
}
var lErr Error
if errors.As(err, &lErr) {
return lErr.ErrCode() == code
}
return false
}