forked from shkh/lastfm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
55 lines (46 loc) · 1.12 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
package lastfm_go
import (
"fmt"
"reflect"
"strings"
)
const (
ApiResponseStatusOk = "ok"
ApiResponseStatusFailed = "failed"
)
const (
ErrorAuthRequired = 50
ErrorParameterMissing = 51
ErrorInvalidTypeOfArgument = 52
)
var Messages = map[int]string{
50: "This method requires authentication.",
51: "Required parameter missing. Required: %v, Missing: %v.",
52: "Invalid type of argument passed. Supported types are int, string and []string.",
}
type LastfmError struct {
Code int
Message string
Where string
Caller string
}
func (e *LastfmError) Error() string {
return fmt.Sprintf("LastfmError[%d]: %s (%s)", e.Code, e.Message, e.Caller)
}
func newApiError(errorXml *ApiError) (e *LastfmError) {
e = new(LastfmError)
e.Code = errorXml.Code
e.Message = strings.TrimSpace(errorXml.Message)
return e
}
func newLibError(code int, message string) (e *LastfmError) {
e = new(LastfmError)
e.Code = code
e.Message = message
return e
}
func appendCaller(err error, caller string) {
if err != nil && reflect.TypeOf(err).String() == "*lastfm.LastfmError" {
err.(*LastfmError).Caller = caller
}
}