-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw custom Joi Errors #779
Comments
The conditional So my try {
// throw new JoiError(/* ... */);
} catch (err) {
if (err instanceof JoiError) {
// Use Joi error details...
} else if (err instanceof TypeError) {
// ... Etc.
}
} |
What's the benefit from current try {
// throw new JoiError(/* ... */);
} catch (err) {
if (err.name === 'ValidationError') {
// Use Joi error details...
} else if (err instanceof TypeError) {
// ... Etc.
}
} ? |
It's cleaner and more future-proof (vendor specific, object casting)? But this is probably my Java/PHP OOP backgroung telling me that. (I cant help myself but thinking basing logic on string names isn't as strong as using a language strengths) I missed the 'name' property, so my point isn't as much important anymore. Thank you for replying! |
FWIW, instanceof performance sucks, like half as fast, that might be something you consider if you do it often. |
👍 for custom error objects. I don't like depending on |
Mongoose also uses ValidationError as the name. |
Fair point, I'd go for a |
Why not |
Closed by 52c1781. |
👍 |
Error Response:
|
Out of curiosity, is there any reason not to implement this error as an extension of the Error class? E.g.,
This seems like a more standard approach rather than assigning properties to an instance of the generic error class. |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Hi,
That would be nice if Joi could throw custom Joi Errors:
https://github.com/hapijs/joi/blob/master/lib/errors.js#L140
Something in the fashion of:
It would make easier catching/narrowing to Joi-related errors in
try {} catch (err) {}
blocks as illustrated above. I am using koa and that would come very handy. For now I am testing if the thrownerr
has adetails
property which is not exactly future-proof nor elegant...Or may you have a better suggestion to cast Joi errors only?
The text was updated successfully, but these errors were encountered: