-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The error type was provided to remove all the mutable error variables.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cachego | ||
|
||
import "fmt" | ||
|
||
type err string | ||
|
||
// Error returns the string error value. | ||
func (e err) Error() string { | ||
return string(e) | ||
} | ||
|
||
const ( | ||
// ErrCacheExpired returns an error when the cache key was expired. | ||
ErrCacheExpired = err("cache expired") | ||
|
||
// ErrFlush returns an error when flush fails. | ||
ErrFlush = err("unable to flush") | ||
|
||
// ErrSave returns an error when save fails. | ||
ErrSave = err("unable to save") | ||
|
||
// ErrDelete returns an error when deletion fails. | ||
ErrDelete = err("unable to delete") | ||
|
||
// ErrDecode returns an errors when decode fails. | ||
ErrDecode = err("unable to decode") | ||
) | ||
|
||
func Wrap(err, additionalErr error) error { | ||
return fmt.Errorf("%s: %w", additionalErr, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cachego | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestError(t *testing.T) { | ||
expect := "failed" | ||
er := err(expect) | ||
|
||
if r := fmt.Sprint(er); r != expect { | ||
t.Errorf("invalid string: expect %s, got %s", expect, r) | ||
} | ||
} | ||
|
||
func TestWrap(t *testing.T) { | ||
additionalErr := errors.New("failed") | ||
err := Wrap(ErrSave, additionalErr) | ||
|
||
if !errors.Is(err, ErrSave) { | ||
t.Errorf("wrap failed: expected true") | ||
} | ||
} |