Skip to content

Commit

Permalink
Always keep innerException
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaya committed May 14, 2024
1 parent 5f3bfad commit 2ef6437
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<SignAssembly>true</SignAssembly>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,9 @@ public async Task<ClientCredentialsToken> RequestTokenAsync(string clientName, C

private static void ThrowOnIdentityModelError(string clientName, TokenResponse response)
{
// Checking this error type first helps us preserve the original exception
if (response is { ErrorType: ResponseErrorType.Exception, Exception: { } exception })
{
throw new ClientCredentialsException(GetErrorMessage(clientName, response), exception);
}

// TokenResponse.IsError and TokenResponse.Error implementations are incomplete but we handle the missing use cases below
// https://github.com/IdentityModel/IdentityModel/blob/6.0.0/src/Client/Messages/ProtocolResponse.cs#L190
if (response.IsError)
{
throw new ClientCredentialsException(GetErrorMessage(clientName, response));
}

switch (response.ErrorType)
{
case ResponseErrorType.Http when response.HttpStatusCode != default:
throw new ClientCredentialsException(GetErrorMessage(clientName, response, $"HTTP error {(int)response.HttpStatusCode}"));
case ResponseErrorType.Protocol:
throw new ClientCredentialsException(GetErrorMessage(clientName, response, $"HTTP error {(int)HttpStatusCode.BadRequest}"));
throw new ClientCredentialsException(GetErrorMessage(clientName, response), response.Exception);
}

if (string.IsNullOrEmpty(response.AccessToken))
Expand All @@ -102,12 +86,12 @@ private static void ThrowOnIdentityModelError(string clientName, TokenResponse r
private static string GetErrorMessage(string clientName, TokenResponse response, string? additionalMessagePart = null)
{
var exceptionMessagePrefixBuilder = new StringBuilder($"An error occurred while retrieving token for client '{clientName}'");

if (response.Error != null)
{
exceptionMessagePrefixBuilder.Append($": {response.Error}");
}

if (response.ErrorDescription != null)
{
exceptionMessagePrefixBuilder.Append($": {response.ErrorDescription}");
Expand All @@ -117,7 +101,7 @@ private static string GetErrorMessage(string clientName, TokenResponse response,
{
exceptionMessagePrefixBuilder.Append($": {additionalMessagePart}");
}

return exceptionMessagePrefixBuilder.ToString();
}
}

0 comments on commit 2ef6437

Please sign in to comment.