异步/等待错误处理

时间:2018-02-19 04:52:50

标签: javascript node.js error-handling promise async-await

我正在尝试处理我的async方法抛出的自定义错误,但try catch块无效。

我认为我这样做的方式应该有效,但错误没有被捕获,程序通过在终端中显示终止。

这是它抛出错误的地方:

async setupTap(tap) {
  const model = this.connection.model('Tap', TapSchema);

  await model.findOneAndUpdate({ id: tap.id }, tap, (err, result) => {
    let error = null;
    if (!result) {
      throw new Error('Tap doesn\'t exists', 404);
    }
    return result;
  });
}

然后,错误处理代码:

async setupTapHandler(request, h) {
  const tapData = {
    id: request.params.id,
    clientId: request.payload.clientId,
    beerId: request.payload.beerId,
    kegId: request.payload.kegId,
  };

  try {
    await this.kegeratorApi.setupTap(tapData);
  } catch (e) {
    if (e.code === 404) return h.response().code(404);
  }

  return h.response().code(204);
}

有人能帮助我吗?

我还看了其他主题:

https://secure.gravatar.com/avatar/

Correct Try...Catch Syntax Using Async/Await

2 个答案:

答案 0 :(得分:2)

如果您正在等待承诺,则只能使用await成功等待异步操作。假设你使用猫鼬,我不太了解mongoose,但是如果你传递回调,model.findOneAndUpdate()似乎不会返回一个承诺。相反,它会执行并将结果放入回调中。

此外,从这样的回调中执行throw只会进入数据库(调用回调的代码)并且根本不会对你有任何好处。要让throw做出被拒绝的承诺,您需要从异步函数的顶层抛出,或者从.then().catch()处理程序内部或在promise执行程序内抛出功能。这就是投掷拒绝的承诺。

这里的关键是你想使用数据库的promise接口,而不是回调接口。如果您没有通过回调,那么它会返回一个查询,您可以使用.exec()来获取您可以与await一起使用的承诺。

此外,您还没有构建一个将.code属性设置为404的错误对象。这不是Error对象构造函数支持的属性,因此如果您需要该属性,您必须手动设置。

我建议:

async setupTap(tap) {
    const model = this.connection.model('Tap', TapSchema);

    let result = await model.findOneAndUpdate({ id: tap.id }, tap).exec();
    if (!result) {
        let err = new Error('Tap doesn\'t exists');
        err.code = 404;
        throw err;
    }
    return result;
}

或者,在这里只有一个异步操作,使用await真的没什么好处。你可以这样做:

setupTap(tap) {
    const model = this.connection.model('Tap', TapSchema);

    return model.findOneAndUpdate({ id: tap.id }, tap).exec().then(result => {
        if (!result) {
            let err = new Error('Tap doesn\'t exists');
            err.code = 404;
            throw err;
        }
        return result;
    });
}

答案 1 :(得分:1)

函数findOneAndUpdate返回promise,因此不需要回调。如果需要回调并且您无法更新到新版本,那么可能会wrap calls in a promise(在To use a callback api as promise you can do:下)

然后你想在错误时设置代码,你不能用构造函数来做。

async setupTap(tap) {
  const model = this.connection.model('Tap', TapSchema);

  const result =   await model.findOneAndUpdate({ id: tap.id }, tap);
  if (!result) {
    const e = new Error('Tap doesn\'t exists');
    e.code = 404;
    throw(e);
  }
  return result;
}

async setupTapHandler(request, h) {
  const tapData = {
    id: request.params.id,
    clientId: request.payload.clientId,
    beerId: request.payload.beerId,
    kegId: request.payload.kegId,
  };

  try {
    await this.kegeratorApi.setupTap(tapData);
  } catch (e) {
    if (e.code === 404) return h.response().code(404);
  }

  return h.response().code(204);
}
相关问题