-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
72 lines (60 loc) · 2.19 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
package raiderio
import (
"errors"
"strings"
)
// Errors that the api produces
var (
ErrInvalidRegion = errors.New("invalid region")
ErrInvalidRealm = errors.New("invalid realm")
ErrInvalidCharName = errors.New("invalid character name")
ErrInvalidGuildName = errors.New("invalid guild name")
ErrInvalidRaidName = errors.New("invalid raid name")
ErrInvalidRaidDiff = errors.New("invalid raid difficulty")
ErrInvalidRaid = errors.New("invalid raid")
ErrFieldMissing = errors.New("field missing from api response")
ErrCharacterNotFound = errors.New("character not found")
ErrGuildNotFound = errors.New("guild not found")
ErrUnsupportedExpac = errors.New("unsupported expansion")
ErrLimitOutOfBounds = errors.New("limit must be a positive int")
ErrPageOutOfBounds = errors.New("page must be a positive int")
ErrInvalidBoss = errors.New("invalid boss")
ErrInvalidQuery = errors.New("invalid query")
ErrApiTimeout = errors.New("raiderio api request timeout")
ErrUnexpected = errors.New("unexpected error")
)
// Turns api errors into standardized go errors with
// consistent error messages
func wrapApiError(responseBody *apiErrorResponse) error {
if strings.Contains(responseBody.Message, "Failed to find region") {
return ErrInvalidRegion
}
if strings.Contains(responseBody.Message, "Failed to find realm") {
return ErrInvalidRealm
}
if strings.Contains(responseBody.Message, "Failed to find raid") {
return ErrInvalidRaid
}
if strings.Contains(responseBody.Message, "Failed to find boss") {
return ErrInvalidBoss
}
if strings.Contains(responseBody.Message, "Could not find requested character") {
return ErrCharacterNotFound
}
if strings.Contains(responseBody.Message, "Could not find requested guild") {
return ErrGuildNotFound
}
if strings.Contains(responseBody.Message, "Requested unsupported expansion_id") {
return ErrUnsupportedExpac
}
if strings.Contains(responseBody.Message, "Could not find requested raid") {
return ErrInvalidRaid
}
return ErrUnexpected
}
func wrapHttpError(err error) error {
if strings.Contains(err.Error(), "context deadline exceeded") {
return ErrApiTimeout
}
return ErrUnexpected
}