cockroachdb / errors

Go error library with error portability over the network
Apache License 2.0
2.07k stars 66 forks source link

markers: delegate to an Is() method if present #59

Closed knz closed 3 years ago

knz commented 3 years ago

Fixes #58

The Go std errors package has this rule about Is():

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

(source)

This patch extends the errors library accordingly.

Caveat: the doc says "if a.Is(b) is true, then errors.Is(a, b) is true". This is the behavior implemented here. The doc does not say "if a.Is(b) is false, then errors.Is(a, b) is false".

The CockroachDB errors library uses error identity markers which enable comparisons across the network. The identity marker includes only the type and message. Therefore, it is not possible to "make two errors unequal" by customizing an Is() method to return false, if their network identity marker is otherwise equal. To change this behavior, use the errors.Mark() API instead, or implement an ErrorTypeKey() method.

Requested by @romariolopezc in #58


This change is Reviewable