0

这是我正在尝试的代码的一部分

const boom = require('boom')
exports.checkOtp = async (req, reply) => {
    try {
        return boom.badRequest('no active otp')
    } catch (err) {
        throw boom.boomify(err)
    }
}

我正进入(状态

{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "no active otp"
}

boom.badRequest应该开火400吧?

4

2 回答 2

1

why not just using throw error? and creating new error just by throw it

const err = new Error();
err.statusCode = 400;
err.message = 'message';
throw err;
于 2020-09-14T11:21:57.323 回答
1

Boom 是 hapi 为 hapi 构建和设计的模块。

您应该使用(非官方)fastify-boom插件或添加您的状态码:

const boom = require('boom')
exports.checkOtp = async (req, reply) => {
    try {
        reply.code(400)
        return boom.badRequest('no active otp')
    } catch (err) {
        reply.code(500)
        throw boom.boomify(err)
    }
}
于 2019-09-09T07:27:09.030 回答