You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
694 B

7 years ago
  1. /**
  2. * Module dependencies.
  3. */
  4. var AssertionError = require('assert').AssertionError
  5. , callsite = require('callsite')
  6. , fs = require('fs')
  7. /**
  8. * Expose `assert`.
  9. */
  10. module.exports = process.env.NO_ASSERT
  11. ? function(){}
  12. : assert;
  13. /**
  14. * Assert the given `expr`.
  15. */
  16. function assert(expr) {
  17. if (expr) return;
  18. var stack = callsite();
  19. var call = stack[1];
  20. var file = call.getFileName();
  21. var lineno = call.getLineNumber();
  22. var src = fs.readFileSync(file, 'utf8');
  23. var line = src.split('\n')[lineno-1];
  24. var src = line.match(/assert\((.*)\)/)[1];
  25. var err = new AssertionError({
  26. message: src,
  27. stackStartFunction: stack[0].getFunction()
  28. });
  29. throw err;
  30. }