Logging errors from the client-side
Tags: javascript, apex, orclapex
Published: 2021-01-01 16:29:13

---
title: Logging errors from the client-side
published: true
date: 2021-01-01 16:13:55 UTC
tags: Javascript,APEX,ORCLAPEX
canonical_url: 
---

 ![](https://micodify.de/media/posts/15/nathan-dumlao-a3ra9eXUjvo-unsplash.jpg)

APEX and the Oracle database offers us a lot of possibilities to store errors or log outputs in the DB. Starting with the popular OraOpenSource framework logger up to the APEX own processing by apex\_debug. With both options, the error or log output is created and processed in the database. But what about errors that occur in the browser? For example, if a dynamic action hits an unhandled exception via JavaScript?

* * *

For this, most browsers provide us with the onError event. This is called whenever there is an unhandled exception. How convenient! Let's take advantage of this and implement the actual logging of the error here via an APEX server process.

First we will create a DynamikAction on page 0 (Global Page). This page has a special function. Everything that is stored here is available on every page. So will our future error handling.

![](https://micodify.de/media/posts/15/page-builder-01)

As True-Action (Execute JavaScript Code) we deposit the following code:

```JavaScript
window.onerror = function (msg, url, lineNo, columnNo, error) {  
  apex.server.process('LOG_ERROR',
    {
      x01: msg ,
      x02: url,
      x03: lineNo,
      x04: columnNo
    },
    {
      dataType: "text",
      success: function (pData) {
        // Error logged, let the User see some message
        apex.message.clearErrors();
        apex.message.showErrors([
            {
                type: "error",
                location: "page",
                message: "Warning, an unhandled error occured, please contact an administrator!",
                unsafe: false
            }
        ]);
      }
    }
  );

  return false;
}
```

Here we register a function that calls our LOG\_ERROR process, which is yet to be implemented, in the event of an error. We pass the parameters with the respective information about the error that occurred (message, URL, row number, column number).  
  
Then we create a server process to be called via AJAX callback. Here we implement our actual logging. For example, we use OraOpenSource-Logger for logging.

![](https://micodify.de/media/posts/15/application-process-02)

With this method, we will no longer miss any client-side errors. If you think about the whole thing a bit further, the error can be transferred directly into the own ticket management. Before doing so, however, you should check whether the error has not already been transferred, so it is sufficient if only one counter is incremented here if the same error occurs several times, otherwise the number of error tickets can of course increase enormously.

![](https://micodify.de/media/posts/15/console-output-03)

![](https://micodify.de/media/posts/15/query-output-04)

With this method, you no longer miss errors that appear on the client side / in the browser. And only errors that are reported to us and those that we "see" we can fix as developers.

Feature-Image: Photo by [Nathan Dumlao](https://unsplash.com/@nate_dumlao?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/error?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)