vspd/types/types_test.go
Jamie Holdstock 6d872db60e
Rename APIError to ErrorResponse. (#360)
Last commit before tagging v1.0.0 of the types module. The name ErrorResponse is consistent with all other response types.
2022-11-22 17:09:06 +08:00

54 lines
1.4 KiB
Go

// Copyright (c) 2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package types
import (
"errors"
"testing"
)
// TestAPIErrorAs ensures APIError can be unwrapped via errors.As.
func TestAPIErrorAs(t *testing.T) {
tests := []struct {
testName string
apiError error
expectedKind ErrorCode
expectedMessage string
}{{
testName: "BadRequest error",
apiError: ErrorResponse{Message: "something went wrong", Code: int64(ErrBadRequest)},
expectedKind: ErrBadRequest,
expectedMessage: "something went wrong",
},
{
testName: "Unknown error",
apiError: ErrorResponse{Message: "something went wrong again", Code: int64(999)},
expectedKind: 999,
expectedMessage: "something went wrong again",
}}
for _, test := range tests {
// Ensure APIError can be unwrapped from error.
var parsedError ErrorResponse
if !errors.As(test.apiError, &parsedError) {
t.Errorf("%s: unable to unwrap error", test.testName)
continue
}
if parsedError.Code != int64(test.expectedKind) {
t.Errorf("%s: error was wrong kind. expected: %d actual %d",
test.testName, test.expectedKind, parsedError.Code)
continue
}
if parsedError.Message != test.expectedMessage {
t.Errorf("%s: error had wrong message. expected: %q actual %q",
test.testName, test.expectedMessage, parsedError.Message)
continue
}
}
}