Fetch resource string correctly#5114
Conversation
| "WebCmdletStrings", "JsonStringInBadFormat"); | ||
| ErrorRecord errorRecord = new ErrorRecord( | ||
| new InvalidOperationException(details.Message), | ||
| new InvalidOperationException(WebCmdletStrings.JsonStringInBadFormat), |
There was a problem hiding this comment.
Is the change for the PR? I don't see a description and a test.
There was a problem hiding this comment.
If the resource file name and the resource string id is known/fixed, there is no point to use ResourceManager to retrieve the error message (which is what ErrorDetails does). Instead, we can just use the generated C# property.
The existence of the resource string is guaranteed by the build, so I didn't add a test for it.
There was a problem hiding this comment.
I meant that the PR description mention only Add-Member cmdlet but not ConvertTo-Json.
There was a problem hiding this comment.
Updated the description to include the ConvertTo-Json change.
There was a problem hiding this comment.
The error string for this resource (WebCmdletStrings.JsonStringInBadFormat) doesn't seem right:
"The converted JSON string is in bad format."
I think this would be a better error message:
"The JSON string could not be converted because the format is incorrect."
There was a problem hiding this comment.
This code is from ConvertTo-Json cmdlet, which takes arbitrary objects and converts them to JSON string. The error message is used when it detects the JSON string we converted to is in bad format.
PaulHigin
left a comment
There was a problem hiding this comment.
Please also update the WebCmdletStrings.JsonStringInBadFormat error string.
| "WebCmdletStrings", "JsonStringInBadFormat"); | ||
| ErrorRecord errorRecord = new ErrorRecord( | ||
| new InvalidOperationException(details.Message), | ||
| new InvalidOperationException(WebCmdletStrings.JsonStringInBadFormat), |
There was a problem hiding this comment.
The error string for this resource (WebCmdletStrings.JsonStringInBadFormat) doesn't seem right:
"The converted JSON string is in bad format."
I think this would be a better error message:
"The JSON string could not be converted because the format is incorrect."
I found
Add-Memberthrows error with an empty message. This PR fixes it.Before fix
After fix
Also updated a resource string use in
ConvertTo-Jsonto directly use the C# property.