上QQ阅读APP看书,第一时间看更新
Creating the ValidationError interface
Create a new file at src/validators/errors/validation-error.js and add a class definition for ValidationError:
class ValidationError extends Error {
constructor(...params) {
super(...params);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ValidationError);
}
}
}
export default ValidationError;
The preceding code extends the Error class to create its own class. We need to do this in order to distinguish between validation errors (which should return a 400 response) and errors in our code (which should return a 500 response).