Skip to content
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

How to surface custom CastError messages? #4113

Closed
joshkopecek opened this issue May 2, 2016 · 5 comments
Closed

How to surface custom CastError messages? #4113

joshkopecek opened this issue May 2, 2016 · 5 comments

Comments

@joshkopecek
Copy link

We're working on mongoose-geojson-schema, which adds validation for GeoJSON objects to Mongoose schemas. Following the guidance on creating a Custom Schema Type in the mongoose docs we're using

throw new mongoose.SchemaType.CastError('Crs must be an object or null');

referenced here
Yet when we test the code and review the exceptions thrown, they are generic messages:

Cast to Point failed for value "[object Object]" at path "point"

How can the user find out the detailed error message? Is there something else that we need to in the plugin, or something else the user does on the calling side?

Thanks in advance for any help here.

@markstos
Copy link
Contributor

markstos commented May 4, 2016

This is similar to, if not the same issue as #3162.

@vkarpov15 vkarpov15 added this to the 4.4.16 milestone May 7, 2016
@vkarpov15
Copy link
Collaborator

Right now a CastError has a reason property that contains the original error. Unfortunately the custom schema type examples currently prevent that property from getting populated though. To work around this, just throw any error type that isn't mongoose's CastError and you'll get the reason property.

@joshkopecek
Copy link
Author

@vkarpov15 thanks for looking at this. I've changed to using Error instead, which is definitely more meaningful than the generic CastError. We'll wait on the milestone release for switching back to CastError. @markstos

@vkarpov15
Copy link
Collaborator

You can also just use your own error type, a casterror gets surfaced in the end anyway

@markstos
Copy link
Contributor

markstos commented May 10, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants