Skip to content

Conversation

@AnatoliB
Copy link
Contributor

Resolves #145 (see the last proposal).

For example, the following script:

$ErrorActionPreference = 'Stop'
function MyFunctionA { 'abc' | ConvertFrom-Json }
function MyFunctionB { MyFunctionA }
MyFunctionB

will produce a trace like this:

[9/16/2019 7:46:23 PM] EXCEPTION: ConvertFrom-Json : Conversion from JSON failed with error: Unexpected character encountered while parsing value: a. Path '', line 0, position 0.
[9/16/2019 7:46:23 PM] At D:\GitHub\AnatoliB\af-ps-errors\Error\run.ps1:8 char:13
[9/16/2019 7:46:23 PM] +     'abc' | ConvertFrom-Json
[9/16/2019 7:46:23 PM] +             ~~~~~~~~~~~~~~~~
[9/16/2019 7:46:23 PM] + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
[9/16/2019 7:46:23 PM] + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
[9/16/2019 7:46:23 PM]
[9/16/2019 7:46:23 PM] Script stack trace:
[9/16/2019 7:46:23 PM]    at MyFunctionA, D:\GitHub\AnatoliB\af-ps-errors\Error\run.ps1: line 8
[9/16/2019 7:46:23 PM]    at MyFunctionB, D:\GitHub\AnatoliB\af-ps-errors\Error\run.ps1: line 12
[9/16/2019 7:46:23 PM]    at <ScriptBlock>, D:\GitHub\AnatoliB\af-ps-errors\Error\run.ps1: line 17
[9/16/2019 7:46:23 PM]
[9/16/2019 7:46:23 PM] System.ArgumentException: Conversion from JSON failed with error: Unexpected character encountered while parsing value: a. Path '', line 0, position 0.
[9/16/2019 7:46:23 PM]    at Microsoft.PowerShell.Commands.JsonObject.ConvertFromJson(String input, Boolean returnHashtable, Nullable`1 maxDepth, ErrorRecord& error)
[9/16/2019 7:46:23 PM]    at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.ConvertFromJsonHelper(String input)
[9/16/2019 7:46:23 PM]    at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.EndProcessing()
[9/16/2019 7:46:23 PM]    at System.Management.Automation.Cmdlet.DoEndProcessing()
[9/16/2019 7:46:23 PM]    at System.Management.Automation.CommandProcessorBase.Complete()
[9/16/2019 7:46:23 PM]
[9/16/2019 7:46:23 PM] Inner exception: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: a. Path '', line 0, position 0.
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonTextReader.ParseValue()
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonTextReader.Read()
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonReader.ReadAndMoveToContent()
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
[9/16/2019 7:46:23 PM]    at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings)
[9/16/2019 7:46:23 PM]    at Microsoft.PowerShell.Commands.JsonObject.ConvertFromJson(String input, Boolean returnHashtable, Nullable`1 maxDepth, ErrorRecord& error)

Copy link
Contributor

@Francisco-Gamino Francisco-Gamino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor comments. Other than that, LGTM.

@AnatoliB AnatoliB merged commit 1fda3f2 into Azure:dev Sep 17, 2019
@AnatoliB AnatoliB deleted the error-logging-2 branch September 17, 2019 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatically log ErrorRecords instead of just Exceptions

2 participants