Skip to content

Commit

Permalink
fix: handle null/undefined as last query value in mysql and postgres …
Browse files Browse the repository at this point in the history
…patchers (#81)

* fix: handle null/undefined as last query value

Was beforehand throwing a TypeError when trying to access the constructor property on `null` or `undefined`:

```js
connection.query('SELECT a FROM b', null);
// without xray --> resolves query
// with xray    --> throws TypeError: Cannot read property 'constructor' of null  
```

As far as I know null and undefined are the only data types that will encounter this problem, hence a loose null check should suffice.

* fix: handle null/undefined as last query value in postgres

* test: add test cases for sending null in last query parameter
  • Loading branch information
M-TGH authored and haotianw465 committed Dec 13, 2018
1 parent 5c3c6ff commit fb0e4e5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/mysql/lib/mysql_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function resolveArguments(argsObj) {
args.callback = typeof argsObj[1] === 'function' ? argsObj[1] : (typeof argsObj[2] === 'function' ? argsObj[2] : undefined);
}

args.segment = (argsObj[argsObj.length-1].constructor && (argsObj[argsObj.length-1].constructor.name === 'Segment' ||
args.segment = (argsObj[argsObj.length-1] != null && argsObj[argsObj.length-1].constructor && (argsObj[argsObj.length-1].constructor.name === 'Segment' ||
argsObj[argsObj.length-1].constructor.name === 'Subsegment')) ? argsObj[argsObj.length-1] : null;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/mysql/test/unit/mysql_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ describe('captureMySQL', function() {

stubClose.should.have.been.calledWithExactly(err);
});

it('should start a new automatic context when last query paramater is null', function() {
query.call(connectionObj, 'sql here', function() {}, null);

assert.equal(stubBaseQuery.args[0][2].name, 'autoContext');
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/postgres/lib/postgres_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function resolveArguments(argsObj) {
args.callback = typeof argsObj[1] === 'function' ? argsObj[1] : (typeof argsObj[2] === 'function' ? argsObj[2] : undefined);
}

args.segment = (argsObj[argsObj.length-1].constructor && (argsObj[argsObj.length-1].constructor.name === 'Segment' ||
args.segment = (argsObj[argsObj.length-1] != null && argsObj[argsObj.length-1].constructor && (argsObj[argsObj.length-1].constructor.name === 'Segment' ||
argsObj[argsObj.length-1].constructor.name === 'Subsegment')) ? argsObj[argsObj.length-1] : null;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/postgres/test/unit/postgres_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ describe('capturePostgres', function() {

stubClose.should.have.been.calledWithExactly(err);
});

it('should start a new automatic context when last query paramater is null', function() {
query.call(postgres, 'sql here', [], function() {}, null);

assert.equal(queryObj.callback.name, 'autoContext');
});
});


Expand Down

0 comments on commit fb0e4e5

Please sign in to comment.