search

Home  >  Q&A  >  body text

javascript - nodejs express 中,throw 一个异常,如何指定响应号?

throw new Error('asdasdad')

这样会返回
{

error: asdasdad,
error_code: 500,

}

如何指定error_code呢?

例如 throw new Error('asdasdad', 400)

大家讲道理大家讲道理2783 days ago527

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 16:36:04

    Just write a class that inherits from Error. For example:

    export default class ApiError extends Error {
      constructor(msg, code) {
        super(msg);
        this.msg = msg;
        this.code = code;
      }
    }
    
    

    reply
    0
  • Cancelreply