-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
ValidationError string representation shows no relevant details #2135
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
Comments
This is really causing problems for me as I'm doing some wide spread refactorings of the models in an existing system. Pinpointing the location of failures would save me a lot of time... If anyone can suggest a way to get real stack traces out of this I'm all ears. |
Using validate produces the same result. Also, I noticed that if I'm using chai, and I: expect(err).to.not.exist ... with one of the validation errors from Mongoose, I get:
Most other errors from Node libraries toString() as their message and stack trace by default, which would be my desired result. |
I'm going to take a stab at correcting some of these issues. Would you like the changes on the 3.8.x branch or some other? |
@bitmage would be much appreciated. Changes against master branch please. |
@vkarpov15 Well, I got something to work, but it's a hack that involves generating an error at the entry point in order to harvest the stack trace. You can see the code here: https://github.com/TorchlightSoftware/mongoose/compare/master A few notes: I first tried capturing the entry point by saving the validate function's caller: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller I passed that through to the ValidationError constructor and used it as the second argument for Error.captureStackTrace(). The stack came out blank. I think this is because after process.nextTick, the stack doesn't include that entry point at all, so the filtering can't work as expected. That seems consistent with the documentation for captureStackTrace: https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi So, then I tried the method you see in the current code. I created a new error as soon as we enter the validate function. There I get a stack trace that includes my application code, but has a bunch of noise from the hooks api. Next, I used Error.captureStackTrace on the error. Strangely enough, the hook entries disappeared and it put the top line of the stack trace right on the line of code in my app that called validate(). Success! Though through a strange route... So a few questions:
Let me know what you think. I didn't submit a pull request because I think it still needs some work. |
Async stack traces are pretty difficult, not sure if its at all possible. Also, I'd recommend having a sensible fallback in case |
I've added some further changes to include the sub-errors in the stack trace message. Super helpful in saving time now that I have full details on what failed and where. I'll work on incorporating a fallback as you mention, and trying for a more encapsulated solution. Will probably be a few days. |
See PR -- #2364 -- I believe this fixes the reported issue. |
Can you show an example stack trace that you're obtaining? I'm curious as to how you got any info before the process.nextTick without explicitly capturing a stack trace at the entry point. That nextTick prevented me from seeing any application lines in the stack trace in my testing... And I think it's important to have them. |
I think the greater issue here is that there can be multiple validation errors triggered in a single pass. Have you attempted to inspect the |
Generally the |
@jondavidjohn There are no relevant stack trace details in any of the err.errors objects. |
ahh, you're looking for a stack trace to a validation error? Seems like stack traces are most useful in situations where uncaught or unexpected errors happen, it seems like
I don' t particularly see the benefit of the |
I do however think, adding the model name to the original error message might be useful. |
Concrete example of where more information would be helpful: I wrote a job that gets data from an API and creates mongoose models out of the data. After running it, I got:
I didn't know why it failed. I looked at the stack trace to see exactly at which line it failed - line 139 - and navigated to that line of code.
Why would this fail? I looked at the model, This could have all been avoided if the error message read:
As for multiple errors, just append Thoughts? |
While trying saving the document get this on 4.11.6 I get nothing meaning validation error
When I use
Is it expected behavior? If so, I wonder why. |
@whitecolor not expected behavior. Can you open up a new issue with some code samples please? |
I'm getting a ValidationError which looks like this:
The problem is there's no useful information contained in the error.
I hunted down the error, and I see it has an 'errors' subobject:
There are a couple of things I think would be helpful:
model name
and alisting of fields that errored
to the main error message. This way it will print out useful information by default, without requiring the consumer to create custom logic to pull out the details.The text was updated successfully, but these errors were encountered: