Skip to content

ExceptionWidget

IExceptionWidget pretty-prints an Exception. Class name, message, and stack frames each get their own style; the trace tokeniser splits each frame into method / parameter type / parameter name / parenthesis / path / line-number tokens you can style independently.

When to use

  • Crash reporting in long-running CLIs.
  • Better-than-default error rendering inside try / except.
  • Capturing styled stack traces for Recorder HTML export.

Basic usage

pascal
try
  raise EInOutError.Create('disk full');
except
  on E : Exception do
    AnsiConsole.Write(
      Widgets.ExceptionWidget(E)
        .WithStackTrace(
          'MyApp.Storage.Save in C:\src\MyApp\Storage.pas:142' + sLineBreak +
          'MyApp.Worker.Run  in C:\src\MyApp\Worker.pas:56'    + sLineBreak +
          'MyApp.Main')
        .WithFormats([TExceptionFormat.ShortenPaths,
                      TExceptionFormat.ShortenMethods]));
end;

The trace string follows the convention <method> in <path>:<lineno> per line. Delphi's Exception class doesn't carry a structured trace, so the caller supplies it - typically from madExcept, JclDebug, EurekaLog, or a hand-rolled walker.

Configuration

MethodPurpose
WithStackTrace(value)Multi-line trace string.
WithFormats(values)Set of TExceptionFormat flags.
WithStyle(value : IExceptionStyle)Per-token style sheet.
WithClassNameStyle(value) / WithMessageStyle(value) / WithFrameStyle(value)Backwards-compatible per-section setters.

TExceptionFormat flags:

FlagEffect
ShortenPathsExtractFileName(path) on emit.
ShortenTypes'A.B.Type.Method' -> 'Type.Method'.
ShortenMethods'A.B.Type.Method' -> 'Method'.
ShowLinksWrap path tokens in OSC 8 file:// links.
NoStackTraceSkip the trace block entirely.

Per-token style sheet

Build a richer style via Widgets.ExceptionStyle:

pascal
AnsiConsole.Write(
  Widgets.ExceptionWidget(E)
    .WithStyle(
      Widgets.ExceptionStyle
        .WithMethod(TAnsiStyle.Plain.WithForeground(TAnsiColor.Aqua))
        .WithPath(TAnsiStyle.Plain.WithForeground(TAnsiColor.Grey))
        .WithLineNumber(TAnsiStyle.Plain.WithForeground(TAnsiColor.Yellow))));

The full set of styleable tokens: Message, ExceptionType, Method, ParameterType, ParameterName, Parenthesis, Path, LineNumber, Dimmed, NonEmphasized.

Without an Exception instance

For tests / post-mortem dumps where you have raw strings:

pascal
AnsiConsole.Write(
  Widgets.ExceptionWidget('EInOutError', 'disk full')
    .WithStackTrace(traceText));

Convenience: AnsiConsole.WriteException

pascal
on E : Exception do
  AnsiConsole.WriteException(E);

Equivalent to writing a default-styled ExceptionWidget(E) directly.

API reference

See also

Released under the MIT License.