-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util/must: add
Handle
for convenient failure handling
This patch adds `Handle()`, a convenience function that avoids returning assertion errors when running many assertions. Instead, non-fatal assertion failures throw panics that are caught by `Handle()` and converted to errors. Example usage: ```go // nolint:errcheck err := must.Handle(ctx, func(ctx context.Context) { must.True(ctx, true, "not true") must.Equal(ctx, 1, 1, "not equal") }) ``` Inside a `Handle` closure, must assertions never return errors, so they can be ignored. However, the errcheck linter will complain about this and must be disabled. Initially, an attempt was made to pass in a struct with infallible assertion methods, for a safer API. Unfortunately, methods can't use generics, nor can function types, so this appears to be one of the only ways to achieve this. Epic: none Release note: None
- Loading branch information
1 parent
0a1699d
commit 40a3971
Showing
2 changed files
with
54 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
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