Skip to content

Commit

Permalink
Deliver an error type.
Browse files Browse the repository at this point in the history
The error type was provided to remove all the mutable error variables.
  • Loading branch information
faabiosr committed Mar 8, 2020
1 parent a503f29 commit c10a733
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions errors.go
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)
}
25 changes: 25 additions & 0 deletions errors_test.go
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")
}
}

0 comments on commit c10a733

Please sign in to comment.