Skip to content
Snippets Groups Projects
Commit 22d978fe authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Added readme and test for nexted Is()

parent 34f2caf5
Branches
Tags
No related merge requests found
......@@ -175,3 +175,9 @@ This function can be used also for logging to determine the caller from the runt
## func Stack()
This function is similar to Caller() but reports an array of callers, not only one.
## func Is()
You can compare a message with errors.Is() to some error specification.
It will look at the error or any cause to match the spec.
The spec is the error message.
\ No newline at end of file
......@@ -109,6 +109,23 @@ func TestIs(t *testing.T) {
t.Logf("n=%d worked", n)
}
}
//same but err is wrapped deeper
for n := 0; n <= 4; n++ {
if err := UserExists(n); err != nil {
switch {
case errors.Is(err, errNotFound):
t.Logf("u=%d failed with NOT FOUND", n)
case errors.Is(err, errDisabled):
t.Logf("u=%d failed because disabled", n)
default:
t.Logf("u=%d failed for unknown cause: %+v", n, err)
}
} else {
t.Logf("u=%d worked", n)
}
}
}
var (
......@@ -130,3 +147,10 @@ func ReadDb(x int) error {
return errors.Errorf("invalid x=%d", x)
}
}
func UserExists(u int) error {
if err := ReadDb(u); err != nil {
return errors.Wrapf(err, "cannot read user %d", u)
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment