createCustomError.js 543 B

123456789101112131415161718
  1. module.exports = function createCustomError(name, message) {
  2. // use Object.create(), because some VMs prevent setting line/column otherwise
  3. // (iOS Safari 10 even throws an exception)
  4. var error = Object.create(SyntaxError.prototype);
  5. var errorStack = new Error();
  6. error.name = name;
  7. error.message = message;
  8. Object.defineProperty(error, 'stack', {
  9. get: function() {
  10. return (errorStack.stack || '').replace(/^(.+\n){1,3}/, name + ': ' + message + '\n');
  11. }
  12. });
  13. return error;
  14. };