@@ -35,14 +35,14 @@ private async Task<Token> RetrieveTokenAsync(string grantType, CancellationToken
3535 {
3636 result . EnsureSuccessStatusCode ( ) ;
3737 return Token . FromTokenResponse (
38- await result . Content . ReadFromJsonAsync < TokenResponse > ( cancellationToken ) ?? throw new HttpRequestException ( Translations . INVALID_OR_UNEXPECTED_RESPONSE , null , result . StatusCode ) ,
38+ await result . Content . ReadFromJsonAsync < TokenResponse > ( cancellationToken ) ?? throw new ExtendedHttpRequestException ( Translations . INVALID_OR_UNEXPECTED_RESPONSE , null , result . StatusCode ) ,
3939 _timeprovider . GetUtcNow ( )
4040 ) ;
4141 }
4242 catch ( HttpRequestException ex )
4343 {
44- var err = await result . Content . ReadFromJsonAsync < ErrorResponse > ( cancellationToken ) ?? throw new HttpRequestException ( Translations . INVALID_OR_UNEXPECTED_RESPONSE , null , result . StatusCode ) ;
45- throw new HttpRequestException ( $ "[{ err . Error } ] { err . Description } ", ex , result . StatusCode ) ;
44+ var err = await result . Content . ReadFromJsonAsync < ErrorResponse > ( cancellationToken ) ?? throw new ExtendedHttpRequestException ( Translations . INVALID_OR_UNEXPECTED_RESPONSE , null , result . StatusCode ) ;
45+ throw new ExtendedHttpRequestException ( $ "[{ err . Error } ] { err . Description } ", ex , result . StatusCode ) ;
4646 }
4747 }
4848
@@ -76,7 +76,7 @@ private async Task<Token> RefreshTokenAsync(Token token, CancellationToken cance
7676 return await RetrieveTokenAsync ( GrantTypes . Refresh , cancellationToken ) ;
7777 }
7878 }
79- catch ( HttpRequestException ex ) when ( ex . StatusCode is HttpStatusCode . BadRequest or HttpStatusCode . Unauthorized ) { }
79+ catch ( ExtendedHttpRequestException ex ) when ( ex . StatusCode is HttpStatusCode . BadRequest or HttpStatusCode . Unauthorized ) { }
8080 // Refresh token expired or refresh failed? Try to get a new token
8181 return await RetrieveTokenAsync ( GetGrantType ( ) , cancellationToken ) . ConfigureAwait ( false ) ;
8282 }
@@ -99,4 +99,9 @@ private record ErrorResponse
9999 [ property: JsonPropertyName ( "error" ) ] string Error ,
100100 [ property: JsonPropertyName ( "error_description" ) ] string Description
101101 ) ;
102- }
102+
103+ private class ExtendedHttpRequestException ( string message , Exception ? innerException , HttpStatusCode httpStatusCode ) : HttpRequestException ( message , innerException )
104+ {
105+ public HttpStatusCode StatusCode { get ; private set ; } = httpStatusCode ;
106+ }
107+ }
0 commit comments