在SailsJS中处理异常的最佳实践

时间:2021-01-01 20:18:44

I just started trying out SailsJS a few days ago.
I've realized that the Node is terminated whenever I have an uncaught exception.
I have a list of controllers and each of them calls a specific service JS file (Containing logics and DB calls) in services/.
Can I write a global error handler for all services so that any type of error that occurs from these services should be handled by it and appropriate error response has to be communicated to front-end.

我几天前才开始尝试帆船运动。我已经意识到,每当有未捕获的异常时,节点就会终止。我有一个控制器列表,每个控制器都在services/中调用一个特定的服务JS文件(包含逻辑和DB调用)。我是否可以为所有服务编写一个全局错误处理程序,以便it能够处理来自这些服务的任何类型的错误,并且必须将适当的错误响应传递给前端。

I tried using process.on('uncaughtexception') or some of basic exceptions but it needs to be added to each service method.

我尝试使用process.on('uncaughtexception')或一些基本的异常,但它需要添加到每个服务方法中。

Also can I have one common point for all service calls made from client to server through which all io.socket.post() and io..socket.get() goes through

我还可以有一个公共点,用于所有io.socketter .post()和io. socket.get()通过的从客户端到服务器的所有服务调用

I would appreciate any pointer/article that would show me the common best practices for handling uncaught exceptions in SailsJS and using shorter code rather than writing redundant code in all services.

我希望任何能向我展示处理SailsJS中未捕获异常和使用更短代码(而不是在所有服务中编写冗余代码)的通用最佳实践的指针/文章。

3 个解决方案

#1


4  

Best practice is using Domains in your controller. This will handle exceptions in async code, and its relatively straight forward.

最佳实践是在控制器中使用域。这将处理异步代码中的异常,以及相对直接的异常。

You can use something like trycatch to simplify things a little, but domain based exceptions will be most effective. It'll insure that exceptions do not crash your application. Just create a new domain in your controller and run your controller methods inside of that domain.

您可以使用trycatch之类的东西来简化一些事情,但是基于领域的异常将是最有效的。它将确保异常不会导致应用程序崩溃。只需在控制器中创建一个新域,并在该域内运行控制器方法。

Sailsjs being based on express you can use connect middleware, and you can seamlessly create a new domain from middleware. There such thing as express-domain-middleware. This might be the most aesthetic option, and most convenient.

Sailsjs基于express,您可以使用连接中间件,并且可以从中间件无缝地创建新域。比如表达式-域-中间件。这可能是最美观、最方便的选择。

Update: As mention by Benjamin Gruenbaum, Domains are planned to become deprecated in v1 of node. Perhaps you should read through Joyents Error Handling Best Practices. Its agnostic to the framework you are using.

更新:正如Benjamin Gruenbaum所提到的,在node的v1中,域将被废弃。也许您应该阅读通过Joyents错误处理最佳实践。它与您正在使用的框架无关。

Additonally you can still use Domains, while there isn't a way to globally handle errors in node.js otherwise. Once deprecated you could always remove your dependence on Domains, relatively easily. That said, it may be best not to rely solely on domains.

此外,您还可以使用域,而没有全局处理节点中的错误的方法。js。一旦被弃用,您就可以相对容易地消除对域的依赖。也就是说,最好不要仅仅依靠域名。

Strongloop also provides a library inspired by domains called Zone. This is also an option.

Strongloop还提供了一个被称为Zone的域的库。这也是一种选择。

#2


1  

Its OK to let node instance error out due to a programming error, else it may continue in an inconsistent state and mess-up business logic. In production environment the server can be restarted on crash, this will reset its state and keep it available, if the error is not frequent. And in all of it its very important to Log everything. This applies to most of Node setups, including SailsJS.

由于编程错误而让节点实例出错是可以的,否则它可能继续处于不一致的状态和混乱的业务逻辑。在生产环境中,服务器可以在崩溃时重新启动,这将重置其状态并保持可用,如果错误不频繁的话。所有这些都要记录。这适用于大多数节点设置,包括SailsJS。

The following approach can be taken:

可以采取以下方法:

  1. Use a Logger : A dedicated logger should be accessible to server components. Should be connected to a service that notifies the developer (email ?) of very serious errors.
  2. 使用日志记录器:服务器组件应该可以访问专用日志记录器。应该连接到一个服务,该服务通知开发人员(电子邮件?)非常严重的错误。
  3. Propagate per request errors to the end: Carefully forward errors from any step in request processing. In ExperssJs/ConnectJs/middle-ware based setup's, the next(err) can be used to pass an error to the middle-ware chain. An error catching middle-ware at the end of the chain will get this error, log it verbose, and send a 500 status back. You can use Domains or Zones or Promises or async or whatever you like to process request and catch errors.
  4. 将每个请求错误传播到末尾:仔细地从请求处理的任何步骤转发错误。在ExperssJs/ConnectJs/中间件基础设置中,下一个(err)可以用来将错误传递给中间件。在链的末尾捕获中间件的错误将会得到这个错误,将它详细记录下来,并将一个500状态返回。您可以使用域或区域、承诺或异步或任何您喜欢的方式来处理请求和捕获错误。
  5. Shutdown on process.on('uncaughtexception'): Log the erorr, do necessary clean-up, and throw the same error again to shutdown process.
  6. 关闭进程。on('uncaughtexception'):记录erorr,进行必要的清理,然后再次抛出相同的错误来关闭进程。
  7. User PM2/Forever or Upstart/init.d on linux : Now when the process shuts down due to the bad exception, these tools will restart it and track how many time server has been crashing. If the server is crashing way too many time, its good to stop it and take immediate action.
  8. 用户PM2 /永远或Upstart / init。linux上的d:现在当进程由于糟糕的异常而关闭时,这些工具将重新启动它并跟踪服务器崩溃的时间。如果服务器崩溃的时间太长,最好停止它并立即采取行动。

