Tag: error handling

表示next()标题错误

在一个路线我有这个: if (elements.length <= 0) { var msg = 'no elements found'; console.error(msg); var err = new Error('Not found'); err.status = 404; err.message = msg; next(err); } console.log('Found ' + elements.length + ' elements'); res.setHeader('Content-Type', 'application/json'); /*error*/ res.status(200).json(elements); res.end(); 在app.js中最后定义的error handling程序: // development error handler app.use(function(err, req, res, next) { res.type('application/json'); res.status(err.status || 500); res.json({ […]

在使用Raygun的快递处理程序后,如何closures我的stream程?

我正在用NodeJS和Express构build一个应用程序,最近还join了Raygun的error handling。 Raygun提供了一个专门用于Express的error handling程序,但它必须是运行的最后一个中间件。 现在我有: //Log the domain id, req, and error to the console app.use(function errorHandler(err, req, res, next) { console.log('error on request %d %s %s: %s', process.domain.id, req.method, req.url, err) next(err) }) //Log the error to Raygun //!Must be last piece of middleware to be defined app.use(raygunClient.expressHandler); 但问题是服务器继续运行,可能会使应用程序处于未知状态。 我想做一个优雅的服务器关机,但如果我添加 //Shuts down the process […]

在Node.js中捕获SQL错误

我有一个pSQL表设置为要求一些数据的唯一值,我打电话插入这个数据的function: CREATE OR REPLACE FUNCTION create_location( name text, latitude numeric, longitude numeric, logo_url text = '') RETURNS void AS $$ INSERT INTO "PartySpot".locations (name, latitude, longitude, logo_url) VALUES (name, latitude, longitude, logo_url); $$ LANGUAGE SQL VOLATILE STRICT; 我使用node.js / express从一个端点调用它: pg.connect(connectionString, function(err, client, done) { var query = client.query("SELECT * FROM create_location($1,$2,$3,$4)", [req.body.name, req.body.latitude, req.body.longitude, […]

在Node.js中,如何确定subprocess是否已经正常启动?

var spawn = require('child_process').spawn; var child = spawn('some-command'); 我知道我可以防止ENOENT (当some-command不存在时) child.on('error', function(err) { … }) 现在,我如何asynchronous确定进程正在运行, 并没有发生错误 ? 我可以听error和close事件,但是这仍然使得“ 正在运行 ”的情况看起来与“ 操作系统还没有到处寻找文件 ”相同,这可能导致恶劣的竞争条件。 一个open事件会很好,但文件没有提到一个。 function解决方法是否存在?

从生成的节点进程中捕获ECONNRESET错误

我正在使用sftps NPM使用用户名/密码authentication连接到SFTP服务器并上传文件。 成功的时候,这个工作很好。 但是,如果提供了无效的身份validation信息,几分钟后会发出ECONNRESET错误,导致整个应用程序崩溃。 查看sftps模块的源代码,它似乎使用child_process.spawn来运行shell命令,并且看起来应该正常捕获任何错误: var sftp = spawn(shellCmd, shellOpts); var data = ""; var error = ""; sftp.stdout.on('data', function (res) { data += res; }); sftp.stderr.on('data', function (res) { error += res; }); function finished(err) { error = error.split('\n').filter(function(line) { if (/^Connected to /.test(line)) return false; return true; }).join('\n'); data = data.split('\n').filter(function(line) { if […]

如何捕捉蓝鸟ENOENT?

因为fs.exists的弃用,我喜欢用蓝鸟捕捉ENOENT。 举个例子: .then(() => { return promisedFs.unlinkAsync(excelPath); }) .catch(ENOENT ERROR, () => { //do something }) .catch(all other errors, () => {//do something})

Nodejs / Express – 错误:发送后无法设置标头

相当新的节点/expression。 我正在检查用户(通过用户名)是否已经存在于数据库中,如果他们已经存在,就会发出错误。 当我使用curl来尝试将其closures时,我得到以下错误: 错误:发送后无法设置标题。 我已经知道,我所做的第一个检查是为了确保所有的字段都被正确填写,并且不会多次设置标题。 任何帮助将不胜感激。 (我的相关代码如下,如果你需要其他的东西,可以这么说!) router.post('/register', function(req, res, next) { if(!req.body.username || !req.body.password){ return res.status(400).json({ message: 'Please fill out all fields.' }); } User.count({ username: req.body.username}, function(err, count){ console.log(count); if(count > 0) { return res.status(400).json({message: 'This user already exists!' }); } }); var user = new User(); user.username = req.body.username; user.setPassword(req.body.password); user.save(function(err) { […]

在Node.js中用MongoDB删除上传的文件

我有两个实体之间的一对多关系:课程和文件。 所以,我设置我的数据库的方式是使用两个独立的集合:课程和文件。 var CourseSchema = new mongoose.Schema({ name: { type: String, required: true }, code: { type: String, required: true, unique: 1, uppercase: 1 }, files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'File' }] // more fields }); var FileSchema = new mongoose.Schema({ name: { type: String, required: true } // more fields }); 我有一个工作页面,允许用户在课程中上传和添加文件。 同样,他们也可以在同一页面上删除选定的文件。 我担心的是当我删除选定的文件。 […]

为什么在节点7.2.0中logging被拒绝的`Promise`会导致logging一个堆栈跟踪?

在节点7.2.0中运行此代码时: let prms = Promise.reject(new Error('error')); prms.catch(() => {}); console.log(prms); 我正在期待Promise {<rejected> Error: error}被logging到控制台,而是我收到这个输出: Promise { <rejected> Error: error at Object.<anonymous> (/Users/davidlund/Dropbox/test.js:1:89) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:420:7) at startup (bootstrap_node.js:139:9) at bootstrap_node.js:535:3 } 这是为什么?

快递:下一个(错误)堆栈跟踪

我发现很难find使用next(err)时发生next(err) 。 有没有办法如何保存堆栈以追溯到原始文件的错误? (即使这是一个validation错误,我想知道它起源的确切位置。)