Webview catch console exceptions/errors

Problem Description

I use the webivew to display different web pages in my electron app. However I want to catch all the incoming inputs inside the webview console.
I have added an event listener console-message for this as specified in the documentation, but it seems that doesn’t catch the exceptions/errors as:
Failed to load resource: the server responded with a status of 502 (Bad Gateway)

Using console-message

Here is the example I use from the official documentation. Error is not caught.

  const webview = document.querySelector('webview');
  webview.addEventListener('console-message', event =>
    trackLogs(`WEBVIEW_LOG_EVENT: ${event.message}`)

Script injection

Here is an example where I try to inject the code, but it seems the error is caught, even before the injected script is called.

  const webview = document.querySelector('webview');
  function contentload() {
      window.onerror = function(error, url, line) {
        `trackErrors({acc:'error',` data:'ERR:'+error+' URL:'+url+' L:'+line});
  webview.addEventListener('did-start-loading', contentload);


How can I catch such inputs from the webview console and send them to my logger service?