-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dex: general purpose unwrapable Error
- Loading branch information
Showing
9 changed files
with
98 additions
and
19 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
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
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,39 @@ | ||
// This code is available on the terms of the project LICENSE.md file, | ||
// also available online at https://blueoakcouncil.org/license/1.0.0. | ||
|
||
package dex | ||
|
||
// ErrorKind identifies a kind of error that can be used to define new errors | ||
// via const SomeError = dex.ErrorKind("something"). | ||
type ErrorKind string | ||
|
||
// Error satisfies the error interface and prints human-readable errors. | ||
func (e ErrorKind) Error() string { | ||
return string(e) | ||
} | ||
|
||
// Error pairs an error with details. | ||
type Error struct { | ||
wrapped error | ||
detail string | ||
} | ||
|
||
// Error satisfies the error interface, combining the wrapped error message with | ||
// the details. | ||
func (e Error) Error() string { | ||
return e.wrapped.Error() + ": " + e.detail | ||
} | ||
|
||
// Unwrap returns the wrapped error, allowing errors.Is and errors.As to work. | ||
func (e Error) Unwrap() error { | ||
return e.wrapped | ||
} | ||
|
||
// NewError wraps the provided Error with details in a Error, facilitating the | ||
// use of errors.Is and errors.As via errors.Unwrap. | ||
func NewError(err error, detail string) Error { | ||
return Error{ | ||
wrapped: err, | ||
detail: detail, | ||
} | ||
} |
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,45 @@ | ||
package dex | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
type testErr string | ||
|
||
func (te testErr) Error() string { | ||
return string(te) | ||
} | ||
|
||
const ( | ||
err0 = testErr("other error") | ||
err1 = ErrorKind("error 1") | ||
err2 = ErrorKind("error 2") | ||
) | ||
|
||
func TestDexError(t *testing.T) { | ||
|
||
dexErr := NewError(err1, "stuff") | ||
|
||
var err1Back ErrorKind | ||
if !errors.As(dexErr, &err1Back) { | ||
t.Errorf("it wasn't an ErrorKind") | ||
} | ||
wantErrStr := err1.Error() + ": " + "stuff" | ||
if dexErr.Error() != wantErrStr { | ||
t.Errorf("incorrect error string %v, want %v", dexErr.Error(), wantErrStr) | ||
} | ||
|
||
if !errors.Is(dexErr, err1) { | ||
t.Errorf("it wasn't err1") | ||
} | ||
if errors.Is(dexErr, err2) { | ||
t.Errorf("it was err2") | ||
} | ||
|
||
otherErr := NewError(err0, "other stuff") | ||
var err0Back testErr | ||
if !errors.As(otherErr, &err0Back) { | ||
t.Errorf("it wasn't an testErr") | ||
} | ||
} |
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
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
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
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
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