node.js 错误_Node.js中的错误处理

node.js 错误

Errors in Node.js are handled through exceptions.

Node.js中的错误通过异常处理。

创建例外 (Creating exceptions)

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对象。

错误对象 (Error objects)

An error object is an object that is either an instance of the Error object, or extends the Error class, provided in the Error core module:

错误对象是错误对象的实例,或者是错误核心模块中提供的扩展错误类的对象:

throw new Error('Ran out of coffee')

or

要么

class NotEnoughCoffeeError extends Error {
  //...
}
throw new NotEnoughCoffeeError

处理异常 (Handling exceptions)

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.

您可以添加多个处理程序,它们可以捕获各种错误。

捕获未捕获的异常 (Catching uncaught exceptions)

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核心模块,因为它是自动注入的。

承诺的例外 (Exceptions with promises)

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))

使用异步/等待处理错误 (Error handling with async/await)

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)
  }
}

翻译自: https://flaviocopes.com/node-exceptions/

node.js 错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值