add validation error type, fix tests, add number tests
This commit is contained in:
@@ -36,7 +36,7 @@ type Error struct {
|
|||||||
Data map[string]interface{} `json:"data,omitempty"` // Optional additional data.
|
Data map[string]interface{} `json:"data,omitempty"` // Optional additional data.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error retrieves the message of a REST API Error.
|
// Error retrieves the message of a REST API error.
|
||||||
// If it has a "error" string attached using WithData or WithError, that message is returned.
|
// If it has a "error" string attached using WithData or WithError, that message is returned.
|
||||||
// Otherwise, the Error's own message is returned.
|
// Otherwise, the Error's own message is returned.
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
@@ -54,14 +54,10 @@ func (e Error) Error() string {
|
|||||||
// If the target is a REST API error and specifies a status code, this function returns true if the status codes match.
|
// If the target is a REST API error and specifies a status code, this function returns true if the status codes match.
|
||||||
// If the target is an empty REST API error, this function always returns true.
|
// If the target is an empty REST API error, this function always returns true.
|
||||||
func (e Error) Is(target error) bool {
|
func (e Error) Is(target error) bool {
|
||||||
t, ok := target.(Error)
|
if t, ok := target.(Error); ok {
|
||||||
if !ok {
|
return t.StatusCode == e.StatusCode || t.StatusCode == 0
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
|
||||||
if t.StatusCode == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return t.StatusCode == e.StatusCode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithData returns a copy of the HTTP error with the given data merged in.
|
// WithData returns a copy of the HTTP error with the given data merged in.
|
||||||
|
|||||||
+2
-2
@@ -132,7 +132,7 @@ func TestErrorWrite(t *testing.T) {
|
|||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
_, err := tc.Input.Write(rec)
|
_, err := tc.Input.Write(rec)
|
||||||
if err != tc.Err {
|
if !errors.Is(err, tc.Err) {
|
||||||
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -163,7 +163,7 @@ func TestErrorWriteJSON(t *testing.T) {
|
|||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
err := tc.Input.WriteJSON(rec)
|
err := tc.Input.WriteJSON(rec)
|
||||||
if err != tc.Err {
|
if !errors.Is(err, tc.Err) {
|
||||||
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user