Skip to content

Commit

Permalink
trace: don't require throw to add trace info for verbose (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghv authored Apr 28, 2020
1 parent 5c3c682 commit 024bd4f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ function extendTrace(object, property, pos) {
var cb = arguments[pos];
if (typeof arguments[pos] === 'function') {
arguments[pos] = function replacement() {
try {
return cb.apply(this, arguments);
} catch (err) {
if (err && err.stack && !err.__augmented) {
err.stack = filter(err).join('\n');
err.stack += '\n--> in ' + name;
err.stack += '\n' + filter(error).slice(1).join('\n');
err.__augmented = true;
}
throw err;
var err = arguments[0];
if (err && err.stack && !err.__augmented) {
err.stack = filter(err).join('\n');
err.stack += '\n--> in ' + name;
err.stack += '\n' + filter(error).slice(1).join('\n');
err.__augmented = true;
}
return cb.apply(this, arguments);
};
}
return old.apply(this, arguments);
Expand Down
60 changes: 60 additions & 0 deletions test/verbose.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var sqlite3 = require('..');
var assert = require('assert');

var invalid_sql = 'update non_existent_table set id=1';

var originalMethods = {
Database: {},
Statement: {},
};

function backupOriginalMethods() {
for (var obj in originalMethods) {
for (var attr in sqlite3[obj].prototype) {
originalMethods[obj][attr] = sqlite3[obj].prototype[attr];
}
}
}

function resetVerbose() {
for (var obj in originalMethods) {
for (var attr in originalMethods[obj]) {
sqlite3[obj].prototype[attr] = originalMethods[obj][attr];
}
}
}

describe('verbose', function() {
it('Shoud add trace info to error when verbose is called', function(done) {
var db = new sqlite3.Database(':memory:');
backupOriginalMethods();
sqlite3.verbose();

db.run(invalid_sql, function(err) {
assert(err instanceof Error);

assert(
err.stack.indexOf(`Database#run('${invalid_sql}'`) > -1,
`Stack shoud contain trace info, stack = ${err.stack}`
);

done();
resetVerbose();
});
});

it('Shoud not add trace info to error when verbose is not called', function(done) {
var db = new sqlite3.Database(':memory:');

db.run(invalid_sql, function(err) {
assert(err instanceof Error);

assert(
err.stack.indexOf(invalid_sql) === -1,
`Stack shoud not contain trace info, stack = ${err.stack}`
);

done();
});
});
});

0 comments on commit 024bd4f

Please sign in to comment.