app error design #57
-
do we really need the prop status in appError new AppError(404, 'fail', 'No document found with that id'); and also in some responses res.status(201).json({
status: 'success',
data: {
doc,
},
}); |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
The statusCode is enough to describe the response type .Thats why i think we don't need the status property. |
Beta Was this translation helpful? Give feedback.
-
Eid Mubarak to you too, my friend. I haven't returned home yet. I was so busy in those days. So extending error class will be very helpful. Now we throw error like this: //bad request error
throw new AppError(400,'fail', 'Bad request');
//forbidden error
throw new AppError(403,'fail', 'Forbidden!'); But if we create specific errors such as: //extend the base(AppError) class
//bad Request Error
class BadRequestError extends AppError {
constructor(message = "Bad request"){
super(400, 'fail', message)
}
}
//Forbidden Error
class ForbiddenError extends AppError {
constructor(message = "Forbidden"){
super(403 'fail', message)
}
} //now we can throw error like this if(some-logic){
throw new ForbiddenError('you are not allowed to perform this action');
}else if(some-logic){
throw new BadRequestError('data is required');
}else{
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
the issue is open for more than a week and adding app error follows best practice @muttaqin1, but anyway you can wait |
Beta Was this translation helpful? Give feedback.
Yeah sure let's do it,