Skip to content

feat(common): migrate Guard to LuYao.Common with tests and docs#119

Merged
Soar360 merged 4 commits into
mainfrom
feat/guard-common-migration
Jun 26, 2026
Merged

feat(common): migrate Guard to LuYao.Common with tests and docs#119
Soar360 merged 4 commits into
mainfrom
feat/guard-common-migration

Conversation

@Soar360

@Soar360 Soar360 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces a new utility class Guard to the LuYao.Common library, 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:

  • Added static class Guard with methods TryRun, TryRunAsync, TryGet, and TryGetAsync for 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:

  • Added GuardTests unit test class to verify all major behaviors of the new Guard methods, including successful execution, exception handling, fallback values, filtering, and critical exception bypass. (tests/LuYao.Common.UnitTests/GuardTests.cs)

Copilot AI review requested due to automatic review settings June 26, 2026 12:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Guard static helper with TryRun/TryRunAsync and TryGet/TryGetAsync APIs plus “critical exception” bypass logic.
  • Added GuardTests covering 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 thread src/LuYao.Common/Guard.cs
Comment thread src/LuYao.Common/Guard.cs
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 thread src/LuYao.Common/Guard.cs
Comment thread src/LuYao.Common/Guard.cs
Comment thread src/LuYao.Common/Guard.cs
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);
}

Soar360 and others added 3 commits June 26, 2026 20:15
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 方法由单行实现重构为标准多行大括号格式,提升代码可读性和一致性,未更改方法逻辑。
@Soar360 Soar360 merged commit e697ddd into main Jun 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants