@@ -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 }
0 commit comments