-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
60 lines (49 loc) · 1.64 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
package httpclient
import (
"fmt"
"net/http"
)
// Constants for error management
const (
MsgErrCannotObtain = "cannotObtain"
MsgErrCannotGetIssuer = "cannotGetIssuer"
MsgErrCannotParse = "cannotParse"
MsgErrUnkownHTTPContentType = "unkownHTTPContentType"
MsgErrUnknownResponseStatusCode = "unknownResponseStatusCode"
PrmTokenProviderURL = "tokenProviderURL"
PrmAPIURL = "APIURL"
PrmTokenMsg = "token"
PrmResponse = "response"
)
// HTTPError is returned when an error occured while contacting the keycloak instance.
type HTTPError struct {
StatusCode int
Message string
}
func (e HTTPError) Error() string {
return fmt.Sprintf("%d:%s", e.StatusCode, e.Message)
}
// IsSuccess is true when HTTP status code is 2xx or 3xx
func (e HTTPError) IsSuccess() bool {
return e.StatusCode < http.StatusBadRequest
}
// IsError is true when HTTP request failed
func (e HTTPError) IsError() bool {
return e.StatusCode >= http.StatusBadRequest
}
// IsErrorFromClient is true when HTTP request failed and the cause was related to the client request
func (e HTTPError) IsErrorFromClient() bool {
return e.StatusCode >= http.StatusBadRequest && e.StatusCode < http.StatusInternalServerError
}
// IsErrorFromServer is true when HTTP request failed and the cause was related to the HTTP server
func (e HTTPError) IsErrorFromServer() bool {
return e.StatusCode >= http.StatusInternalServerError
}
// Status returns the HTTP status code
func (e HTTPError) Status() int {
return e.StatusCode
}
// ErrorMessage returns the HTTP error message
func (e HTTPError) ErrorMessage() string {
return e.Message
}