-
Notifications
You must be signed in to change notification settings - Fork 0
Frends.JSON.Query: fix compliance findings (net8, static class, CancellationToken, error handling) #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-compliance-findings-frends-json-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Frends.JSON.Query: fix compliance findings (net8, static class, CancellationToken, error handling) #40
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b36045a
Initial plan
Copilot 8d2cbce
Fix Frends.JSON.Query compliance findings: net8, static class, Cancel…
Copilot 2b6c720
Refactor error handling: extract ErrorHandler helper, simplify Result…
Copilot 4c0380d
Add ErrorHandlerTests.cs; remove inline error-handler test from UnitT…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
49 changes: 49 additions & 0 deletions
49
Frends.JSON.Query/Frends.JSON.Query.UnitTests/ErrorHandlerTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using Frends.JSON.Query.Definitions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Threading; | ||
|
|
||
| namespace Frends.JSON.Query.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class ErrorHandlerTests | ||
| { | ||
| private const string CustomErrorMessage = "CustomErrorMessage"; | ||
|
|
||
| [TestMethod] | ||
| public void Should_Throw_Error_When_ThrowErrorOnFailure_Is_True() | ||
| { | ||
| var ex = Assert.ThrowsException<Exception>(() => | ||
| JSON.Query(DefaultInput(), DefaultOptions(), CancellationToken.None)); | ||
| Assert.IsNotNull(ex); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Should_Return_Failed_Result_When_ThrowErrorOnFailure_Is_False() | ||
| { | ||
| var options = DefaultOptions(); | ||
| options.ThrowErrorOnFailure = false; | ||
| var result = JSON.Query(DefaultInput(), options, CancellationToken.None); | ||
| Assert.IsFalse(result.Success); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Should_Use_Custom_ErrorMessageOnFailure() | ||
| { | ||
| var options = DefaultOptions(); | ||
| options.ErrorMessageOnFailure = CustomErrorMessage; | ||
| var ex = Assert.ThrowsException<Exception>(() => | ||
| JSON.Query(DefaultInput(), options, CancellationToken.None)); | ||
| Assert.IsNotNull(ex); | ||
| StringAssert.Contains(ex.Message, CustomErrorMessage); | ||
| } | ||
|
|
||
| private static Input DefaultInput() => new() | ||
| { | ||
| Json = "{\"key\":\"value\"}", | ||
| Query = "$.nonexistent" | ||
| }; | ||
|
|
||
| private static Options DefaultOptions() => | ||
| new() { ErrorWhenNotMatched = true, ThrowErrorOnFailure = true, ErrorMessageOnFailure = string.Empty }; | ||
| } |
4 changes: 3 additions & 1 deletion
4
Frends.JSON.Query/Frends.JSON.Query.UnitTests/Frends.JSON.Query.UnitTests.csproj
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System; | ||
|
|
||
| namespace Frends.JSON.Query.Definitions; | ||
|
|
||
| /// <summary> | ||
| /// Error information returned when the task fails and ThrowErrorOnFailure is false. | ||
| /// </summary> | ||
| public class Error | ||
| { | ||
| /// <summary> | ||
| /// Error message. | ||
| /// </summary> | ||
| public string Message { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The exception that caused the error. | ||
| /// </summary> | ||
| public Exception AdditionalInfo { get; internal set; } | ||
| } |
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
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
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
54 changes: 54 additions & 0 deletions
54
Frends.JSON.Query/Frends.JSON.Query/Helpers/ErrorHandler.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using Frends.JSON.Query.Definitions; | ||
| using System; | ||
|
|
||
| namespace Frends.JSON.Query.Helpers; | ||
|
|
||
| internal static class ErrorHandler | ||
| { | ||
| /// <summary> | ||
| /// Handle an exception according to the task options. | ||
| /// </summary> | ||
| /// <param name="exception">The exception to handle.</param> | ||
| /// <param name="options">Task options that control whether failures are returned as a Result object or thrown.</param> | ||
| /// <param name="throwCanceled"> | ||
| /// When true, an OperationCanceledException is rethrown immediately. | ||
| /// When false, cancellation is handled like any other failure. | ||
| /// </param> | ||
| internal static Result Handle(this Exception exception, Options options, bool throwCanceled = true) | ||
| { | ||
| ThrowIfCanceled(exception, throwCanceled); | ||
| if (options.ThrowErrorOnFailure) ThrowBaseException(exception, options.ErrorMessageOnFailure); | ||
|
|
||
| return ReturnResult(exception, options.ErrorMessageOnFailure); | ||
| } | ||
|
|
||
| private static void ThrowIfCanceled(Exception exception, bool throwCanceled = true) | ||
| { | ||
| if (throwCanceled && exception is OperationCanceledException) throw exception; | ||
| } | ||
|
|
||
| private static void ThrowBaseException(Exception exception, string customMessage = null) | ||
| { | ||
| if (string.IsNullOrEmpty(customMessage)) | ||
| throw new Exception(exception.Message, exception); | ||
|
|
||
| throw new Exception(customMessage, exception); | ||
| } | ||
|
|
||
| private static Result ReturnResult(Exception exception, string customMessage = null) | ||
| { | ||
| var errorMessage = string.IsNullOrEmpty(customMessage) | ||
| ? exception.Message | ||
| : $"{customMessage}: {exception.Message}"; | ||
|
|
||
| return new Result | ||
| { | ||
| Success = false, | ||
| Error = new Error | ||
| { | ||
| Message = errorMessage, | ||
| AdditionalInfo = exception, | ||
| }, | ||
| }; | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot use Result Handle method that was provided in initial prompt instead this code in main method for error handler options. You can also remove newly added constructor with error and add error filed to current constructor, or remove both constructors if they are not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the latest commit. The inline error handling has been extracted into a new
Helpers/ErrorHandler.csfollowing the standardHandleextension method pattern.Query.csnow simply callsex.Handle(options)in the catch block, and the success path uses an object initializer. BothResultconstructors were removed — all three properties now useinternal set, which is what allowsErrorHandlerto use the object initializer syntax.