Skip to content

Commit

Permalink
Use pointer receiver on pq.Error.Error()
Browse files Browse the repository at this point in the history
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.
      ...
    }
  • Loading branch information
nhooyr committed May 25, 2022
1 parent 8c6de56 commit 89fee89
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 89fee89

Please sign in to comment.