-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Open
Description
The current implementation of the assertError function doesn't handle all error cases safely. Specifically, if want is nil, calling want.Error() will result in a panic. This becomes problematic now that we’ve added custom errors like ErrNotFound and ErrWordExists.
Current implementation:
func assertError(t testing.TB, got, want error) {
t.Helper()
if got != want {
t.Errorf("got error %q want %q", got, want)
}
}
Suggestion:
func assertError(t testing.TB, got, want error) {
t.Helper()
switch {
case got != nil && want == nil:
t.Fatalf("got unexpected error: %q", got)
case got == nil && want != nil:
t.Fatalf("expected error %q but got none", want)
case got != nil && got.Error() != want.Error():
t.Errorf("got error %q, want %q", got, want)
}
}
Metadata
Metadata
Assignees
Labels
No labels