Skip to content

Commit

Permalink
Merge pull request #67 from MindscapeHQ/inner-error-support
Browse files Browse the repository at this point in the history
Inner error support
  • Loading branch information
UberMouse authored Jun 18, 2018
2 parents 361f2c6 + cf35f8c commit f0cb704
Show file tree
Hide file tree
Showing 9 changed files with 2,220 additions and 234 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- "6.1.0"
- "4.1"
- "0.12"
- "0.11"
- "0.10"
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.10.0
- Add support for inner errors. Option `innerErrorFieldName` to specify a field or a function on the error object to use for retrieval of an inner error. Defaults to `cause` which is used in [VError](https://github.com/joyent/node-verror)

## 0.9.1

- Add an option to report column number for each frame of the stack trace
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ Release 0.3 previously had a setUser function that accepted a string or function

Call setVersion(*string*) on a RaygunClient to set the version of the calling application. This is expected to be of the format x.x.x.x, where x is a positive integer. The version will be visible in the dashboard.

### Inner Errors

Starting from 0.10.0 support for inner errors was added. Provide option `innerErrorFieldName` to specify a field or a function on the error object to use for retrieval of an inner error. Inner errors will be retrieved recursively until there is no more errors. Option `innerErrorFieldName` defaults to `cause` which is used in [VError](https://github.com/joyent/node-verror), therefore `VError` is supported out of the box.

### Changing the API endpoint

You can change the endpoint that error messages are sent to by specifying the `host`, `port`, and `useSSL` properties in the `raygunClient.init()` options hash. By default, `host` is `api.raygun.io`, `port` is `443`, and `useSSL` is `true`.
Expand Down
5 changes: 3 additions & 2 deletions lib/raygun.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var MessageBuilder = require('./raygun.messageBuilder');
var OfflineStorage = require('./raygun.offline');

var Raygun = function () {
var _apiKey, _filters, raygun = this, _user, _version, _host, _port, _useSSL, _onBeforeSend, _offlineStorage, _isOffline, _offlineStorageOptions, _groupingKey, _tags, _useHumanStringForObject, _reportColumnNumbers;
var _apiKey, _filters, raygun = this, _user, _version, _host, _port, _useSSL, _onBeforeSend, _offlineStorage, _isOffline, _offlineStorageOptions, _groupingKey, _tags, _useHumanStringForObject, _reportColumnNumbers, _innerErrorFieldName;

raygun.init = function (options) {
_apiKey = options.apiKey;
Expand All @@ -31,6 +31,7 @@ var Raygun = function () {
_tags = options.tags;
_useHumanStringForObject = options.useHumanStringForObject === undefined ? true : options.useHumanStringForObject;
_reportColumnNumbers = options.reportColumnNumbers;
_innerErrorFieldName = options.innerErrorFieldName || 'cause'; // VError function to retrieve inner error;

if (_isOffline) {
_offlineStorage.init(_offlineStorageOptions);
Expand Down Expand Up @@ -94,7 +95,7 @@ var Raygun = function () {
mergedTags = mergedTags.concat(tags);
}

var builder = new MessageBuilder({filters: _filters, useHumanStringForObject: _useHumanStringForObject, reportColumnNumbers: _reportColumnNumbers})
var builder = new MessageBuilder({filters: _filters, useHumanStringForObject: _useHumanStringForObject, reportColumnNumbers: _reportColumnNumbers, innerErrorFieldName: _innerErrorFieldName})
.setErrorDetails(exception)
.setRequestDetails(request)
.setMachineName()
Expand Down
61 changes: 39 additions & 22 deletions lib/raygun.messageBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,44 @@ function filterKeys(obj, filters) {
return obj;
}

function getStackTrace(error, options) {
var stack = [];
var trace = stackTrace.parse(error);

trace.forEach(function (callSite) {
var frame = {
lineNumber: callSite.getLineNumber(),
className: callSite.getTypeName() || 'unknown',
fileName: callSite.getFileName(),
methodName: callSite.getFunctionName() || '[anonymous]'
};

if (!!options.reportColumnNumbers && typeof (callSite.getColumnNumber) === 'function') {
frame.columnNumber = callSite.getColumnNumber();
}

stack.push(frame);
});

return stack;
}

function buildError(error, options) {
var builtError = {
stackTrace: getStackTrace(error, options),
message: error.message || "NoMessage",
className: error.name
};

var innerError = typeof error[options.innerErrorFieldName] === 'function' ? error[options.innerErrorFieldName]() : error[options.innerErrorFieldName];

if(innerError instanceof Error) {
builtError.innerError = buildError(innerError, options);
}

return builtError;
}

var RaygunMessageBuilder = function (options) {
options = options || {};
var _filters;
Expand Down Expand Up @@ -63,28 +101,7 @@ var RaygunMessageBuilder = function (options) {
return this;
}

var stack = [];
var trace = stackTrace.parse(error);
trace.forEach(function (callSite) {
var frame = {
lineNumber: callSite.getLineNumber(),
className: callSite.getTypeName() || 'unknown',
fileName: callSite.getFileName(),
methodName: callSite.getFunctionName() || '[anonymous]'
};

if (!!options.reportColumnNumbers && typeof (callSite.getColumnNumber) === 'function') {
frame.columnNumber = callSite.getColumnNumber();
}

stack.push(frame);
});

message.details.error = {
stackTrace: stack,
message: error.message || "NoMessage",
className: error.name
};
message.details.error = buildError(error, options);

return this;
};
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "raygun",
"description": "Raygun.io plugin for Node",
"version": "0.9.1",
"version": "0.10.0",
"homepage": "https://github.com/MindscapeHQ/raygun4node",
"author": {
"name": "MindscapeHQ",
Expand Down Expand Up @@ -30,9 +30,11 @@
"devDependencies": {
"jshint": "^2.5.6",
"semver": "^5.0.3",
"tap": "^7.1.2"
"tap": "^7.1.2",
"verror": "^1.10.0"
},
"dependencies": {
"nock": "~8",
"object-to-human-string": "0.0.3",
"stack-trace": "0.0.6"
},
Expand Down
Loading

0 comments on commit f0cb704

Please sign in to comment.