Custom JavaScript Errors
gonace
Posted on
Nov 08 2016
Hi I'm trying to get RayGun to identify some custom a custom errors but it's just shown as "Script error", is there a way to solve this?
Here is the code i'm trying out:
// Define custom exception
function ClientException(message) {
this.name = "ClientException";
this.message = message || "";
}
ClientException.prototype = Error.prototype;
// Define payload to send to raygun
var model = {
'Level': options.level || undefined,
'Request': {
'Parameters': options.parameters || null
}
};
// Send payload to raygun
try {
throw new ClientException(options.error);
} catch (e) {
rg4js('send', {
error: e,
customData: model
});
}
This will result in the error being seen as "Script error", is there a way to create these type of custom exceptions and get RayGun to identify them correctly instead of listing them as "Script error"?
Callum
Posted on
Nov 08 2016
I've just tried out your code end-to-end and strangely enough it is working for me. The one thing I changed was to hard-code the options.error, options.parameters and options.level parameters as options was null. Once this was done however, assuming a string is passed into 'throw new ClientException(str)', the payload is created correctly with the Error param containing the ClassName and Message set correctly, and this is transmitted and sent to the Raygun API where it then appears in the dashboard as expected (including the custom data).
One thing to check is that those above three parameters do have valid data, and that the resulting payload is correct. To confirm this you can add an onBeforeSend handler to log the outgoing payload to the console:
rg4js('onBeforeSend', function (payload) {
console.log('OnBeforeSend');
console.log(payload);
return payload;
});
If you see anything odd in Details->Error let us know. Also, what browser are you seeing this behaviour in (I tried the above with Chrome which worked)?