You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConvertFrom-Json does not respect -ErrorAction #2860
@PowerShell/powershell-committee (subset of Hemant Mahawar (@HemantMahawar)Jason Shirk (@lzybkr)Kenneth Hansen (@khansen00) and myself) agree that the original issue (#2860) is by design. It is unreasonable for ConvertFrom-Json to emit anything but an error when receiving malformed input, and terminating errors are not respected by -ErrorAction. If a developer absolutely wants to make a conscious choice to ignore that terminating error, they should use try/catch.
Joey,
The proposed fix for this was not accepted and resolved as by-design. Under the hood, Json.Net throws an ArgumentException for malformed json content.
Then I'm not understanding why it's not terminating the statement above....my understanding of PS errors (which may be busted, but please help me understand):
terminating errors (usually surfaced from internal exceptions) should terminate the script block or statement. In this case, if the ArgumentException is terminating at the PS level, the 2+2 should then not be executed.
Non-terminating errors respect -ErrorAction. If ArgumentException was caught as a non-terminating error, -ErrorAction SilentlyContinue should silence the error.
This appears to be a side-effect of Cmdlet.ThrowTerminatingError; -ErrorAction is apparently ignored. It's not clear if it is intentional. I see the same behavior on Windows PowerShell.
Here's a couple of PS functions that illustrate the behavior difference:
Okay, now I really don't know what's going on. With your two functions, TerminatingError;2+2 shows that it's not really terminating, but ExceptionError;2+2is terminating.
Unless you have some more insight, Dan Travison (@dantraMSFT), I'll probably open another issue and figure out what the intended behavior is supposed to be.
For posterity, my understanding of terminating errors was wrong. A terminating error cancels execution of the current pipeline or expression, not of an entire script block.
A better way to check for terminating errors is to just use try/catch:
C:\Program Files\PowerShell\6.0.0-alpha.18>try {"fdasf"|convertfrom-json} catch {1}
1
C:\Program Files\PowerShell\6.0.0-alpha.18>try {ls foo} catch {1}
ls : Cannot find path 'C:\Program Files\PowerShell\6.0.0-alpha.18\foo' because it does not exist.
At line:1 char:6+try {ls foo} catch {1}
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Program File....0-alpha.18\foo:String) [Get-ChildItem], ItemNotFound
Exception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Reacted by Michael Klement, Joel Bennett and noamper
I know this is an old closed issue... but using "resolvedByDesign" as an excuse is lame... The optics of this is taht an error is an error is an error, irrespective of terminating or non-terminating. Thus-ErrorAction should be respected for all errors and cmdlets, or it becomes inconsistent and unpredictable. End developers should be free to use errorAction as they see fit.
$json = ("apple" | convertFrom-json -ErrorAction SilentlyContinue)
if (!$json) { write-warning "Issue with json input - ignoring" }
OR maybe we need -TerminatingErrorAction as a new switch? 😊
Reacted by J.D. Cain, SJonesy, Andrew Jackson, Gijs Reijn and ili101
I find ConvertFrom-Json not respecting -ErrorAction counterintuitive and a poor user experience.
I'm reading a stream of one-line json's, and try catch can't be embedded in a pipeline, so here is my workaround: kubectl logs my-pod | ConvertFrom-JsonSafe
function ConvertFrom-JsonSafe {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, Mandatory=$true)]
[string]$InputObject,
[Switch]$Quiet
)
process {
try {
$InputObject | ConvertFrom-Json
} catch {
# If $Quiet is not set, log a warning
if (-not $Quiet) {
Write-Warning "Failed to convert JSON: $InputObject"
}
}
}
}
Activity
alastairs commentedon Dec 8, 2016
If this is not the appropriate place to report bugs against v5.x, please let me know where I can.
iSazonov commentedon Dec 8, 2016
Alastair Smith (@alastairs) Could you please test this with recent Powershell 6 build?
alastairs commentedon Dec 8, 2016
Ilya (@iSazonov) The behaviour is the same with 6.0.0-alpha13
BTW, I love that trying this out was just a case of extracting a zip file somewhere and not a full-blow installer. Great work! 👍
alastairs commentedon Dec 8, 2016
Some further information in case it's useful: my
$ErrorActionPreferencevariable is set to"Stop".joeyaiello commentedon Feb 16, 2017
@PowerShell/powershell-committee (subset of Hemant Mahawar (@HemantMahawar) Jason Shirk (@lzybkr) Kenneth Hansen (@khansen00) and myself) agree that the original issue (#2860) is by design. It is unreasonable for ConvertFrom-Json to emit anything but an error when receiving malformed input, and terminating errors are not respected by -ErrorAction. If a developer absolutely wants to make a conscious choice to ignore that terminating error, they should use try/catch.
joeyaiello commentedon Apr 13, 2017
Okay, now I actually don't know what's going on. It would appear that this is currently a non-terminating error:
If that's the case, then
-ErrorActionshould be respected, right?dantraMSFT commentedon Apr 13, 2017
Joey,
The proposed fix for this was not accepted and resolved as by-design. Under the hood, Json.Net throws an ArgumentException for malformed json content.
joeyaiello commentedon Apr 14, 2017
Then I'm not understanding why it's not terminating the statement above....my understanding of PS errors (which may be busted, but please help me understand):
ArgumentExceptionis terminating at the PS level, the2+2should then not be executed.-ErrorAction. IfArgumentExceptionwas caught as a non-terminating error,-ErrorAction SilentlyContinueshould silence the error.What am I missing here?
dantraMSFT commentedon Apr 14, 2017
This appears to be a side-effect of Cmdlet.ThrowTerminatingError; -ErrorAction is apparently ignored. It's not clear if it is intentional. I see the same behavior on Windows PowerShell.
Here's a couple of PS functions that illustrate the behavior difference:
joeyaiello commentedon Apr 14, 2017
Okay, now I really don't know what's going on. With your two functions,
TerminatingError;2+2shows that it's not really terminating, butExceptionError;2+2is terminating.joeyaiello commentedon Apr 14, 2017
Unless you have some more insight, Dan Travison (@dantraMSFT), I'll probably open another issue and figure out what the intended behavior is supposed to be.
SteveL-MSFT commentedon Apr 14, 2017
cc Bruce Payette (@BrucePay)
Behavior with TerminatingError and Exception is inconsistent, not sure if intended.
joeyaiello commentedon Apr 17, 2017
Alright, I'm closing it, I'll follow up offline or in another issue.
joeyaiello commentedon Apr 18, 2017
For posterity, my understanding of terminating errors was wrong. A terminating error cancels execution of the current pipeline or expression, not of an entire script block.
A better way to check for terminating errors is to just use try/catch:
mklement0 commentedon Aug 15, 2017
Joey Aiello (@joeyaiello):
I had the same misconception, and I think the source is that the current help topics conflate statement-terminating errors with script-terminating errors - please see Our Error Handling, Ourselves - time to fully understand and properly document PowerShell's error handling.
awsles commentedon May 23, 2021
I know this is an old closed issue... but using "resolvedByDesign" as an excuse is lame... The optics of this is taht an error is an error is an error, irrespective of terminating or non-terminating. Thus
-ErrorActionshould be respected for all errors and cmdlets, or it becomes inconsistent and unpredictable. End developers should be free to use errorAction as they see fit.OR maybe we need
-TerminatingErrorActionas a new switch? 😊gerardog commentedon Oct 10, 2024
I find
ConvertFrom-Jsonnot respecting-ErrorActioncounterintuitive and a poor user experience.I'm reading a stream of one-line json's, and try catch can't be embedded in a pipeline, so here is my workaround:
kubectl logs my-pod | ConvertFrom-JsonSafe