How can I identify custom error

前端 未结 2 1982
一整个雨季
一整个雨季 2020-12-22 03:30

I need to assigne an code/id to my custom error:

This is when I create the error:

var err=new Error(\'Numero massimo di cambi di username raggiunto\'         


        
2条回答
  •  清酒与你
    2020-12-22 03:57

    Define

    function MyError(code, message) {
      this.code = code;
      this.message = message;
      Error.captureStackTrace(this, MyError);
    }
    util.inherits(MyError, Error);
    MyError.prototype.name = 'MyError';
    

    Raise

    throw new MyError(777, 'Smth wrong');
    

    Catch

    if (err instanceof MyError) {
      console.log(err.code, err.message);
    }
    

提交回复
热议问题