Changeset 259 for Network


Ignore:
Timestamp:
Jul 31, 2011, 8:00:28 PM (13 years ago)
Author:
george
Message:
  • Upraveno: Dumping exception to standard output or standard error output.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Network/CoolWeb/WebServer/UWebApp.pas

    r256 r259  
    2727    FOnInitialize: TNotifyEvent;
    2828    procedure DoRun; override;
     29    function DumpExceptionCallStack(E: Exception): string;
    2930    procedure HTTPServerRequest(HandlerData: THTTPHandlerData);
    3031  public
     
    3233    HTTPServer: THTTPServer;
    3334    HTTPSessionStorageFile: THTTPSessionStorageFile;
     35    LogException: Boolean;
     36    procedure ShowException(E: Exception); override;
    3437    procedure RegisterPage(PageClass: TWebPageClass; out Reference; Path: string);
    3538    constructor Create(AOwner: TComponent); override;
     
    5659end;
    5760
     61
    5862{ TRegistredPageList }
    5963
     
    7276procedure TWebApp.DoRun;
    7377begin
    74   if Assigned(FOnInitialize) then FOnInitialize(Self);
    75   HTTPServer.Run;
    76   Terminate;
     78  try
     79    if Assigned(FOnInitialize) then FOnInitialize(Self);
     80    HTTPServer.Run;
     81  finally
     82    Terminate;
     83  end;
     84end;
     85
     86function TWebApp.DumpExceptionCallStack(E: Exception): string;
     87var
     88  I: Integer;
     89  Frames: PPointer;
     90  Report: string;
     91begin
     92  Report := 'Program exception! ' + LineEnding +
     93    'Stacktrace:' + LineEnding + LineEnding;
     94  if E <> nil then begin
     95    Report := Report + 'Exception class: ' + E.ClassName + LineEnding +
     96    'Message: ' + E.Message + LineEnding;
     97  end;
     98  Report := Report + BackTraceStrFunc(ExceptAddr);
     99  Frames := ExceptFrames;
     100  for I := 0 to ExceptFrameCount - 1 do
     101    Report := Report + LineEnding + BackTraceStrFunc(PointerArray(Frames)[I]);
     102  Result := Report;
    77103end;
    78104
     
    109135end;
    110136
     137procedure TWebApp.ShowException(E: Exception);
     138var
     139  hstdout: ^Text;
     140begin
     141  if not LogException then begin
     142    hstdout := @stdout;
     143    WriteLn(hstdout^, 'Content-type: text/html');
     144    WriteLn(hstdout^);
     145    Writeln(hstdout^, 'An unhandled exception occurred: ' + E.Message + '<br>');
     146    WriteLn(hstdout^, StringReplace(DumpExceptionCallStack(E), LineEnding, '<br>', [rfReplaceAll]));
     147  end else begin
     148    hstdout := @stdout;
     149    WriteLn(hstdout^, 'Content-type: text/html');
     150    WriteLn(hstdout^);
     151    WriteLn(hstdout^, 'Error occured during page generation.');
     152    hstdout := @stderr;
     153    Writeln(hstdout^, 'An unhandled exception occurred: ' + E.Message + '<br>');
     154    WriteLn(hstdout^, StringReplace(DumpExceptionCallStack(E), LineEnding, '<br>', [rfReplaceAll]));
     155  end;
     156end;
     157
    111158constructor TWebApp.Create(AOwner: TComponent);
    112159begin
Note: See TracChangeset for help on using the changeset viewer.