Skip to content

Wrapping the exception to provide better feedback to the user; fixes …#4606

Merged
jbogard merged 1 commit into
mainfrom
4592-provide-a-better-exception-message-for-errors-validating-jwt
Feb 21, 2026
Merged

Wrapping the exception to provide better feedback to the user; fixes …#4606
jbogard merged 1 commit into
mainfrom
4592-provide-a-better-exception-message-for-errors-validating-jwt

Conversation

@jbogard

@jbogard jbogard commented Feb 21, 2026

Copy link
Copy Markdown
Contributor

#4592

@jbogard jbogard added this to the 16.1.0 milestone Feb 21, 2026
@jbogard jbogard linked an issue Feb 21, 2026 that may be closed by this pull request
@jbogard jbogard requested a review from Copilot February 21, 2026 19:48

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 attempts to address issue #4592 by wrapping JWT license validation in exception handling to provide better user feedback. The changes add a try-catch block around the ValidateKey method to catch platform-specific exceptions when RSA cryptography is not supported (e.g., in Blazor WASM scenarios).

Changes:

  • Added try-catch block around JWT validation logic in ValidateKey method
  • Added specific handling for PlatformNotSupportedException with informative logging

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +82 to 89
catch (PlatformNotSupportedException)
{
_logger.LogCritical(validateResult.Exception, "Error validating the Lucky Penny software license key");
_logger.LogInformation(
"RSA cryptography is not supported on this platform. " +
"For client redistribution scenarios such as Blazor WASM, see: " +
"https://docs.automapper.io/en/latest/License-configuration.html#client-redistribution-scenarios");
return [];
}

Copilot AI Feb 21, 2026

Copy link

Choose a reason for hiding this comment

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

The PR description states it fixes issue #4592 which asks for better exception messages for JWT validation errors (specifically ArgumentException and FormatException like "IDX14101: Unable to decode the payload"). However, the code changes only add a try-catch for PlatformNotSupportedException, which is a different scenario (unsupported cryptography on certain platforms like Blazor WASM). The original issue's JWT decoding errors would still not be caught or given better messages by this implementation.

Copilot uses AI. Check for mistakes.
"For client redistribution scenarios such as Blazor WASM, see: " +
"https://docs.automapper.io/en/latest/License-configuration.html#client-redistribution-scenarios");
return [];
}

Copilot AI Feb 21, 2026

Copy link

Choose a reason for hiding this comment

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

The PR description states this fixes issue #4592, which mentions catching exceptions like ArgumentException and FormatException during JWT validation to provide better error messages. However, the current implementation only catches PlatformNotSupportedException. The ArgumentException and FormatException shown in the issue (IDX14101, IDX10400) would not be caught by this try-catch block. Consider adding a catch block for these exceptions (or a more general exception type) that logs a user-friendly message like "The provided license key is invalid or malformed. Please check that you have copied the entire license key correctly."

Suggested change
}
}
catch (ArgumentException ex)
{
_logger.LogWarning(ex,
"The provided license key is invalid or malformed. Please check that you have copied the entire license key correctly.");
return [];
}
catch (FormatException ex)
{
_logger.LogWarning(ex,
"The provided license key is invalid or malformed. Please check that you have copied the entire license key correctly.");
return [];
}

Copilot uses AI. Check for mistakes.
ValidateLifetime = false
};

var validateResult = handler.ValidateTokenAsync(licenseKey, parms).Result;

Copilot AI Feb 21, 2026

Copy link

Choose a reason for hiding this comment

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

Using .Result on an async method can cause deadlocks in certain synchronization contexts and wraps exceptions in AggregateException. Since the handler.ValidateTokenAsync call might throw exceptions synchronously (before returning a Task), those exceptions may be wrapped in AggregateException when accessing .Result. This could complicate exception handling. Consider using GetAwaiter().GetResult() instead, which unwraps the exception, or making this method async and using await.

Suggested change
var validateResult = handler.ValidateTokenAsync(licenseKey, parms).Result;
var validateResult = handler.ValidateTokenAsync(licenseKey, parms).GetAwaiter().GetResult();

Copilot uses AI. Check for mistakes.
Comment thread src/AutoMapper/Licensing/LicenseAccessor.cs
@jbogard jbogard merged commit 0c96327 into main Feb 21, 2026
@jbogard jbogard deleted the 4592-provide-a-better-exception-message-for-errors-validating-jwt branch February 21, 2026 19:53
This was referenced Mar 4, 2026
This was referenced Mar 14, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a better exception message for errors validating JWT.

2 participants