-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
126 lines (104 loc) · 3.37 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package climacell
import (
"errors"
"fmt"
)
var (
ErrInvalidBaseURL = errors.New("invalid base URL provided")
ErrInvalidAPIKey = errors.New("invalid api key provided")
ErrInvalidLatitude = errors.New("invalid latitude provided")
ErrInvalidLongitude = errors.New("invalid longitude provided")
)
// HTTPError represents an error that was returned from the climacell API
type HTTPError struct {
Endpoint string `json:"-"`
Message string `json:"message"`
}
// Error returns the string representation of the HTTPError
func (e *HTTPError) Error() string {
return fmt.Sprintf("error %v: %v", e.Endpoint, e.Message)
}
// BadRequestError represents an HTTP error with status code 400 - bad request
type BadRequestError struct {
HTTPError
}
// Error returns the string representation of the BadRequestError
func (e *BadRequestError) Error() string {
return fmt.Sprintf("bad request %v: %v", e.Endpoint, e.Message)
}
// UnauthorizedError represents an HTTP error with status code 401 - unauthorized
type UnauthorizedError struct {
HTTPError
}
// Error returns the string representation of the UnauthorizedError
func (e *UnauthorizedError) Error() string {
return fmt.Sprintf("unauthorized %v: %v", e.Endpoint, e.Message)
}
// ForbiddenError represents an HTTP error with status code 403 - forbidden
type ForbiddenError struct {
HTTPError
}
// Error returns the string representation of the ForbiddenError
func (e *ForbiddenError) Error() string {
return fmt.Sprintf("forbidden %v: %v", e.Endpoint, e.Message)
}
// TooManyRequestsError represents an HTTP error with status code 429 - too many requests
type TooManyRequestsError struct {
HTTPError
}
// Error returns the string representation of the TooManyRequestsError
func (e *TooManyRequestsError) Error() string {
return fmt.Sprintf("too many requests %v: %v", e.Endpoint, e.Message)
}
// InternalServerError represents an HTTP error with status code 500 - internal server error
type InternalServerError struct {
HTTPError
}
// Error returns the string representation of the InternalServerError
func (e *InternalServerError) Error() string {
return fmt.Sprintf("internal server error %v: %v", e.Endpoint, e.Message)
}
// NotFoundError represents an HTTP error with status code 404 - not found
type NotFoundError struct {
HTTPError
}
// Error returns the string representation of the NotFoundError
func (e *NotFoundError) Error() string {
return fmt.Sprintf("not found error %v: %v", e.Endpoint, e.Message)
}
func newBadRequestError(endpoint, message string) *BadRequestError {
return &BadRequestError{HTTPError{
Endpoint: endpoint,
Message: message,
}}
}
func newUnauthorizedError(endpoint, message string) *UnauthorizedError {
return &UnauthorizedError{HTTPError{
Endpoint: endpoint,
Message: message,
}}
}
func newForbiddenError(endpoint, message string) *ForbiddenError {
return &ForbiddenError{HTTPError{
Endpoint: endpoint,
Message: message,
}}
}
func newTooManyRequestError(endpoint, message string) *TooManyRequestsError {
return &TooManyRequestsError{HTTPError{
Endpoint: endpoint,
Message: message,
}}
}
func newInternalServerError(endpoint, message string) *InternalServerError {
return &InternalServerError{HTTPError{
Endpoint: endpoint,
Message: message,
}}
}
func newNotFoundError(endpoint, message string) *NotFoundError {
return &NotFoundError{HTTPError{
Endpoint: endpoint,
Message: message,
}}
}