自定义错误信息

1
2
3
4
5
6
7
8
9
10
11
12
13
class ZPError extends Error {
constructor ({ code, message }) {
// Calling parent constrcutor of base Error class.
super(message)
// Capturing stack trace, excluding constructor call from it.
Error.captureStackTrace && Error.captureStackTrace(this, this.constructor)
// Saving class name in the property of our custom error as a shortcut.
this.name = this.constructor.name // 自定义参数
this.code = code // 自定义参数
}
}

export default ZPError

使用👇

1
throw new ZPError({ code: 400, message: '参数错误' })
坚持原创技术分享,您的支持将鼓励我继续创作!