本文共 3438 字,大约阅读时间需要 11 分钟。
node.js 错误
Errors in Node.js are handled through exceptions.
Node.js中的错误通过异常处理。
An exception is created using the throw
keyword:
使用throw
关键字创建一个异常:
throw value
As soon as JavaScript executes this line, the normal program flow is halted and the control is held back to the nearest exception handler.
JavaScript一旦执行此行,就会停止常规程序流,并将控件保留到最近的异常处理程序 。
Usually in client-side code value
can be any JavaScript value including a string, a number or an object.
通常,在客户端代码中, value
可以是任何JavaScript值,包括字符串,数字或对象。
In Node.js, we don’t throw strings, we just throw Error objects.
在Node.js中,我们不抛出字符串,而仅抛出Error对象。
An error object is an object that is either an instance of the Error object, or extends the Error class, provided in the :
错误对象是错误对象的实例,或者是提供的扩展错误类的对象:
throw new Error('Ran out of coffee')
or
要么
class NotEnoughCoffeeError extends Error { //...}throw new NotEnoughCoffeeError
An exception handler is a try
/catch
statement.
异常处理程序是try
/ catch
语句。
Any exception raised in the lines of code included in the try
block is handled in the corresponding catch
block:
try
块中包含的代码行中引发的任何异常都在相应的catch
块中处理:
try { //lines of code} catch (e) {}
e
in this example is the exception value.
在此示例中, e
是异常值。
You can add multiple handlers, that can catch different kinds of errors.
您可以添加多个处理程序,它们可以捕获各种错误。
If an uncaught exception gets thrown during the execution of your program, your program will crash.
如果在程序执行过程中引发了未捕获的异常,则程序将崩溃。
To solve this, you listen for the uncaughtException
event on the process
object:
要解决此问题,请侦听process
对象上的uncaughtException
事件:
process.on('uncaughtException', (err) => { console.error('There was an uncaught error', err) process.exit(1) //mandatory (as per the Node docs)})
You don’t need to import the process
core module for this, as it’s automatically injected.
您不需要为此导入process
核心模块,因为它是自动注入的。
Using promises you can chain different operations, and handle errors at the end:
使用promise可以链接不同的操作,并在最后处理错误:
doSomething1() .then(doSomething2()) .then(doSomething3()) .catch(err => console.error(err))
How do you know where the error occurred? You don’t really know, but you can handle errors in each of the functions you call (doSomethingX
), and inside the error handler throw a new error, that’s going to call the outside catch
handler:
您怎么知道错误发生在哪里? 您并不是很清楚,但是您可以处理所调用的每个函数( doSomethingX
)中的错误,并且在错误处理程序内部将引发新错误,这将调用外部catch
处理程序:
const doSomething1 = () => { //... try { //... } catch (err) { //... handle it locally throw new Error(err.message) } //...}
To be able to handle errors locally without handling them in the function we call, we can break the chain you can create a function in each then()
and process the exception:
为了能够在本地处理错误而无需在我们调用的函数中处理错误,我们可以中断链,您可以在每个then()
创建一个函数并处理异常:
doSomething1 .then((() => { return doSomething2().catch(err => { //handle error throw err //break the chain! }) }) .then((() => { return doSomething2().catch(err => { //handle error throw err //break the chain! }) }) .catch(err => console.error(err))
Using async/await, you still need to catch errors, and you do it this way:
使用异步/等待,您仍然需要捕获错误,并且您可以通过以下方式进行操作:
async function someFunction() { try { await someOtherFunction() } catch (err) { console.error(err.message) }}
翻译自:
node.js 错误
转载地址:http://ktqgb.baihongyu.com/