GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||
GeezerVison is showing github.com — Small cleanup Invoke-RestMethod by CarloToso · Pull Request #19490 · PowerShell/PowerShell Conversation
|
|
|
||
| // NOTE: Tests use this verbose output to verify the encoding. | ||
| WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}")); | ||
| WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encoding.HeaderName}")); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
HeaderName can throw.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I don't think it will throw
Test:
[System.Text.Encoding]::GetEncodings().GetEncoding().Count -eq ` ([System.Text.Encoding]::GetEncodings().GetEncoding() | % {$_.EncodingName}).Count -eq ` ([System.Text.Encoding]::GetEncodings().GetEncoding() | % {$_.HeaderName}).Count -eq 116 #tested on windows
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There is throw in implementation.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
EncodingName: https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs,355
HeaderName: https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs,371
Both can throw in the same condition: https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs,327
So I don't think the try-catch is correct
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I don't understand - you point NotSupportedException in the implementation but says the try-catch is not correct.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'll try to explain better.
We obtain the encoding from StreamHelper.DecodeStream() --> TryGetEncoding() --> this will always generate a valid encoding, if there is an unrecognized encoding it will be replaced by ContentHelper.GetDefaultEncoding() --> UTF8
Every valid encoding (all 116 of them) has both encoding.EncodingName and encoding.HeaderName so we don't need to check string.IsNullOrEmpty(encoding.HeaderName) and we can simply choose the name we like best
If an invalid encoding got to the encodingVerboseName try-catch:
string encodingVerboseName; try { encodingVerboseName = string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName; } catch (NotSupportedException) { encodingVerboseName = encoding.EncodingName; }
both the names throw in the same condition (EncodingTable.GetCodePageDataItem(_codePage) is null) so both would throw:
Cases:
string.IsNullOrEmpty(encoding.HeaderName) --> encodingVerboseName = encoding.EncodingName --> throw --> catch --> encodingVerboseName = encoding.EncodingName --> throw
!string.IsNullOrEmpty(encoding.HeaderName) --> encodingVerboseName = encoding.HeaderName --> throw --> catch --> encodingVerboseName = encoding.EncodingName --> throw
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
3 is good catch and we could use String.Empty.
I cannot agree with 1. and 2. points since it is not safe as the previous code can be changed and also there is dynamic code pages registration.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I updated the code following your suggestions
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
🎉 Handy links: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.