简体   繁体   English

如何在Node.js中处理不同的错误类型

[英]How to handle different error types in node.js

In node world, Error object is passed as a first argument to callback. 在节点世界中,Error对象作为第一个参数传递给回调。

What is the proper way to distinguish between error types in node? 区分节点中错误类型的正确方法是什么?

Use some attribute (.errorCode, or .userNotFound)? 使用某些属性(.errorCode或.userNotFound)?

Have separate (Error-inherited) class and use .constructor / instanceof? 有单独的(继承自错误的)类,并使用.constructor / instanceof?

Any other convention I am missing? 我还有其他约定吗?

You can pass anything, as long as it's not null or undefined it should represent an error. 您可以传递任何东西,只要它不是null或未定义,就应该表示一个错误。

You could use it any way you like, inherit Error is one way but you could pass an error Number or even an error String, it's really up to you. 您可以按照自己喜欢的任何方式使用它,继承Error是一种方法,但是您可以传递错误Number甚至是String,这完全取决于您。

A bit too "loose" IMHO but that's how it is. 恕我直言有点“宽松”,但事实就是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM