generated from keboola/template
/
encryption_error.go
66 lines (57 loc) · 1.69 KB
/
encryption_error.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
// nolint: dupl
package keboola
import (
"fmt"
"net/http"
)
// EncryptionError represents the structure Encryption API error.
type EncryptionError struct {
Message string `json:"error"`
ErrCode int `json:"code"`
ExceptionID string `json:"exceptionId"`
request *http.Request
response *http.Response
}
func (e *EncryptionError) Error() string {
msg := e.Message
if e.request != nil {
msg += fmt.Sprintf(`, method: "%s", url: "%s"`, e.request.Method, e.request.URL)
}
if e.response != nil {
msg += fmt.Sprintf(`, httpCode: "%d"`, e.StatusCode())
}
if e.ErrCode > 0 {
msg += fmt.Sprintf(`, errCode: "%d"`, e.ErrCode)
}
if len(e.ExceptionID) > 0 {
msg += fmt.Sprintf(`, exceptionId: "%s"`, e.ExceptionID)
}
return msg
}
// ErrorName returns a human-readable name of the error.
func (e *EncryptionError) ErrorName() string {
return http.StatusText(e.ErrCode)
}
// ErrorUserMessage returns error message for end user.
func (e *EncryptionError) ErrorUserMessage() string {
return e.Message
}
// ErrorExceptionID returns exception ID to find details in logs.
func (e *EncryptionError) ErrorExceptionID() string {
return e.ExceptionID
}
// StatusCode returns HTTP status code.
func (e *EncryptionError) StatusCode() int {
if e.response == nil {
return 0
}
return e.response.StatusCode
}
// SetRequest method allows injection of HTTP request to the error, it implements client.errorWithRequest.
func (e *EncryptionError) SetRequest(request *http.Request) {
e.request = request
}
// SetResponse method allows injection of HTTP response to the error, it implements client.errorWithResponse.
func (e *EncryptionError) SetResponse(response *http.Response) {
e.response = response
}