Skip to content

Commit

Permalink
doc: update error-compare and define unwrapped-error
Browse files Browse the repository at this point in the history
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
  • Loading branch information
mmorel-35 and ccoVeille committed Sep 4, 2024
1 parent 72f0b19 commit 4cb531e
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Describe a new checker in [checkers section](./README.md#checkers).
- [require-len](#require-len)
- [suite-test-name](#suite-test-name)
- [useless-assert](#useless-assert)

- [unwrapped-error](#unwrapped-error)
---

### elements-match
Expand All @@ -169,22 +169,41 @@ Describe a new checker in [checkers section](./README.md#checkers).

---

### unwrapped-error

```go
❌ assert.Contains(t, err.Error(), "not found")
assert.Equal(t, err.Error(), "user not found")

✅ assert.ErrorContains(t, err, "not found")
assert.EqualError(t, err, "user not found")
assert.ErrorIs(t, err, ErrUserNotFound)
assert.NotErrorIs(t, err, errSentinel)
```

**Autofix**: true. <br>
**Enabled by default**: true. <br>
**Reason**: Using testify methods will avoid the risk of nil pointer exception that might happen with the use of the `Error()` method on the `error` interface. <br>
**Related issues**: [#47](https://github.com/Antonboom/testifylint/issues/47)

---

### error-compare

```go
❌ assert.ErrorContains(t, err, "not found")
assert.EqualError(t, err, "user not found")
assert.Equal(t, err.Error(), "user not found")
assert.Equal(t, err, errSentinel) // Through `reflect.DeepEqual` causes error strings to be compared.
assert.NotEqual(t, err, errSentinel)
// etc.

✅ assert.ErrorIs(t, err, ErrUserNotFound)
assert.ErrorAs(t, err, &target)
```

**Autofix**: false. <br>
**Enabled by default**: true. <br>
**Reason**: The `Error()` method on the `error` interface exists for humans, not code. <br>
**Reason**: The `assert.ErrorContains()` is commonly used as an anti-pattern. The code and tests often needs to be adapted to be able to use `assert.ErrorIs()` or `assert.ErrorAs()` methods. <br>
**Related issues**: [#47](https://github.com/Antonboom/testifylint/issues/47)

---
Expand Down

0 comments on commit 4cb531e

Please sign in to comment.