From 6d7679da9c22c38c3840d9b9e254d0de534188cf Mon Sep 17 00:00:00 2001 From: Jameel Al-Aziz Date: Mon, 13 Nov 2023 16:06:02 -0800 Subject: [PATCH] fix potential panic in IsRecoverable The way `unrecoverableError` wraps an error can lead to potential panics. In `IsRecoverable`, an `unrecoverableError` is passed to `errors.Is` without an embedded error. This is problematic because any error type that implements the `Is` interface and expects to be able to call `Error()` will trigger a panic due to the embedded error being `nil`. To fix this, we can implement `Error()` on `unrecoverableError` and handle the case where the embedded error is nil. --- retry.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/retry.go b/retry.go index b5d49d5..56bc9dd 100644 --- a/retry.go +++ b/retry.go @@ -302,6 +302,13 @@ type unrecoverableError struct { error } +func (e unrecoverableError) Error() string { + if e.error == nil { + return "unrecoverable error" + } + return e.error.Error() +} + func (e unrecoverableError) Unwrap() error { return e.error }