feat(common): migrate Guard to LuYao.Common with tests and docs#119
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a new Guard utility to LuYao.Common for guarded execution of sync/async delegates (with optional exception filtering, callbacks, and fallback values) and introduces a corresponding MSTest test suite to validate core behaviors.
Changes:
- Added
Guardstatic helper withTryRun/TryRunAsyncandTryGet/TryGetAsyncAPIs plus “critical exception” bypass logic. - Added
GuardTestscovering success paths, handled exceptions, filtering, rethrow, cancellation bypass, and fallback behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/LuYao.Common/Guard.cs | Introduces guarded execution APIs for sync/async delegates with configurable handling behavior. |
| tests/LuYao.Common.UnitTests/GuardTests.cs | Adds unit tests for key Guard behaviors (success, fallback, cancellation bypass, filtering/rethrow). |
Comment on lines
+43
to
+46
| public static async Task<bool> TryRunAsync(Func<Task> action, Action<Exception>? onError = null, Func<Exception, bool>? when = null, bool rethrow = false) | ||
| { | ||
| try | ||
| { |
Comment on lines
+107
to
+112
| private static bool CanHandle(Exception ex, Func<Exception, bool>? when) | ||
| { | ||
| if (IsCritical(ex)) return false; | ||
| if (when == null) return true; | ||
| return when(ex); | ||
| } |
Comment on lines
+49
to
+56
| [TestMethod] | ||
| public void TryGet_WhenFuncThrows_ReturnsFallback() | ||
| { | ||
| int value = Guard.TryGet(() => throw new InvalidOperationException("boom"), fallback: 42); | ||
|
|
||
| Assert.AreEqual(42, value); | ||
| } | ||
|
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
将 Guard.cs 中 TryRun、TryGet、TryGetAsync 方法由单行实现重构为标准多行大括号格式,提升代码可读性和一致性,未更改方法逻辑。
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a new utility class
Guardto theLuYao.Commonlibrary, providing a set of helper methods for guarded execution of synchronous and asynchronous code blocks. These helpers reduce repetitive try/catch boilerplate, allow for custom exception handling logic, and support fallback values. Comprehensive unit tests are also included to verify the behavior of these new methods.Exception handling utilities:
Guardwith methodsTryRun,TryRunAsync,TryGet, andTryGetAsyncfor executing actions/functions with configurable exception handling, fallback values, and optional rethrow/filter logic. Critical exceptions are not handled by design. (src/LuYao.Common/Guard.cs)Testing:
GuardTestsunit test class to verify all major behaviors of the newGuardmethods, including successful execution, exception handling, fallback values, filtering, and critical exception bypass. (tests/LuYao.Common.UnitTests/GuardTests.cs)