Skip to content

Instantly share code, notes, and snippets.

@jstott
Last active June 9, 2020 14:55
Show Gist options
  • Save jstott/7158758 to your computer and use it in GitHub Desktop.
Save jstott/7158758 to your computer and use it in GitHub Desktop.
Durandal plugin (durandal spa framework) for raygun.io (error exception tracking) This assumes you've loaded the raygun.js script in your main page or elsewhere: see https://github.com/MindscapeHQ/raygun4js All system.error() and unhanded exceptions will be recorded via Raygun.
// fragment from main.js
// specify which plugins to install and their configuration v2.0
// assumes raygun4js is loaded
app.configurePlugins({
router: true,
rayGun: {
enable: true, // !system.debug,
errorOption: { systemError: true, suppress: true },
apiKey: 'your-apikey-here',
version: 'x.y.z',
customData: { name: 'someName' }
}
});
/**
* Integrates Raygun real time error tracking within Durandal, optionally (re)mapping system.error.
* @module rayGun
* @requires system
* @example
// sample configuration from main.js
app.configurePlugins({
... other plugins ...
rayGun: {
enable: true, // !system.debug,
errorOption: { systemError: true, suppress: true },
apiKey: 'your-apikey-here',
version: 'x.y.z',
customData: { name: 'someName' }
}
*/
define(['durandal/system'], function (system) {
var _error = system.error, // if duck-punching system.error
noop = system.noop,
/**
* @class rayGun
* @static
*/
rayGun = {
/**
* Returns boolean for active state of logger
* @property {Boolean} isEnabled
*/
isEnabled: false,
/**
* submit manual errors
* @method send
* @param {Error} Error error being thrown
* @param {object} extra Extra object information to attach to the error log
*/
send: noop,
/**
* provide the user name or email address of the currently logged in user
* @method setUser
* @param {String} user User email or name
*/
setUser: noop,
/**
* set version for log filtering & build identification
* @method setVersion
* @param {String} version Version string used to identify & filter from the error logs.
*/
setVersion: noop,
/**
* detach the logger
* @method detach
*/
detach: noop,
/**
* Installs rayGun's custom handlers and sets up system.error mapping if enabled.
* @method install
* @param {object} config Config options for rayGun operation
* @example
// durandal fragment from main.js
app.configurePlugins({
... other plugins ...
rayGun: {
enable: true, // !system.debug,
errorOption: { systemError: true, suppress: true },
apiKey: 'your-apikey-here',
version: 'app.version.here',
customData: { name: 'someName' }
}
*/
install: function (config) {
var self = this,
logger = window.Raygun,
errorOption = defValue(config.errorOption, { error: true, suppress: true }),
enabled = defValue(config.enable, true),
customData = defValue(config.customData, {});
if (!enabled) {
system.log('Plugin:Disabled plugins/rayGun');
return;
}
if (!logger) {
abortPlugin('Raygun "raygun4js" library is missing or not loaded');
return;
}
if (!config.apiKey) {
abortPlugin('Raygun apiKey not defined');
return;
}
configure();
mapSystemError();
this.isEnabled = true;
function configure() {
logger.init(config.apiKey, system.debug, customData).attach();
if (config.version) {
logger.setVersion(config.version);
}
if (config.user) {
logger.setUser(config.user);
}
self.send = logger.send;
self.setUser = logger.setUser;
self.setVersion = logger.setVersion;
self.detach = function () {
if (errorOption.systemError && errorOption.suppress) {
system.error = _error;
}
logger.detach();
};
}
function mapSystemError() {
if (errorOption.systemError) {
system.error = function (error, extra) {
var err = (error instanceof Error) ? error : new Error(error),
custom = merge(customData, extra);
system.log("Plugin:rayGun", { source: "system.error", 'error': err, custom: custom });
if (errorOption.suppress) {
logger.send(err, custom);
} else {
throw err;
}
};
}
}
}
};
return rayGun;
/* internals */
function abortPlugin(errorMsg) {
_error(new Error(errorMsg));
}
// helper allows 'falsey' values for default values - so check for undefined vs more typical x || 'dftValue' style
function defValue(source, dftValue) {
return (typeof source !== 'undefined' ? source : dftValue);
}
function merge(o1, o2) {
var a, o3 = {};
for (a in o1) { o3[a] = o1[a]; }
for (a in o2) { o3[a] = o2[a]; }
return o3;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment