Skip to content

Commit e033235

Browse files
committed
Updated the error message in the console to match more closely with the ISE.
1 parent fc9551f commit e033235

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

Cognifide.PowerShell/Client/Applications/PowerShellIse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ protected virtual void ClientExecute(ClientPipelineArgs args)
453453
result += scriptSession.Output.ToHtml();
454454
}
455455
result += string.Format("<pre style='background:red;'>{0}</pre>",
456-
scriptSession.GetExceptionString(exc));
456+
scriptSession.GetExceptionString(exc, ScriptSession.ExceptionStringFormat.Html));
457457
Context.ClientPage.ClientResponse.SetInnerHtml("Result", result);
458458
}
459459
if (settings.SaveLastScript)
@@ -618,7 +618,7 @@ protected void ExecuteInternal(params object[] parameters)
618618
result += scriptSession.Output.ToHtml();
619619
}
620620
result += string.Format("<pre style='background:red;'>{0}</pre>",
621-
scriptSession.GetExceptionString(exc));
621+
scriptSession.GetExceptionString(exc, ScriptSession.ExceptionStringFormat.Html));
622622

623623
Context.Job.Status.Result = result;
624624
JobContext.PostMessage("ise:updateresults");

Cognifide.PowerShell/Core/Host/ScriptSession.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,27 @@ namespace Cognifide.PowerShell.Core.Host
2323
{
2424
public class ScriptSession : IDisposable
2525
{
26+
public enum ExceptionStringFormat
27+
{
28+
Default,
29+
Html,
30+
Console
31+
}
32+
2633
private const string HtmlExceptionFormatString =
2734
"<div class='white-space:normal; width:70%;'>{1}</div>{0}<strong>Of type</strong>: {3}{0}<strong>Stack trace</strong>:{0}{2}{0}";
2835

2936
private const string HtmlInnerExceptionPrefix = "{0}<strong>Inner Exception:</strong>{1}";
30-
private const string LineEndformat = "\n";
37+
private const string HtmlLineEndFormat = "<br/>";
3138

3239
private const string ConsoleExceptionFormatString =
3340
"[[;#f00;#000]Exception: {0}]\r\n" + "[[;#f00;#000]Type: {1}]\r\n" + "[[;#f00;#000]Stack trace:\r\n{2}]";
3441

35-
private const string ConsoleInnerExceptionPrefix = "\r\n[[b;#fff;#F00]Inner ] {0}";
42+
private const string ConsoleInnerExceptionPrefix = "{0}[[b;#fff;#F00]Inner ] {1}";
43+
private const string ConsoleLineEndFormat = "\r\n";
44+
45+
private const string ExceptionFormatString = "{1}{0}Of type: {3}{0}Stack trace:{0}{2}{0}";
46+
private const string ExceptionLineEndFormat = "\r\n";
3647

3748
public static Dictionary<string, string> RenamedCommandlets = new Dictionary<string, string>
3849
{
@@ -227,7 +238,7 @@ public void Initialize(bool reinitialize)
227238
runspace.SessionStateProxy.SetVariable("ScriptSession", this);
228239
if (PsVersion == null)
229240
{
230-
PsVersion = (Version) ExecuteScriptPart("$PSVersionTable.PSVersion", false, true)[0];
241+
PsVersion = (Version)ExecuteScriptPart("$PSVersionTable.PSVersion", false, true)[0];
231242
}
232243

233244
var sb = new StringBuilder(2048);
@@ -334,7 +345,7 @@ private List<object> ExecuteCommand(bool stringOutput, bool internalScript, Pipe
334345

335346
if (execResults != null && execResults.Any())
336347
{
337-
foreach (var record in execResults.Select(p=> p.BaseObject).OfType<ErrorRecord>().Select(result => result))
348+
foreach (var record in execResults.Select(p => p.BaseObject).OfType<ErrorRecord>().Select(result => result))
338349
{
339350
Log.Error(record + record.InvocationInfo.PositionMessage, this);
340351
}
@@ -350,34 +361,32 @@ private void PipelineStateChanged(object sender, PipelineStateEventArgs e)
350361
{
351362
}
352363

353-
public string GetExceptionString(Exception exc)
364+
public string GetExceptionString(Exception ex, ExceptionStringFormat format = ExceptionStringFormat.Default)
354365
{
355-
var exception = String.Empty;
356-
exception += String.Format(
357-
HtmlExceptionFormatString,
358-
LineEndformat,
359-
exc.Message,
360-
exc.StackTrace.Replace("\n", LineEndformat),
361-
exc.GetType());
362-
if (exc.InnerException != null)
366+
var stacktrace = ex.StackTrace;
367+
var exceptionPrefix = String.Empty;
368+
var exceptionFormat = ExceptionFormatString;
369+
var lineEndFormat = ExceptionLineEndFormat;
370+
switch (format)
363371
{
364-
exception += String.Format(HtmlInnerExceptionPrefix, LineEndformat,
365-
GetExceptionString(exc.InnerException));
372+
case ExceptionStringFormat.Html:
373+
lineEndFormat = HtmlLineEndFormat;
374+
stacktrace = stacktrace.Replace("\n", lineEndFormat);
375+
exceptionPrefix = HtmlInnerExceptionPrefix;
376+
exceptionFormat = HtmlExceptionFormatString;
377+
break;
378+
case ExceptionStringFormat.Console:
379+
lineEndFormat = ConsoleLineEndFormat;
380+
stacktrace = stacktrace.Replace("[", "%((%").Replace("]", "%))%");
381+
exceptionPrefix = ConsoleInnerExceptionPrefix;
382+
exceptionFormat = ConsoleExceptionFormatString;
383+
break;
366384
}
367-
return exception;
368-
}
369-
370-
public string GetExceptionConsoleString(Exception exc)
371-
{
372385
var exception = String.Empty;
373-
exception += String.Format(
374-
ConsoleExceptionFormatString,
375-
exc.Message,
376-
exc.GetType(),
377-
exc.StackTrace.Replace("[", "%((%").Replace("]", "%))%"));
378-
if (exc.InnerException != null)
386+
exception += String.Format(exceptionFormat, lineEndFormat, ex.Message, stacktrace, ex.GetType());
387+
if (ex.InnerException != null)
379388
{
380-
exception += String.Format(ConsoleInnerExceptionPrefix, GetExceptionConsoleString(exc.InnerException));
389+
exception += String.Format(exceptionPrefix, lineEndFormat, GetExceptionString(ex.InnerException));
381390
}
382391
return exception;
383392
}

Cognifide.PowerShell/sitecore modules/PowerShell/Services/PowerShellWebService.asmx.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public object ExecuteCommand(string guid, string command, string stringFormat)
124124
result = output +
125125
"\r\n[[;#f00;#000]Ooops, something went wrong... Do you need assistance?]\r\n" +
126126
"[[;#f00;#000]Send an email with the stack trace to adam@najmanowicz.com or contact me on Twitter @AdamNaj]\r\n\r\n" +
127-
session.GetExceptionConsoleString(ex) + "\r\n",
127+
session.GetExceptionString(ex, ScriptSession.ExceptionStringFormat.Console) + "\r\n",
128128
prompt = string.Format("PS {0}>", session.CurrentLocation)
129129
});
130130
}
@@ -167,6 +167,7 @@ protected void RunJob(ScriptSession session, string command)
167167
job.Status.Messages.Add("Ooops, something went wrong... Do you need assistance?");
168168
job.Status.Messages.Add(
169169
"Send an email with the stack trace to adam@najmanowicz.com or contact me on Twitter @AdamNaj");
170+
job.Status.Messages.Add(session.GetExceptionString(ex));
170171
job.Status.LogException(ex);
171172
}
172173
else

0 commit comments

Comments
 (0)