Skip to content

Commit 89fee89

Browse files
committedMay 25, 2022
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. ... }
1 parent 8c6de56 commit 89fee89

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed
 

‎error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func (err *Error) Get(k byte) (v string) {
449449
return ""
450450
}
451451

452-
func (err Error) Error() string {
452+
func (err *Error) Error() string {
453453
return "pq: " + err.Message
454454
}
455455

0 commit comments

Comments
 (0)
Please sign in to comment.