-
Notifications
You must be signed in to change notification settings - Fork 12
/
error.go
119 lines (102 loc) · 3.08 KB
/
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
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
package core
import (
"encoding/json"
"fmt"
)
const (
// ResponseReadError occurs when error happened while we tried to
// read the response.
ResponseReadError = "Error reading body"
// ResponseReadBodyError occurs when error happened while we tried to
// read the body of the response.
ResponseReadBodyError = "Error reading body"
// ErrorResponseParseError occurs when the response was an error,
// but something went wrong with parsing it as an Error.
ErrorResponseParseError = "Error response parse error"
// UndefinedRequiredField occurs when a mandatory field is not defined
// in a request.
UndefinedRequiredField = "Undefined required field"
// OutOfRangeError is a template error where a given value
// has to be in a given range.
OutOfRangeError = "Out of range [%d..%d]"
// ExceedMaximumLengthError occurs when a parameter is longer
// than expected on an endpoint.
ExceedMaximumLengthError = "Value is too long"
)
// NewRangeError generates an error message for an OutOfRangeError.
func NewRangeError(from, to int64) string {
return fmt.Sprintf(OutOfRangeError, from, to)
}
// RequestError happens when something went wrong with the request.
type RequestError struct {
Message string
Origin error
}
func (e RequestError) Error() string {
return fmt.Sprintf("%s: %s", e.Message, e.Origin.Error())
}
// EndpointNotFound happens when the requested endpoint returs with 404 error code.
type EndpointNotFound struct {
Endpoint string
}
func (e EndpointNotFound) Error() string {
return fmt.Sprintf("404 - Not found: %s", e.Endpoint)
}
// ErrorResponseWrapper is the wrapper for error responses.
type ErrorResponseWrapper struct {
Error json.RawMessage `json:"error"`
}
// UnknownError occurs when we couldn't determine the source of the error.
type UnknownError struct {
Response ErrorResponse
}
func (e UnknownError) Error() string {
return fmt.Sprintf("<%s> %s -> %s", e.Response.Code, e.Response.Info.Param, e.Response.Info.Reason)
}
// InvalidFieldReferenceError occurs when we couldn't determine the source of the error.
type InvalidFieldReferenceError struct {
Name string
Type string
Reference string
}
func (e InvalidFieldReferenceError) Error() string {
return fmt.Sprintf(
"%s refers to %s as %s, but %s is not defined",
e.Name,
e.Reference,
e.Type,
e.Reference,
)
}
// RequestValidationError occurs when one or more
// mandatory fields are missing.
type RequestValidationError struct {
Request BaseRequest
Message string
Field string
}
// FieldError is the detailed error on a given field in a request.
type FieldError struct {
Name string
Issue string
}
func (e RequestValidationError) Error() string {
return fmt.Sprintf(
"%T request validation failed: [%s] %s",
e.Request,
e.Field,
e.Message,
)
}
// NotImplementedYet is an error for endpoint without implementation.
// The error will contain a reason for that, for example
// we don't know what is the response structure yet.
type NotImplementedYet struct {
Reason string
}
func (e NotImplementedYet) Error() string {
return fmt.Sprintf(
"Not implemented yet, reason: %s",
e.Reason,
)
}