Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Intercept JS errors ?

Post Reply
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

Intercept JS errors ?

Post by thefunkyjoint »

Let's say i run a JS code with a syntax error. In which event i can get the error message ? It seems the error does not trigger the 'onConsoleMessage' event.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Intercept JS errors ?

Post by salvadordf »

Use the GlobalCEFApp.OnUncaughtException event to catch exceptions globally.

That event is triggered in the render process only and you will need to set GlobalCEFApp.UncaughtExceptionStackSize to a value greater than 0 to enable it.

You can also use the "window.onerror" JavaScript event to catch errors and send them to Delphi with a custom JS extension.
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

Re: Intercept JS errors ?

Post by thefunkyjoint »

salvadordf wrote: Tue Feb 12, 2019 11:01 am Use the GlobalCEFApp.OnUncaughtException event to catch exceptions globally.

That event is triggered in the render process only and you will need to set GlobalCEFApp.UncaughtExceptionStackSize to a value greater than 0 to enable it.
In this case a JS error will throw a Delphi exception ?
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Intercept JS errors ?

Post by salvadordf »

thefunkyjoint wrote: Wed Feb 13, 2019 11:21 am
salvadordf wrote: Tue Feb 12, 2019 11:01 am Use the GlobalCEFApp.OnUncaughtException event to catch exceptions globally.

That event is triggered in the render process only and you will need to set GlobalCEFApp.UncaughtExceptionStackSize to a value greater than 0 to enable it.
In this case a JS error will throw a Delphi exception ?
No. The JS error will only trigger the GlobalCEFApp.OnUncaughtException event in the render process.

The "OnUncaughtException" name is just the CEF method name for that event but it has nothing to do with Delphi exceptions.
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

Re: Intercept JS errors ?

Post by thefunkyjoint »

Thanks, is there any example on any demo ?
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Intercept JS errors ?

Post by salvadordf »

I'm afraid not. :oops:

However, it shouldn't be too difficult to extract the information you need from the context, exception and stackTrace parameters.

The API documentation links for those parameters and for ICefV8StackFrame are these : Then you can use the browser parameter to send a message to the main process with the information, like the DOMVisitor demo does :

Code: Select all

browser.SendProcessMessage(PID_BROWSER, msg);
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Intercept JS errors ?

Post by salvadordf »

I just edited my previous post. In case you read the old version, reload this page.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Intercept JS errors ?

Post by salvadordf »

mmontu wrote: Tue Oct 15, 2019 2:51 pm I have tried UncaughtExceptionStackSize with:

Code: Select all

GlobalCEFApp.UncaughtExceptionStackSize := 10;
GlobalCEFApp.OnUncaughtException := MyAppOnUncaughtException;
but seems that it´s not working. MyAppOnUncaughtException() it´s not being executed. There it´s an example with this feature working or there is some issue and should use Window.onerror?
GlobalCEFApp.OnUncaughtException is executed in the render process. You need to use the debugging techniques described here :
https://www.briskbard.com/index.php?lan ... #debugging
mmontu wrote: Tue Oct 15, 2019 3:31 pm I ended up using TChromium.OnConsoleMessage and checking if Level=LOGSEVERITY_ERROR.
I 'got the Message, line and source. :)
I'm glad you could get the information you needed. :)
Post Reply