JavaScript error handling

When writing the JavaScript code for your EdgeWorkers functions consider the areas where errors are likely to occur. You should try to configure fallback options to handle these potential errors inside of the JavaScript code.

📘

Errors not caught in JavaScript can be detected in Property Manager and handled through Site Failover behaviors. See Configure Site Failover for more information.

Try/catch blocks

You can write try/catch blocks in your JavaScript code to handle exceptions and catch runtime errors. When an error occurs in the "try" block, the code in the "catch" block will log the error and can perform the appropriate logic to handle the error. These errors are also caught in the logs and can help future troubleshooting efforts.

export function onClientRequest (request) {
  try {
    // ...
    // Code that might fail
    // ...
  } catch(error) {
    logger.error (error.toString())
    // ...
    // Error handling logic
    // ...
  }
}

Rejected promises

Handling errors in asynchronous code is different. Errors in asynchronous code implemented through promises will result in a rejected promise rather than a catchable runtime error. The code below shows how to use the then() and catch() methods to handle both a successfully resolved promise and a rejected promise.

export function onClientRequest (request) {
  var response = httpRequest('https://www.example.com/')
    .then ((response) => {
      // Action to take on successful completion of HTTP request
    log.info('handling example.com response');
    })
    .catch ((error) => {
      logger.error(error.toString());
      // Action to take on unsuccessful completion of HTTP request
    });
}

Async/Await

The code for handling rejected promises is driven by the JavaScript specifications.

In many cases, you can use the async/await pattern to simplify this code. Async/await is syntactic sugar. It lets you catch errors in the more familiar try/catch syntax by using the Async/await pattern in an awaited promise.

For more information you can review this async/await tutorial.

The code below is functionally equivalent to the previous rejected promises example. Internally, the JavaScript runtime creates the necessary handlers to execute code when the promise returned by httpRequest() function is resolved or rejected.

export async function onClientRequest (request) {
  try {
    var response = await httpRequest('https://www.example.com/');
    // Action to take on successful completion of HTTP request
    log.info('handling example.com response');

  } catch(error) {
    logger.error(error.toString());
    // Action to take on unsuccessful completion of HTTP request
  }
}
👍

As a best practice you should only use try/catch to catch errors that you expect to handle in your JavaScript code. If you catch errors without properly handling the error, you will prevent the error from propagating. Thus you will prevent further error handling logic from being able to address the error.

Use DataStream 2 to deliver uncaught JavaScript exceptions in JavaScript logs

If there is no try-catch block and the log-uncaught-exceptions field is set to true in the logging config inside the bundle.json, the exception will be logged and streamed to the specified ds2 endpoint.

Follow the steps in Use DataStream 2 to deliver JavaScript logs to use DataStream 2 to deliver JavaScript logs to an external endpoint. JavaScript logging captures log messages, including uncaught JavaScript exceptions generated by your EdgeWorkers functions during the current request. The following steps are required to log uncaught JavaScript exceptions.

  1. Add the logging parameter to the bundle.json file. You can set the EdgeWorkers log level in the code bundle. By default, EdgeWorkers logs are set to ERROR.
  2. Add log-uncaught-exceptions to enable logging for uncaught JavaScript exceptions inside bundle.json.
{
    "edgeworker-version": "0.3",
    "description" : "Hello World Observability Example",
    "config": {
    "logging": {
        "level":"warn",
        "schema": "v1",
        "ds2id": 12345,
        "log-uncaught-exceptions": true
    }
  }
}

Did this page help you?