#3


0  

I have not tried this, but I believe you should be able to set a catch-all exception handler in bootstrap.js using process.on('uncaughtexception').

我还没有尝试过这种方法,但是我相信您应该能够在引导程序中设置一个“全部捕获”的异常处理程序。js使用至(“uncaughtexception”)。

Personally, I use promises via the bluebird library, and put a catch statement that passes all errors to a global error handling function.

就我个人而言,我通过bluebird库使用承诺,并将捕获语句传递给全局错误处理函数。

#1


4  

Best practice is using Domains in your controller. This will handle exceptions in async code, and its relatively straight forward.

最佳实践是在控制器中使用域。这将处理异步代码中的异常,以及相对直接的异常。

You can use something like trycatch to simplify things a little, but domain based exceptions will be most effective. It'll insure that exceptions do not crash your application. Just create a new domain in your controller and run your controller methods inside of that domain.

您可以使用trycatch之类的东西来简化一些事情,但是基于领域的异常将是最有效的。它将确保异常不会导致应用程序崩溃。只需在控制器中创建一个新域,并在该域内运行控制器方法。

Sailsjs being based on express you can use connect middleware, and you can seamlessly create a new domain from middleware. There such thing as express-domain-middleware. This might be the most aesthetic option, and most convenient.

Sailsjs基于express,您可以使用连接中间件,并且可以从中间件无缝地创建新域。比如表达式-域-中间件。这可能是最美观、最方便的选择。

Update: As mention by Benjamin Gruenbaum, Domains are planned to become deprecated in v1 of node. Perhaps you should read through Joyents Error Handling Best Practices. Its agnostic to the framework you are using.

更新:正如Benjamin Gruenbaum所提到的,在node的v1中,域将被废弃。也许您应该阅读通过Joyents错误处理最佳实践。它与您正在使用的框架无关。

Additonally you can still use Domains, while there isn't a way to globally handle errors in node.js otherwise. Once deprecated you could always remove your dependence on Domains, relatively easily. That said, it may be best not to rely solely on domains.

此外,您还可以使用域,而没有全局处理节点中的错误的方法。js。一旦被弃用,您就可以相对容易地消除对域的依赖。也就是说,最好不要仅仅依靠域名。

Strongloop also provides a library inspired by domains called Zone. This is also an option.

Strongloop还提供了一个被称为Zone的域的库。这也是一种选择。

#2


1  

Its OK to let node instance error out due to a programming error, else it may continue in an inconsistent state and mess-up business logic. In production environment the server can be restarted on crash, this will reset its state and keep it available, if the error is not frequent. And in all of it its very important to Log everything. This applies to most of Node setups, including SailsJS.

由于编程错误而让节点实例出错是可以的,否则它可能继续处于不一致的状态和混乱的业务逻辑。在生产环境中,服务器可以在崩溃时重新启动,这将重置其状态并保持可用,如果错误不频繁的话。所有这些都要记录。这适用于大多数节点设置,包括SailsJS。

The following approach can be taken:

可以采取以下方法:

  1. Use a Logger : A dedicated logger should be accessible to server components. Should be connected to a service that notifies the developer (email ?) of very serious errors.
  2. 使用日志记录器:服务器组件应该可以访问专用日志记录器。应该连接到一个服务,该服务通知开发人员(电子邮件?)非常严重的错误。
  3. Propagate per request errors to the end: Carefully forward errors from any step in request processing. In ExperssJs/ConnectJs/middle-ware based setup's, the next(err) can be used to pass an error to the middle-ware chain. An error catching middle-ware at the end of the chain will get this error, log it verbose, and send a 500 status back. You can use Domains or Zones or Promises or async or whatever you like to process request and catch errors.
  4. 将每个请求错误传播到末尾:仔细地从请求处理的任何步骤转发错误。在ExperssJs/ConnectJs/中间件基础设置中,下一个(err)可以用来将错误传递给中间件。在链的末尾捕获中间件的错误将会得到这个错误,将它详细记录下来,并将一个500状态返回。您可以使用域或区域、承诺或异步或任何您喜欢的方式来处理请求和捕获错误。
  5. Shutdown on process.on('uncaughtexception'): Log the erorr, do necessary clean-up, and throw the same error again to shutdown process.
  6. 关闭进程。on('uncaughtexception'):记录erorr,进行必要的清理,然后再次抛出相同的错误来关闭进程。
  7. User PM2/Forever or Upstart/init.d on linux : Now when the process shuts down due to the bad exception, these tools will restart it and track how many time server has been crashing. If the server is crashing way too many time, its good to stop it and take immediate action.
  8. 用户PM2 /永远或Upstart / init。linux上的d:现在当进程由于糟糕的异常而关闭时,这些工具将重新启动它并跟踪服务器崩溃的时间。如果服务器崩溃的时间太长,最好停止它并立即采取行动。

#3


0  

I have not tried this, but I believe you should be able to set a catch-all exception handler in bootstrap.js using process.on('uncaughtexception').

我还没有尝试过这种方法,但是我相信您应该能够在引导程序中设置一个“全部捕获”的异常处理程序。js使用至(“uncaughtexception”)。

Personally, I use promises via the bluebird library, and put a catch statement that passes all errors to a global error handling function.

就我个人而言,我通过bluebird库使用承诺,并将捕获语句传递给全局错误处理函数。