Skip to content

Commit 1ef54d2

Browse files
committed
Fix for vscode-powershell issue 168.
Catch exceptions when calling ToString().
1 parent 72099dd commit 1ef54d2

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

src/PowerShellEditorServices/Debugging/VariableDetails.cs

+26-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Linq;
1111
using System.Management.Automation;
1212
using System.Reflection;
13+
using Microsoft.PowerShell.EditorServices.Utility;
1314

1415
namespace Microsoft.PowerShell.EditorServices
1516
{
@@ -159,44 +160,49 @@ private static string GetValueString(object value, bool isExpandable)
159160
entry.Key,
160161
GetValueString(entry.Value, GetIsExpandable(entry.Value)));
161162
}
162-
else if (value.ToString().Equals(objType.ToString()))
163+
else
163164
{
164-
// If the ToString() matches the type name, then display the type
165-
// name in PowerShell format.
166-
string shortTypeName = objType.Name;
165+
string valueToString = value.SafeToString();
167166

168-
// For arrays and ICollection, display the number of contained items.
169-
if (value is Array)
167+
if (valueToString.Equals(objType.ToString()))
170168
{
171-
var arr = value as Array;
172-
if (arr.Rank == 1)
169+
// If the ToString() matches the type name, then display the type
170+
// name in PowerShell format.
171+
string shortTypeName = objType.Name;
172+
173+
// For arrays and ICollection, display the number of contained items.
174+
if (value is Array)
173175
{
174-
shortTypeName = InsertDimensionSize(shortTypeName, arr.Length);
176+
var arr = value as Array;
177+
if (arr.Rank == 1)
178+
{
179+
shortTypeName = InsertDimensionSize(shortTypeName, arr.Length);
180+
}
175181
}
182+
else if (value is ICollection)
183+
{
184+
var collection = (ICollection)value;
185+
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
186+
}
187+
188+
valueString = "[" + shortTypeName + "]";
176189
}
177-
else if (value is ICollection)
190+
else
178191
{
179-
var collection = (ICollection)value;
180-
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
192+
valueString = valueToString;
181193
}
182-
183-
valueString = "[" + shortTypeName + "]";
184-
}
185-
else
186-
{
187-
valueString = value.ToString();
188194
}
189195
}
190196
else
191197
{
192-
// ToString() output is not the typename, so display that as this object's value
198+
// Value is a scalar (not expandable). If it's a string, display it directly otherwise use SafeToString()
193199
if (value is string)
194200
{
195201
valueString = "\"" + value + "\"";
196202
}
197203
else
198204
{
199-
valueString = value.ToString();
205+
valueString = value.SafeToString();
200206
}
201207
}
202208

src/PowerShellEditorServices/PowerShellEditorServices.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<Compile Include="Utility\AsyncLock.cs" />
121121
<Compile Include="Utility\AsyncQueue.cs" />
122122
<Compile Include="Utility\AsyncContext.cs" />
123+
<Compile Include="Utility\Extensions.cs" />
123124
<Compile Include="Utility\Logger.cs" />
124125
<Compile Include="Utility\ThreadSynchronizationContext.cs" />
125126
<Compile Include="Utility\Validate.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Utility
9+
{
10+
internal static class ObjectExtensions
11+
{
12+
/// <summary>
13+
/// Extension to evaluate an object's ToString() method in an exception safe way. This will
14+
/// extension method will not throw.
15+
/// </summary>
16+
/// <param name="obj">The object on which to call ToString()</param>
17+
/// <returns>The ToString() return value or a suitable error message is that throws.</returns>
18+
public static string SafeToString(this object obj)
19+
{
20+
string str;
21+
22+
try
23+
{
24+
str = obj.ToString();
25+
}
26+
catch (Exception ex)
27+
{
28+
str = $"<Error converting poperty value to string - {ex.Message}>";
29+
}
30+
31+
return str;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)