Skip to content

Commit

Permalink
fix the incorrect order of explanation of the .Error() method (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz authored May 13, 2024
1 parent 822e707 commit a817a93
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ func TestSearch(t *testing.T) {

The way to handle this scenario in Go is to return a second argument which is an `Error` type.

`Error`s can be converted to a string with the `.Error()` method, which we do when passing it to the assertion. We are also protecting `assertStrings` with `if` to ensure we don't call `.Error()` on `nil`.
Notice that as we've seen in the [pointers and error section](./pointers-and-errors.md) here in order to asset the error message
we first check that the error is not `nil` and then use `.Error()` method to get the string which we can then pass to the assertion.

## Try and run the test

Expand Down
3 changes: 3 additions & 0 deletions pointers-and-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ Update our helper for a `string` to compare against.
```go
assertError := func(t testing.TB, got error, want string) {
t.Helper()

if got == nil {
t.Fatal("didn't get an error but wanted one")
}
Expand All @@ -465,6 +466,8 @@ assertError := func(t testing.TB, got error, want string) {
}
```

As you can see `Error`s can be converted to a string with the `.Error()` method, which we do in order to compare it with the string we want. We are also making sure that the error is not `nil` to ensure we don't call `.Error()` on `nil`.

And then update the caller

```go
Expand Down

0 comments on commit a817a93

Please sign in to comment.