Serge Bazanski | 1c2bc12 | 2021-05-23 18:23:47 +0200 | [diff] [blame] | 1 | package ident |
| 2 | |
| 3 | // IdentError is an ErrorResponse received from a ident server, wrapped as a Go |
| 4 | // error type. |
| 5 | // When using errors.Is/errors.As against an IdentError, the Inner field |
| 6 | // controls the matching behaviour. |
| 7 | // - If set, the error will match if the tested error is an IdentError with |
| 8 | // same ErrorResponse as the IdentError tested against |
| 9 | // - If not set, the error will always match if the tested error is any |
| 10 | // IdentError. |
| 11 | // |
| 12 | // For example: |
| 13 | // errors.Is(err, &IdentError{} |
| 14 | // will be true if err is any *IdentError, but: |
| 15 | // errors.Is(err, &IdentError{NoUser}) |
| 16 | // will be true only if err is an *IdentError with an Inner NoUser. |
| 17 | type IdentError struct { |
| 18 | // Inner is the ErrorResponse contained by this error. |
| 19 | Inner ErrorResponse |
| 20 | } |
| 21 | |
| 22 | func (e *IdentError) Error() string { |
| 23 | return string(e.Inner) |
| 24 | } |
| 25 | |
| 26 | func (e *IdentError) Is(target error) bool { |
| 27 | t, ok := target.(*IdentError) |
| 28 | if !ok { |
| 29 | return false |
| 30 | } |
| 31 | if t.Inner == "" { |
| 32 | return true |
| 33 | } |
| 34 | if t.Inner == e.Inner { |
| 35 | return true |
| 36 | } |
| 37 | return false |
| 38 | } |