You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
694 B
JavaScript
39 lines
694 B
JavaScript
5 years ago
|
/**
|
||
|
* Module dependencies.
|
||
|
*/
|
||
|
|
||
|
var AssertionError = require('assert').AssertionError
|
||
|
, callsite = require('callsite')
|
||
|
, fs = require('fs')
|
||
|
|
||
|
/**
|
||
|
* Expose `assert`.
|
||
|
*/
|
||
|
|
||
|
module.exports = process.env.NO_ASSERT
|
||
|
? function(){}
|
||
|
: assert;
|
||
|
|
||
|
/**
|
||
|
* Assert the given `expr`.
|
||
|
*/
|
||
|
|
||
|
function assert(expr) {
|
||
|
if (expr) return;
|
||
|
|
||
|
var stack = callsite();
|
||
|
var call = stack[1];
|
||
|
var file = call.getFileName();
|
||
|
var lineno = call.getLineNumber();
|
||
|
var src = fs.readFileSync(file, 'utf8');
|
||
|
var line = src.split('\n')[lineno-1];
|
||
|
var src = line.match(/assert\((.*)\)/)[1];
|
||
|
|
||
|
var err = new AssertionError({
|
||
|
message: src,
|
||
|
stackStartFunction: stack[0].getFunction()
|
||
|
});
|
||
|
|
||
|
throw err;
|
||
|
}
|