-
Notifications
You must be signed in to change notification settings - Fork 7.7k
return HTTP response for error status as part of exception #3201
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
Changes from 1 commit
0d8be7b
6fe216a
1920187
8483c7c
5dd0b5f
eaaaa7f
4a8e693
4a420db
c3c939f
4b1e21c
8fbe863
650839b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… if detailMsg is empty changed error message test to be localization friendly by just matching the part returned from server
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,16 +379,28 @@ protected override void ProcessRecord() | |
< 10000 td class="blob-code blob-code-context js-file-line"> if (!response.IsSuccessStatusCode) | ||
{ | ||
string message = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, | ||
Convert.ToInt32(response.StatusCode).ToString(), response.ReasonPhrase); | ||
(int)response.StatusCode, response.ReasonPhrase); | ||
HttpResponseException httpEx = new HttpResponseException(message, response); | ||
ErrorRecord er = new ErrorRecord(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); | ||
string detailMsg = ""; | ||
using (StreamReader reader = new StreamReader(StreamHelper.GetResponseStream(response))) | ||
StreamReader reader = new StreamReader(StreamHelper.GetResponseStream(response)); | ||
try | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest using try-catch-finally to avoid nesting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @2xmax do you mean like this?
|
||
{ | ||
// remove HTML tags making it easier to read | ||
detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>",""); | ||
} | ||
er.ErrorDetails = new ErrorDetails(detailMsg); | ||
catch (Exception) | ||
{ | ||
// catch all | ||
} | ||
finally | ||
{ | ||
reader.Dispose(); | ||
} | ||
if (!String.IsNullOrEmpty(detailMsg)) | ||
{ | ||
er.ErrorDetails = new ErrorDetails(detailMsg); | ||
} | ||
ThrowTerminatingError(er); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will make the change |
||
} | ||
ProcessResponse(response); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -409,7 +409,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
$result.Error.Exception | Should BeOfType Microsoft.PowerShell.Commands.HttpResponseException | ||
$result.Error.Exception.Response.StatusCode | Should Be 418 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we test for Exception.Headers and Exception.Status too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are getting removed |
||
$result.Error.Exception.Response.ReasonPhrase | Should Be "I'm a teapot" | ||
$result.Error.Exception.Message | Should Be "Response status code does not indicate success: 418 (I'm a teapot)." | ||
$result.Error.Exception.Message | Should Match ": 418 \(I'm a teapot\)\." | ||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" | ||
} | ||
} | ||
|
@@ -665,7 +665,7 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { | |
$result.Error.Exception | Should BeOfType Microsoft.PowerShell.Commands.HttpResponseException | ||
$result.Error.Exception.Response.StatusCode | Should Be 418 | ||
$result.Error.Exception.Response.ReasonPhrase | Should Be "I'm a teapot" | ||
$result.Error.Exception.Message | Should Be "Response status code does not indicate success: 418 (I'm a teapot)." | ||
$result.Error.Exception.Message | Should Match ": 418 \(I'm a teapot\)\." | ||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid you need to put
new StreamReader(StreamHelper.GetResponseStream(response));
in the try block. Maybe like this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed