-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
196 lines (162 loc) · 5.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2015-2025 Bleemeo
//
// bleemeo.com an infrastructure monitoring solution in the Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bleemeo
import (
"errors"
"fmt"
"reflect"
"time"
)
const errorRespMaxLength = 1 << 20 // 1MB
var (
// ErrTokenIsRefreshOnly is returned when trying to request a new token,
// but credentials haven't been provided.
ErrTokenIsRefreshOnly = errors.New("the OAuth token can only be refreshed")
// errTokenHasNoRefresh is returned when the OAuth access token has no associated refresh token.
errTokenHasNoRefresh = errors.New("the OAuth token has no refresh")
// ErrNoAuthMeanProvided is returned when the client has no way to retrieve an OAuth token.
ErrNoAuthMeanProvided = errors.New("no authentication mean provided")
// ErrTokenRevoke is returned when the logout operation has not been completed successfully.
ErrTokenRevoke = errors.New("failed to revoke token")
// ErrResourceNotFound is returned when the resource with the specified ID doesn't exist (HTTP status 404).
ErrResourceNotFound = errors.New("resource not found")
)
// JSONErrorDataKind indicates the type of data whose conversion failed.
type JSONErrorDataKind int
//nolint: revive,stylecheck,gofmt,gofumpt,goimports
const (
JsonErrorDataKind_400Details JSONErrorDataKind = iota
JsonErrorDataKind_401Details
JsonErrorDataKind_404Details
JsonErrorDataKind_ResultPage
JsonErrorDataKind_RequestBody
)
func (kind JSONErrorDataKind) String() string {
switch kind {
case JsonErrorDataKind_400Details:
return "400 details"
case JsonErrorDataKind_401Details:
return "401 details"
case JsonErrorDataKind_404Details:
return "404 details"
case JsonErrorDataKind_ResultPage:
return "result page"
case JsonErrorDataKind_RequestBody:
return "request body"
default:
return fmt.Sprintf("unknown JsonErrorDataKind %d", kind)
}
}
// APIError represents an error returned by the Bleemeo API,
// when the StatusCode is in the 4xx or 5xx range.
type APIError struct {
ReqPath string
StatusCode int
ContentType string
Message string
Err error
// The first MB of the response, if any.
Response []byte
}
func (apiErr *APIError) Error() string {
var errStr string
if apiErr.StatusCode != 0 {
errStr += fmt.Sprint(apiErr.StatusCode, " - ")
}
if apiErr.Message != "" {
errStr += apiErr.Message
}
if apiErr.Err != nil {
errStr += " (" + apiErr.Err.Error() + ")"
}
return errStr
}
func (apiErr *APIError) Unwrap() error {
return apiErr.Err
}
// An AuthError holds an error due to unspecified or invalid credentials.
type AuthError struct {
*APIError
// ErrorCode is RFC 6749's 'error' parameter.
ErrorCode string
}
func (authErr *AuthError) Error() string {
if authErr.Err != nil {
return "authentication error: " + authErr.Err.Error()
}
return "authentication error: " + authErr.APIError.Error()
}
func (authErr *AuthError) Unwrap() error {
return authErr.APIError
}
// A ThrottleError is returned when the API received too much requests from the client.
type ThrottleError struct {
*APIError
Delay time.Duration
}
type jsonError struct {
Err error
DataKind JSONErrorDataKind
Data any
}
func (jsonErr *jsonError) Error() string {
return jsonErr.DataKind.String() + ": " + jsonErr.Err.Error()
}
func (jsonErr *jsonError) Unwrap() error {
return jsonErr.Err
}
func (jsonErr *jsonError) Is(other error) bool {
if err := new(jsonError); errors.As(other, &err) {
if err.DataKind != jsonErr.DataKind || !reflect.DeepEqual(err.Data, jsonErr.Data) {
return false
}
if errors.Is(err.Err, jsonErr.Err) {
return true
}
return reflect.DeepEqual(err.Err, jsonErr.Err)
}
return false
}
// JSONMarshalError represents an error that occurred
// during the serialization of data to JSON.
type JSONMarshalError struct {
*jsonError
}
func (marshalErr *JSONMarshalError) Error() string {
return "marshalling " + marshalErr.jsonError.Error()
}
// Is returns whether the given error is the same as this.
func (marshalErr *JSONMarshalError) Is(other error) bool {
if err := new(JSONMarshalError); errors.As(other, &err) {
return marshalErr.jsonError.Is(err.jsonError)
}
return false
}
// JSONUnmarshalError represents an error that occurred
// during the deserialization of data from JSON.
type JSONUnmarshalError struct {
*jsonError
}
func (unmarshalErr *JSONUnmarshalError) Error() string {
return "unmarshalling " + unmarshalErr.jsonError.Error()
}
// Is returns whether the given error is the same as this.
func (unmarshalErr *JSONUnmarshalError) Is(other error) bool {
if err := new(JSONUnmarshalError); errors.As(other, &err) {
return unmarshalErr.jsonError.Is(err.jsonError)
}
return false
}