Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Exception handling
Fri, Oct 29 2021 6:22 AMPermanent Link

erickengelke

Avatar

Good morning,

My request for today would be to change Exception handling to include the Javascript stack trace in the Exception class.  It would be compatible with existing projects because it's just a new ignorable field.

Debugging browser code right now can be a real pain to find the source of exception unless the programmer rewrites the internal $we() function yourself to add the stack trace.

I'd also recommend a DEBUGGING flag that can turn on exception stack traces in the default handler, maybe just going to the debugging console.

A horrible but effective way to accomplish that today is to just raise an exception inside the default EWB handler, then the browser will put a stack trace into the debugger output. of the true exception as well as a second message for the nasty default handler.

Thanks,
Erick
EWB Programming Books and Component Library
http://www.erickengelke.com
Fri, Oct 29 2021 8:45 PMPermanent Link

erickengelke

Avatar

erickengelke wrote:
> A horrible but effective way to accomplish that today is to just raise an exception inside the default EWB handler,
> then the browser will put a stack trace into the debugger output. of the true exception as well as a second message
> for the nasty default handler.

I've been asked how to use this.  So here's the code, it finds most errors anywhere in your code at runtime.


procedure TForm1.myerror( o : TObject );
begin
 window.alert('Check the console for an error message');
 raise Exception.Create('Ignore this one');
end;

procedure TForm1.Form1Show(Sender: TObject);
var
 cb : TCheckbox;
 err : TNotifyEvent;
begin
 err := myerror;
 application.onerror := err;

 // next line generates an error
 cb.Visible := True;
end;


In Chrome this displays:


test.js:46545 Uncaught TypeError: Cannot read properties of undefined (reading 'tcontrol_setvisible')
   at main_tform1.$p.form1show (test.js:46545)
   at main_tform1.tcontrol_fonshow (test.js:306)
   at main_tform1.webctrls_tcontrol.$p.doshow (test.js:30933)
   at main_tform1.webforms_tformcontrol.$p.doshow (test.js:35096)
   at main_tform1.webctrls_tcontrol.$p.dispatchevent (test.js:30734)
   at webui_tdivelement.webui_telement.$p.triggerevent (test.js:21013)
   at webui_tdivelement.webui_telement.$p.triggercontrollerevent (test.js:21031)
   at webui_tdivelement.webui_telement.$p.handlechanges (test.js:19431)
   at webui_tdivelement.webui_telement.$p.handleupdatechanges (test.js:18762)
   at webui_telements.$p.endupdates (test.js:18213)

And of course test.js: 46545 is exactly the line with the error, in function form1show.   Very handy.

Erick
EWB Programming Books and Component Library
http://www.erickengelke.com
Fri, Oct 29 2021 10:22 PMPermanent Link

erickengelke

Avatar


The following default error handling code works with most browsers: IE and Chrome and Safari,  showing the line number of the error in the error message.   

But it only works with network based HTML/JS, not local disk versions.


function TForm1.showerror( const message: String; const url: String;
                                          line: Integer; col: Integer=0;
                                          exceptionObject: Exception=nil): Boolean;
begin
  window.alert('ERROR: '+message +' @'+IntTostr( line ));
end;

procedure TForm1.Form1Show(Sender: TObject);
var
  cb : TCheckbox;
begin

  window.onerror := showerror;
  // next line generates an error
  cb.Visible := True;
end;
EWB Programming Books and Component Library
http://www.erickengelke.com
Sun, Oct 31 2021 8:50 PMPermanent Link

erickengelke

Avatar

Okay, here's the final code showing the stack trace.  Tested with a bunch of browsers.  This helped me find a spooky Halloween bug quickly that had eluded me for a few days.


type
 external TExternalException =  class
 public
    property stack : string read;
 end;

function TForm1.showerror( const message: String; const url: String;
                                          line: Integer; col: Integer=0;
                                          exceptionObject: Exception=nil): Boolean;
var
 s : string;
 e : TExternalException;
begin

 e := TExternalException( variant(exceptionObject) );
 s := e.stack;
 showmessage('ERROR: '+message +' @'+IntTostr( line )+#13+s);
end;

procedure TForm1.Form1Show(Sender: TObject);
var
 cb : TCheckbox;
begin

 window.onerror := showerror;
 // next line generates an error
 cb.Visible := True;
end;
EWB Programming Books and Component Library
http://www.erickengelke.com
Mon, Nov 1 2021 12:10 AMPermanent Link

Richard Harding

Wise Nutrition Coaching

Great - many thanks Erick.

It works really well..

Richard
Image