2121using System . Text . RegularExpressions ;
2222using System . Threading ;
2323using System . Threading . Tasks ;
24+ using Microsoft . Azure . Commands . Common ;
25+ using Microsoft . Azure . Commands . Common . Exceptions ;
2426using Newtonsoft . Json ;
2527using Newtonsoft . Json . Linq ;
2628
@@ -112,10 +114,22 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
112114 EnqueueDebug ( "No token returned (null/empty)." ) ;
113115 }
114116 }
117+ catch ( AzPSInvalidOperationException )
118+ {
119+ throw ;
120+ }
121+ catch ( AzPSCloudException )
122+ {
123+ throw ;
124+ }
115125 catch ( Exception ex )
116126 {
117127 EnqueueDebug ( $ "Exception: { ex . GetType ( ) . Name } : { ex . Message } ") ;
118- throw new InvalidOperationException ( $ "Failed to acquire policy token: { ex . Message } ", ex ) ;
128+ throw new AzPSInvalidOperationException (
129+ $ "Failed to acquire policy token for { request . Method } { request . RequestUri } : { ex . Message } ",
130+ ErrorKind . ServiceError ,
131+ ex ,
132+ desensitizedMessage : "Failed to acquire policy token." ) ;
119133 }
120134
121135 return await base . SendAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -126,8 +140,11 @@ private async Task<string> AcquirePolicyTokenAsync(HttpRequestMessage originalRe
126140 var subscriptionId = ExtractSubscriptionId ( originalRequest . RequestUri ) ;
127141 if ( string . IsNullOrEmpty ( subscriptionId ) )
128142 {
129- EnqueueDebug ( "Failed: subscription id not found in URI." ) ;
130- throw new InvalidOperationException ( "Unable to determine subscription ID for policy token acquisition." ) ;
143+ EnqueueDebug ( $ "Failed: subscription id not found in URI { originalRequest . RequestUri } .") ;
144+ throw new AzPSInvalidOperationException (
145+ $ "Unable to determine subscription ID for policy token acquisition from URI: { originalRequest . RequestUri } ",
146+ ErrorKind . UserError ,
147+ desensitizedMessage : "Unable to determine subscription ID for policy token acquisition." ) ;
131148 }
132149
133150 var authority = originalRequest . RequestUri . GetLeftPart ( UriPartial . Authority ) ;
@@ -180,6 +197,10 @@ private async Task<string> AcquirePolicyTokenAsync(HttpRequestMessage originalRe
180197 {
181198 tokenRequest . Headers . TryAddWithoutValidation ( "x-ms-authorization-auxiliary" , auxValues ) ;
182199 }
200+ if ( originalRequest . Headers . TryGetValues ( "x-ms-client-request-id" , out var clientRequestIdValues ) )
201+ {
202+ tokenRequest . Headers . TryAddWithoutValidation ( "x-ms-client-request-id" , clientRequestIdValues ) ;
203+ }
183204
184205 var isOwnedClient = _tokenHttpClient == null ;
185206 var http = _tokenHttpClient ?? new HttpClient ( ) ;
@@ -198,21 +219,33 @@ private async Task<string> AcquirePolicyTokenAsync(HttpRequestMessage originalRe
198219 if ( string . IsNullOrEmpty ( token ) )
199220 {
200221 EnqueueDebug ( "Response OK but token missing." ) ;
201- throw new InvalidOperationException ( $ "No token returned. Response:{ responseContent } ") ;
222+ throw new AzPSCloudException (
223+ $ "Policy token acquisition succeeded but no token was returned. Response: { responseContent } ",
224+ ErrorKind . ServiceError ,
225+ desensitizedMessage : "Policy token acquisition succeeded but no token was returned." ) ;
202226 }
203227 return token ;
204228 }
205- throw new InvalidOperationException ( "Empty response body when acquiring policy token." ) ;
229+ throw new AzPSCloudException (
230+ "Policy token acquisition returned an empty response body." ,
231+ ErrorKind . ServiceError ,
232+ desensitizedMessage : "Policy token acquisition returned an empty response body." ) ;
206233 }
207234 else if ( response . StatusCode == HttpStatusCode . Accepted )
208235 {
209236 EnqueueDebug ( "202 Accepted received (async not supported)." ) ;
210- throw new InvalidOperationException ( "Asynchronous policy token acquisition (202 Accepted) is not supported." ) ;
237+ throw new AzPSCloudException (
238+ "Asynchronous policy token acquisition (202 Accepted) is not supported." ,
239+ ErrorKind . ServiceError ,
240+ desensitizedMessage : "Asynchronous policy token acquisition (202 Accepted) is not supported." ) ;
211241 }
212242 else
213243 {
214- EnqueueDebug ( "Non-success status; will throw." ) ;
215- throw new InvalidOperationException ( $ "Policy token acquisition failed with { ( int ) response . StatusCode } { response . StatusCode } : { responseContent } ") ;
244+ EnqueueDebug ( $ "Non-success status { ( int ) response . StatusCode } ; will throw.") ;
245+ throw new AzPSCloudException (
246+ $ "Policy token acquisition failed with { ( int ) response . StatusCode } { response . StatusCode } : { responseContent } ",
247+ ErrorKind . ServiceError ,
248+ desensitizedMessage : $ "Policy token acquisition failed with status { ( int ) response . StatusCode } .") ;
216249 }
217250 }
218251 finally
0 commit comments