Mongoose 的错误处理 [英] Error handling with Mongoose

查看:20
本文介绍了Mongoose 的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个绝对的 NodeJS 初学者,想用 Express 和 Mongoose 创建一个简单的 REST-Web 服务.

I am an absolute NodeJS beginner and want to create a simple REST-Webservice with Express and Mongoose.

在一个中心位置处理 Mongoose 错误的最佳做法是什么?

Whats the best practice to handle errors of Mongoose in one central place?

当任何地方发生数据库错误时,我想返回一个带有错误消息的 Http-500-Error-Page:

When anywhere an database error occurs I want to return a Http-500-Error-Page with an error message:

if(error) {
  res.writeHead(500, {'Content-Type': 'application/json'});
  res.write('{error: "' + error + '"}');
  res.end();
}

在旧教程中http://blog-next-stage.learnboost.com/mongoose/我读到了一个全局错误侦听器:

In the old tutorial http://blog-next-stage.learnboost.com/mongoose/ I read about an global error listener:

Mongoose.addListener('error',function(errObj,scope_of_error));

但这似乎不起作用,我在 官方 Mongoose 文档 中找不到有关此侦听器的内容.我是否在每次 Mongo 请求后检查错误?

But this doesn't seem to work and I cannot find something in the official Mongoose documentation about this listener. Have I check for errors after every Mongo request?

推荐答案

如果您使用 Express,通常会直接在您的路由中或在 mongoose 之上构建的 api 中处理错误,并将错误转发到 下一步.

If you're using Express, errors are typically handled either directly in your route or within an api built on top of mongoose, forwarding the error along to next.

app.get('/tickets', function (req, res, next) {
  PlaneTickets.find({}, function (err, tickets) {
    if (err) return next(err);
    // or if no tickets are found maybe
    if (0 === tickets.length) return next(new NotFoundError));
    ...
  })
})

可以在您的错误处理程序中间件中嗅探NotFoundError以提供定制的消息传递.

The NotFoundError could be sniffed in your error handler middleware to provide customized messaging.

一些抽象是可能的,但您仍然需要访问 next 方法,以便将错误向下传递到路由链.

Some abstraction is possible but you'll still require access to the next method in order to pass the error down the route chain.

PlaneTickets.search(term, next, function (tickets) {
  // i don't like this b/c it hides whats going on and changes the (err, result) callback convention of node
})

至于集中处理猫鼬错误,没有一个地方可以处理所有错误.可以在多个不同级别处理错误:

As for centrally handling mongoose errors, theres not really one place to handle em all. Errors can be handled at several different levels:

connection 错误会在您的模型使用的 connection 上发出,所以

connection errors are emitted on the connection your models are using, so

mongoose.connect(..);
mongoose.connection.on('error', handler);

// or if using separate connections
var conn = mongoose.createConnection(..);
conn.on('error', handler);

对于典型的查询/更新/删除,错误会传递给您的回调.

For typical queries/updates/removes the error is passed to your callback.

PlaneTickets.find({..}, function (err, tickets) {
  if (err) ...

如果您不传递回调,则会在模型上发出错误如果您正在侦听它:

If you don't pass a callback the error is emitted on the Model if you are listening for it:

PlaneTickets.on('error', handler); // note the loss of access to the `next` method from the request!
ticket.save(); // no callback passed

如果您没有传递回调并且没有在 model 级别监听错误,它们将在模型 connection 上发出.

If you do not pass a callback and are not listening to errors at the model level they will be emitted on the models connection.

这里的关键是您希望以某种方式访问​​ next 以传递错误.

The key take-away here is that you want access to next somehow to pass the error along.

这篇关于Mongoose 的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