Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Into and Must generic helpers
- Loading branch information
Showing
5 changed files
with
79 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,20 @@ | ||
package errors_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-faster/errors" | ||
) | ||
|
||
func ExampleInto() { | ||
_, err := os.Open("non-existing") | ||
if err != nil { | ||
if pathError, ok := errors.Into[*os.PathError](err); ok { | ||
fmt.Println("Failed at path:", pathError.Path) | ||
} | ||
} | ||
|
||
// Output: | ||
// Failed at path: non-existing | ||
} |
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,16 @@ | ||
package errors_test | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/go-faster/errors" | ||
) | ||
|
||
func ExampleMust() { | ||
r := errors.Must(url.Parse(`https://google.com`)) | ||
fmt.Println(r.String()) | ||
|
||
// Output: | ||
// https://google.com | ||
} |
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,9 @@ | ||
package errors | ||
|
||
// Into finds the first error in err's chain that matches target type T, and if so, returns it. | ||
// | ||
// Into is type-safe alternative to As. | ||
func Into[T error](err error) (val T, ok bool) { | ||
ok = As(err, &val) | ||
return val, ok | ||
} |
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,10 @@ | ||
package errors | ||
|
||
// Must is a generic helper, like template.Must, that wraps a call to a function returning (T, error) | ||
// and panics if the error is non-nil. | ||
func Must[T any](val T, err error) T { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return val | ||
} |
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,24 @@ | ||
package errors | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMust(t *testing.T) { | ||
if got := Must(10, nil); got != 10 { | ||
t.Fatalf("Expected %+v, got %+v", 10, got) | ||
} | ||
|
||
panics := func() (r bool) { | ||
defer func() { | ||
if recover() != nil { | ||
r = true | ||
} | ||
}() | ||
Must(10, New("test error")) | ||
return r | ||
}() | ||
if !panics { | ||
t.Fatal("Panic expected") | ||
} | ||
} |