Script Errors

Script errors occur in a few given unfortunate circumstances - fortunately, they usually only require some minor changes in order to accommodate issues with browser security or Javascript error handling.

For more in-depth information on Script errors, see our blog post.


Cross Domain Scripts

Your Own Scripts Hosted on Another Domain

Enable CORS for your cross-origin hosted scripts. The MDN documentation provides further explanation of how to implement this.

Add the appropriate Access-Control-Allow-Origin header.

IIS7 example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

Third Party Scripts Hosted on Another Domain

Add the crossorigin="anonymous" attribute to the <script> tag:

<script crossorigin="anonymous" src="http://anotherdomain/foo.js" ></script>

This means that error information will be exposed and therefore available to Raygun4JS.


Unhandled Promise Rejections

It’s possible for a Script error to occur because of unhandled promise rejections. An unhandled promise rejection appears because each and every promise is expected to have a .catch(...) statement.

UnhandledPromiseRejection Example:

The captureUnhandledRejections flag is set to true by default. If this is enabled it will automatically send unhandled exceptions to Raygun. For captureUnhandledRejections browser support, please refer to the MDN documentation

This can be disabled by setting the captureUnhandledRejections option to false inside the Raygun configuration.

<script type="text/javascript">
     rg4js('options', {
       captureUnhandledRejections: false 
     });
 </script>

The following example will yield a Script error if the captureUnhandledRejections option is enabled and a .catch(...) has not been added.

new Promise(function(resolve, reject) {
  setTimeout(() => {
    if (someVariable === 0) {
      reject();
    } else {
      resolve();
    }
  }, 1000);
});

Solution 1: Add a new error object inside reject() with a message.

new Promise(function(resolve, reject) {
  setTimeout(() => {
    if (someVariable === 0) {
      reject(new Error('fail'));
    } else {
      resolve();
    }
  }, 1000);
});

Solution 2: Add .catch() statements to every Promise with a reject() call

var promiseTest = new Promise(function(resolve, reject) {
  setTimeout(() => {
    if (someVariable === 0) {
      reject(new Error('fail'));
    } else {
      resolve();
    }
  }, 1000);
});

promiseTest.catch(function(errorMessage) {
  console.error(errorMessage);
})

Manually Sending Errors

If you’re sending anything manually, make sure that the error you’re sending is valid. Below is a working example of correctly manually sending errors.

try {
  return JSON.parse(someQuestionableString);
} catch (e) {
   rg4js('send', {
    error: e,
    tags: ["handled exception"]
  });
}

Below are some common pitfalls that cause Script errors to be sent to Raygun.

Error 1: having no error

rg4js('send', {
  error: null,
});

Error 2: having no message

rg4js('send', {
  error: new Error(),
});