Skip to content

Commit b29bc41

Browse files
committed
Add trace flag for configuring long stack traces
1 parent aa3518c commit b29bc41

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ issue [#501](https://github.com/felixge/node-mysql/issues/501). (Default: `'fals
158158
* `dateStrings`: Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then
159159
inflated into JavaScript Date objects. (Default: `false`)
160160
* `debug`: Prints protocol details to stdout. (Default: `false`)
161+
* `trace`: Generates stack traces on `Error` to include call site of library
162+
entrance ("long stack traces"). Slight performance penalty for most calls.
163+
(Default: `true`)
161164
* `multipleStatements`: Allow multiple mysql statements per query. Be careful
162165
with this, it exposes you to SQL injection attacks. (Default: `false`)
163166
* `flags`: List of connection flags to use other than the default ones. It is

lib/ConnectionConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function ConnectionConfig(options) {
2020
this.bigNumberStrings = options.bigNumberStrings || false;
2121
this.dateStrings = options.dateStrings || false;
2222
this.debug = options.debug;
23+
this.trace = options.trace !== false;
2324
this.stringifyObjects = options.stringifyObjects || false;
2425
this.timezone = options.timezone || 'local';
2526
this.flags = options.flags || '';

lib/protocol/Protocol.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ Protocol.prototype._enqueue = function(sequence) {
105105
return sequence;
106106
}
107107

108+
if (this._config.trace) {
109+
// Long stack trace support
110+
sequence._callSite = new Error;
111+
}
112+
108113
this._queue.push(sequence);
109114

110115
var self = this;

lib/protocol/sequences/Sequence.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ function Sequence(callback) {
99
EventEmitter.call(this);
1010

1111
this._callback = callback;
12+
this._callSite = null;
1213
this._ended = false;
13-
14-
// Experimental: Long stack trace support
15-
this._callSite = new Error;
1614
}
1715

1816
Sequence.determinePacket = function(byte) {
@@ -38,7 +36,12 @@ Sequence.prototype._packetToError = function(packet) {
3836
};
3937

4038
Sequence.prototype._addLongStackTrace = function(err) {
39+
if (!this._callSite) {
40+
return;
41+
}
42+
4143
var delimiter = '\n --------------------\n' ;
44+
4245
if (err.stack.indexOf(delimiter) > -1) {
4346
return;
4447
}
@@ -62,7 +65,7 @@ Sequence.prototype.end = function(err) {
6265
// causes a cyclic reference that the GC does not detect properly, but I was
6366
// unable to produce a standalone version of this leak. This would be a great
6467
// challenge for somebody interested in difficult problems : )!
65-
delete this._callSite;
68+
this._callSite = null;
6669

6770
// try...finally for exception safety
6871
try {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection({trace: false});
3+
var assert = require('assert');
4+
5+
var err;
6+
connection.query('invalid sql', function(_err) {
7+
err = _err;
8+
});
9+
10+
connection.end();
11+
12+
process.on('exit', function() {
13+
assert.ok(err.stack.indexOf(__filename) < 0);
14+
});

test/integration/connection/test-long-stack-traces.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Experimental: https://github.com/felixge/node-mysql/issues/198
2-
31
var common = require('../../common');
42
var connection = common.createConnection();
53
var assert = require('assert');

0 commit comments

Comments
 (0)