-
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
…xperience to Windows PowerShell added tests for invoke-webrequest and invoke-restmethod for http error cases
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,50 @@ | |
|
||
namespace Microsoft.PowerShell.Commands | ||
{ | ||
/// <summary> | ||
/// Exception class for webcmdlets to enable returning HTTP error response | ||
/// </summary> | ||
public class HttpResponseException : Exception | ||
{ | ||
private HttpResponseMessage _response; | ||
private HttpStatusCode _status; | ||
private HttpResponseHeaders _headers; | ||
|
||
/// <summary> | ||
/// Constructor for HttpResponseException | ||
/// </summary> | ||
/// <param name="message">Message for the exception</param> | ||
public HttpResponseException (string message) : base(message) | ||
{} | ||
|
||
/// <summary> | ||
/// HTTP error response | ||
/// </summary> | ||
public HttpResponseMessage Response | ||
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 believe we normally use automatic implemented getter/setter property syntax: public HttpResponseMessage Response { get; set; } 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 fix |
||
{ | ||
get { return _response; } | ||
set { _response = value; } | ||
} | ||
|
||
/// <summary> | ||
/// HTTP error status code | ||
/// </summary> | ||
public HttpStatusCode Status | ||
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. It looks like this property is never set in the error handling code below. Does this just return the response.StatusCode? 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. Was originally adding the individual properties before I saw the example of Windows PowerShell just having a Response property. Didn't remove them. Will remove. |
||
{ | ||
get { return _status; } | ||
set { _status = value; } | ||
} | ||
|
||
/// <summary> | ||
/// HTTP error headers | ||
/// </summary> | ||
public HttpResponseHeaders Headers | ||
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. Same for this property. Is this _response.Headers? 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 remove. |
||
{ | ||
get { return _headers; } | ||
set { _headers = value; } | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Base class for Invoke-RestMethod and Invoke-WebRequest commands. | ||
/// </summary> | ||
|
@@ -355,6 +399,12 @@ protected override void ProcessRecord() | |
response.Content.Headers.ContentLength, | ||
contentType); | ||
WriteVerbose(respVerboseMsg); | ||
string message = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, | ||
response.StatusCode.ToString(), response.ReasonPhrase); | ||
HttpResponseException httpEx = new HttpResponseException(message); | ||
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. You could use this syntax here for creating the HttpResponseException:
Or you could make the constructor take all property values and make the properties read only. 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 change constructor |
||
httpEx.Response = response; | ||
ErrorRecord er = new ErrorRecord(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); | ||
er.ErrorDetails = new ErrorDetails(message); | ||
ProcessResponse(response); | ||
UpdateSession(response); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,6 +399,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { | |
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should BeNullOrEmpty | ||
} | ||
$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)." | ||
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. Minor comment: this might fail if we are going to run this test on a localized machine. 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. @daxian-dbw Is a match good enough for the part returned from the server? 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. changed to Match 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. Yes, I think so, |
||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand" | ||
} | ||
|
||
Describe "Invoke-RestMethod tests" -Tags "Feature" { | ||
|
@@ -642,6 +647,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" { | |
$result = ExecuteWebCommand -command $command | ||
$result.Error | Should BeNullOrEmpty | ||
} | ||
$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)." | ||
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. Same minor comment here. 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. changed to Match |
||
$result.Error.FullyQualifiedErrorId | Should Be "WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand" | ||
} | ||
|
||
Describe "Validate Invoke-WebRequest and Invoke-RestMethod -InFile" -Tags "Feature" { | ||
|
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.
Should this class be sealed? Or is there a reason to derive from it?
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.
Agree, no reason to derive from it