vspd/types/types_test.go
jholdstock 6d78e16b23 types: Use ErrorCode in ErrorResponse.
Using ErrorCode instead of int64 in the ErrorResponse type removes the burden of type conversion from calling code.
2023-03-16 16:07:38 +00:00

54 lines
1.4 KiB
Go

// Copyright (c) 2022-2023 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 := map[string]struct {
apiError error
expectedKind ErrorCode
expectedMessage string
}{
"BadRequest error": {
apiError: ErrorResponse{Message: "something went wrong", Code: ErrBadRequest},
expectedKind: ErrBadRequest,
expectedMessage: "something went wrong",
},
"Unknown error": {
apiError: ErrorResponse{Message: "something went wrong again", Code: 999},
expectedKind: 999,
expectedMessage: "something went wrong again",
},
}
for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
// Ensure APIError can be unwrapped from error.
var parsedError ErrorResponse
if !errors.As(test.apiError, &parsedError) {
t.Fatalf("unable to unwrap error")
}
if parsedError.Code != test.expectedKind {
t.Fatalf("error was wrong kind. expected: %d actual %d",
test.expectedKind, parsedError.Code)
}
if parsedError.Message != test.expectedMessage {
t.Fatalf("error had wrong message. expected: %q actual %q",
test.expectedMessage, parsedError.Message)
}
})
}
}