//ctx.js let ctx = {}; ctx.error = function (msg, code) { let error = (new Error); error.code = code; error.message = msg; error.file = error.stack.split('\n')[2]; return error; } module.exports = ctx;
//test.js const ctx = require('./ctx.js'); try { throw ctx.error("test", 500); } catch (e) { console.log(e.message, e.code, e.file); }
test 500 at Object.<anonymous> (F:\jy\jkpt_backend\local_test\t_error.js:9:20)