From 89fee896440712bc328c9087ea185c3990053f31 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 25 May 2022 11:53:23 -0700 Subject: [PATCH] Use pointer receiver on pq.Error.Error() The library returns *pq.Error and not pq.Error. By using a value receiver, the library was documenting that consumers should expect returned error values to contain pq.Error. While *pq.Error implements all methods on pq.Error, *pq.Error is not assignable to pq.Error and so you can't type assert an error value into pq.Error if it actually contains *pq.Error. In particular, this is a problem with errors.As. The following if condition will always return false. var pqe pq.Error if errors.As(err, &pqe) { // Never reached as *pq.Error is not assignable to pqe. ... } --- error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error.go b/error.go index 21b3d933..f67c5a5f 100644 --- a/error.go +++ b/error.go @@ -449,7 +449,7 @@ func (err *Error) Get(k byte) (v string) { return "" } -func (err Error) Error() string { +func (err *Error) Error() string { return "pq: " + err.Message }