From 2eab5507af17bc4cc1b00906fb8d5b982862c47b Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 8 Aug 2024 11:53:39 +0200 Subject: [PATCH] Retry every github.com/lib/pq database error, including "pq: ..." Some errors created within github.com/lib/pq are created with fmt.Errorf, prefixed with "pq: ". Thus, they are not of the *pq.Error type, but are clearly database errors. A change request was created upstream in: https://github.com/lib/pq/issues/1169 Co-Authored-By: Alvar Penning --- retry/retry.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/retry/retry.go b/retry/retry.go index 0686b0fb..fc1648cf 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" "io" "net" + "strings" "syscall" "time" ) @@ -197,5 +198,12 @@ func Retryable(err error) bool { return true } + // For errors without a five-digit code, github.com/lib/pq uses fmt.Errorf(). + // This returns an unexported error type prefixed with "pq: " + // Until this gets changed upstream , we can only check the error message. + if strings.HasPrefix(err.Error(), "pq: ") { + return true + } + return false }