Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use addError to set parent exceptions instead of .exception #370

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
]
},
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 2019
}
}
14 changes: 1 addition & 13 deletions packages/core/lib/segments/attributes/subsegment.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,8 @@ Subsegment.prototype.addError = function addError(err, remote) {

this.addFaultFlag();

if (this.segment && this.segment.exception) {
if (err === this.segment.exception.ex) {
this.fault = true;
this.cause = { id: this.segment.exception.cause };
return;
}
delete this.segment.exception;
}

if (this.segment) {
this.segment.exception = {
ex: err,
cause: this.id
};
this.segment.addError(err, remote, this.id);
} else {
//error, cannot propagate exception if not added to segment
}
Expand Down
30 changes: 13 additions & 17 deletions packages/core/lib/segments/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Segment.prototype.init = function init(name, rootId, parentId) {
} else {
traceId = new TraceID();
}

var id = crypto.randomBytes(8).toString('hex');
var startTime = SegmentUtils.getCurrentTime();

Expand Down Expand Up @@ -235,28 +235,26 @@ Segment.prototype.removeSubsegment = function removeSubsegment(subsegment) {
* @param {boolean} [remote] - Flag for whether the exception caught was remote or not.
*/

Segment.prototype.addError = function addError(err, remote) {
Segment.prototype.addError = function addError(err, remote, id) {
if (err == null || typeof err !== 'object' && typeof(err) !== 'string') {
throw new Error('Failed to add error:' + err + ' to subsegment "' + this.name +
'". Not an object or string literal.');
}

this.addFaultFlag();

if (this.exception) {
if (err === this.exception.ex) {
this.cause = { id: this.exception.cause };
delete this.exception;
return;
}
delete this.exception;
}

if (this.cause === undefined) {
this.cause = {
working_directory: process.cwd(),
exceptions: []
};
if (id) {
this.cause = {
id,
exceptions: []
};
} else {
this.cause = {
working_directory: process.cwd(),
exceptions: []
};
}
}

this.cause.exceptions.push(new CapturedException(err, remote));
Expand Down Expand Up @@ -342,7 +340,6 @@ Segment.prototype.close = function(err, remote) {
this.addError(err, remote);

delete this.in_progress;
delete this.exception;

if (this.counter <= 0) {
this.flush();
Expand All @@ -355,7 +352,6 @@ Segment.prototype.close = function(err, remote) {

Segment.prototype.flush = function flush() {
if (this.notTraced !== true) {
delete this.exception;

var thisCopy = Utils.objectWithoutProperties(
this,
Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/unit/env/aws_lambda.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Segment = require('../../../lib/segments/segment');
var SegmentUtils = require('../../../lib/segments/segment_utils');
var SegmentEmitter = require('../../../lib/segment_emitter');
const TraceID = require('../../../lib/segments/attributes/trace_id');
const { captureAsyncFunc } = require('../../../lib');

describe('AWSLambda', function() {
var sandbox;
Expand Down Expand Up @@ -41,7 +42,7 @@ describe('AWSLambda', function() {
});

describe('#init', function() {
var disableReusableSocketStub, populateStub, sandbox, setSegmentStub, validateStub;
let disableReusableSocketStub, populateStub, sandbox, setSegmentStub, validateStub, disableCentralizedSamplingStub;

beforeEach(function() {
sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -81,6 +82,19 @@ describe('AWSLambda', function() {
assert.equal(facade.trace_id, TraceID.Invalid().toString());
});

describe('subsegment exceptions are added by method', function () {

it('can wrap a subsegment', function () {
const error = new Error('some error');
Lambda.init();
var facade = setSegmentStub.args[0][0];
const subSegment = facade.addNewSubsegment('subsegment');
subSegment.addError(error);
const asString = JSON.stringify(error);
assert.isDefined(asString);
});
});

describe('the facade segment', function() {
afterEach(function() {
populateStub.returns(true);
Expand Down
11 changes: 3 additions & 8 deletions packages/core/test/unit/segments/segment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,11 @@ describe('Segment', function() {
addErrorStub.should.have.not.been.called;
});

it('should delete properties "in_progress" and "exception"', function() {
it('should delete properties "in_progress"', function() {
segment.in_progress = true;
segment.exception = err;
segment.close();

assert.notProperty(segment, 'in_progress');
assert.notProperty(segment, 'exception');
});

it('should flush the segment on close', function() {
Expand Down Expand Up @@ -406,12 +404,11 @@ describe('Segment', function() {
});

describe('#flush', function() {
var err, sandbox, segment;
var sandbox, segment;

beforeEach(function() {
sandbox = sinon.createSandbox();
segment = new Segment('test');
err = new Error('Test error');
});

afterEach(function() {
Expand All @@ -420,18 +417,16 @@ describe('Segment', function() {
});

describe('if traced', function() {
it('should remove properties "notTraced", "counter" and "exception"', function() {
it('should remove properties "notTraced", and "counter"', function() {
var sendStub = sandbox.stub(SegmentEmitter, 'send');
segment.notTraced = false;
segment.in_progress = true;
segment.exception = err;
segment.counter = 1;

segment.flush();

sendStub.should.have.been.calledOnce;
var sentSegment = sendStub.lastCall.args[0];
assert.notProperty(sentSegment, 'exception');
assert.notProperty(sentSegment, 'counter');
assert.notProperty(sentSegment, 'notTraced');
});
Expand Down