Skip to content

Commit

Permalink
make the errors be golint friendly from the start
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Jul 6, 2018
1 parent 6b301d7 commit da4ffbe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
30 changes: 9 additions & 21 deletions maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ and a word that just doesn't have a definition.
## Refactor

```go
var NotFoundError = errors.New("could not find the word you were looking for")
var ErrNotFound = errors.New("could not find the word you were looking for")

func (d Dictionary) Search(word string) (string, error) {
definition, ok := d[word]
if !ok {
return "", NotFoundError
return "", ErrNotFound
}

return definition, nil
Expand All @@ -242,7 +242,7 @@ into a variable. This will also allow us to have a better test.
t.Run("unknown word", func(t *testing.T) {
_, got := dictionary.Search("unknown")

assertError(t, got, NotFoundError)
assertError(t, got, ErrNotFound)
})

func assertError(t *testing.T, got, want error) {
Expand All @@ -255,7 +255,7 @@ func assertError(t *testing.T, got, want error) {
```

By creating a new helper we were able to simplify our test, and start using
our `NotFoundError` variable so our test doesn't fail if we change the error
our `ErrNotFound` variable so our test doesn't fail if we change the error
text in the future.

## Write the test first
Expand Down Expand Up @@ -435,7 +435,7 @@ In `dictionary.go`

```go
var (
NotFoundError = errors.New("could not find the word you were looking for")
ErrNotFound = errors.New("could not find the word you were looking for")
WordExistsError = errors.New("cannot add word because it already exists")
)

Expand All @@ -460,7 +460,7 @@ returning a `nil` error.
func (d Dictionary) Add(word, definition string) error {
_, err := d.Search(word)
switch err {
case NotFoundError:
case ErrNotFound:
d[word] = definition
case nil:
return WordExistsError
Expand All @@ -473,9 +473,7 @@ func (d Dictionary) Add(word, definition string) error {
}
```

Here we are using a `switch` statement to match on the error. Having a `switch`
like this provides an extra safety net, in case `Search`returns an error other
than `NotFoundError`.
Here we are using a `switch` statement to match on the error. Having a `switch` like this provides an extra safety net, in case `Search` returns an error other than `ErrNotFound`.

## Refactor

Expand All @@ -495,18 +493,8 @@ func (e DictionaryErr) Error() string {
}
```

The first thing you will notice, is we made the errors constant. This required
us to create our own `DictionaryErr` type which implements the `error`
interface. You can read more about
the details in [this excellent article by Dave
Cheney](https://dave.cheney.net/2016/04/07/constant-errors). Simply put, it
makes the errors more reusable and immutable.

We also changed the names of the errors to make them IDE friendly.
If you have auto completion enabled it's nice to be able to see all your
errors by typing `Err`. You can perform this change manually or try out
[gorename](https://godoc.org/golang.org/x/tools/refactor/rename), which is
a great refactoring tool!
We made the errors constant; this required us to create our own `DictionaryErr` type which implements the `error` interface. You can read more about the details in [this excellent article by Dave
Cheney](https://dave.cheney.net/2016/04/07/constant-errors). Simply put, it makes the errors more reusable and immutable.

## Write the test first

Expand Down
6 changes: 3 additions & 3 deletions maps/v2/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "errors"
// Dictionary store definitions to words
type Dictionary map[string]string

// NotFoundError means the definition could not be found for the given word
var NotFoundError = errors.New("could not find the word you were looking for")
// ErrNotFound means the definition could not be found for the given word
var ErrNotFound = errors.New("could not find the word you were looking for")

// Search find a word in the dictionary
func (d Dictionary) Search(word string) (string, error) {
definition, ok := d[word]
if !ok {
return "", NotFoundError
return "", ErrNotFound
}

return definition, nil
Expand Down
6 changes: 3 additions & 3 deletions maps/v3/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "errors"
// Dictionary store definitions to words
type Dictionary map[string]string

// NotFoundError means the definition could not be found for the given word
var NotFoundError = errors.New("could not find the word you were looking for")
// ErrNotFound means the definition could not be found for the given word
var ErrNotFound = errors.New("could not find the word you were looking for")

// Search find a word in the dictionary
func (d Dictionary) Search(word string) (string, error) {
definition, ok := d[word]
if !ok {
return "", NotFoundError
return "", ErrNotFound
}

return definition, nil
Expand Down

0 comments on commit da4ffbe

Please sign in to comment.