-
Notifications
You must be signed in to change notification settings - Fork 0
Fix Frends.JSON.QuerySingle compliance findings: net8, static class, CancellationToken, error handling #43
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
Merged
MatteoDelOmbra
merged 9 commits into
main
from
copilot/fix-compliance-findings-frends-json-querysingle
Aug 20, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c3f9714
Initial plan
Copilot bff0670
Fix compliance findings in Frends.JSON.QuerySingle
Copilot 3bd4197
Move Error to separate file, remove Result constructors
Copilot f01370c
Address review feedback: clean CHANGELOG, convert ErrorHandlerTests t…
Copilot 92a9eb2
Convert ErrorHandlerTests to MSTest, remove NUnit packages
Copilot 1142385
Enhance XML comments in Error class
MichalFrends1 74fb6b5
Update example in Options.cs for error message
MichalFrends1 6dd88a4
Enhance documentation for Error property
MichalFrends1 b0a8215
Fix example XML comment for error message
MichalFrends1 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
Some comments aren't visible on the classic Files Changed page.
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
54 changes: 54 additions & 0 deletions
54
Frends.JSON.QuerySingle/Frends.JSON.QuerySingle.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,54 @@ | ||
| using Frends.JSON.QuerySingle.Definitions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Threading; | ||
|
|
||
| namespace Frends.JSON.QuerySingle.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class ErrorHandlerTests | ||
| { | ||
| private const string CustomErrorMessage = "CustomErrorMessage"; | ||
|
|
||
| private static Input InvalidInput() => new Input | ||
| { | ||
| Json = "not valid json", | ||
| Query = "$.key" | ||
| }; | ||
|
|
||
| private static Options DefaultOptions() => new Options | ||
| { | ||
| ErrorWhenNotMatched = false, | ||
| ThrowErrorOnFailure = true, | ||
| ErrorMessageOnFailure = string.Empty | ||
| }; | ||
|
|
||
| [TestMethod] | ||
| public void Should_Throw_Error_When_ThrowErrorOnFailure_Is_True() | ||
| { | ||
| var ex = Assert.ThrowsException<Exception>((Action)(() => | ||
| JSON.QuerySingle(InvalidInput(), 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.QuerySingle(InvalidInput(), options, CancellationToken.None); | ||
| Assert.IsFalse(result.Success); | ||
| Assert.IsNotNull(result.Error); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Should_Use_Custom_ErrorMessageOnFailure() | ||
| { | ||
| var options = DefaultOptions(); | ||
| options.ErrorMessageOnFailure = CustomErrorMessage; | ||
| var ex = Assert.ThrowsException<Exception>((Action)(() => | ||
| JSON.QuerySingle(InvalidInput(), options, CancellationToken.None))); | ||
| Assert.IsNotNull(ex); | ||
| Assert.IsTrue(ex.Message.Contains(CustomErrorMessage)); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...ON.QuerySingle/Frends.JSON.QuerySingle.UnitTests/Frends.JSON.QuerySingle.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
21 changes: 21 additions & 0 deletions
21
Frends.JSON.QuerySingle/Frends.JSON.QuerySingle/Definitions/Error.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,21 @@ | ||
| using System; | ||
|
|
||
| namespace Frends.JSON.QuerySingle.Definitions; | ||
|
|
||
| /// <summary> | ||
| /// Error details. | ||
| /// </summary> | ||
| public class Error | ||
| { | ||
| /// <summary> | ||
| /// Error message. | ||
| /// </summary> | ||
| /// <example>Query failed unexpectedly.</example> | ||
| public string Message { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Additional information about the error (exception). | ||
| /// </summary> | ||
| /// <example>object { Exception AdditionalInfo }</example> | ||
| public Exception AdditionalInfo { get; 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
4 changes: 2 additions & 2 deletions
4
Frends.JSON.QuerySingle/Frends.JSON.QuerySingle/Frends.JSON.QuerySingle.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
45 changes: 45 additions & 0 deletions
45
Frends.JSON.QuerySingle/Frends.JSON.QuerySingle/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,45 @@ | ||
| using Frends.JSON.QuerySingle.Definitions; | ||
| using System; | ||
|
|
||
| namespace Frends.JSON.QuerySingle.Helpers; | ||
|
|
||
| internal static class ErrorHandler | ||
| { | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.