question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidationError string representation shows no relevant details

See original GitHub issue

I’m getting a ValidationError which looks like this:

ValidationError: Validation failed
  at model.Document.invalidate (.../project/node_modules/mongoose/lib/document.js:990:32)
  at .../project/node_modules/mongoose/lib/document.js:959:16
  at validate (.../project/node_modules/mongoose/lib/schematype.js:561:7)
  at .../project/node_modules/mongoose/lib/schematype.js:577:9
  at Array.forEach (native)
  at ObjectId.SchemaType.doValidate (.../project/node_modules/mongoose/lib/schematype.js:565:19)
  at .../project/node_modules/mongoose/lib/document.js:957:9
  at process._tickCallback (node.js:415:13)

The problem is there’s no useful information contained in the error.

  1. No lines of code in my application are mentioned.
  2. It doesn’t say what model failed.
  3. It doesn’t say what field or value failed.

I hunted down the error, and I see it has an ‘errors’ subobject:

ValidatorError: Path `account` is required.
  at validate (.../project/node_modules/mongoose/lib/schematype.js:610:16)
  at .../project/node_modules/mongoose/lib/schematype.js:627:9
  at Array.forEach (native)
  at ObjectId.SchemaType.doValidate (.../project/node_modules/mongoose/lib/schematype.js:614:19)
  at .../project/node_modules/mongoose/lib/document.js:956:9
  at process._tickCallback (node.js:415:13)
  1. Still no mention of what model failed (account is required on a number of my models).
  2. Still no mention of my application code in the stack trace.

There are a couple of things I think would be helpful:

  1. Add the model name and a listing 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.
  2. Is it possible to create a dummy error object when the initial create or save request is made, and then use the stack trace if any errors occur? Just throwing out ideas here, but anything that could show me the entry point into your library in the trace would be very helpful, as that’s the line of code I actually need to change.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:4
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ksikkacommented, May 10, 2016

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:

ValidationError: Client validation failed

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.

client.lastName = remoteClient.LastName;

Why would this fail? I looked at the model, lastName expects a string. It must be the case that the value assigned to the field was not a string. But then what was it? I had to run my script again with extra logging to determine it was a number.

This could have all been avoided if the error message read:

ValidationError: Client validation failed: Expected string for lastName, but got number instead.

As for multiple errors, just append (N more errors...) to the message.

Thoughts?

0reactions
vkarpov15commented, Aug 25, 2017

@whitecolor not expected behavior. Can you open up a new issue with some code samples please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

I don't want drf ValidationError response to be textual. How to ...
This is where your boolean becomes string ( text = force_text(data) part). One way to overcome this is to overwrite the ValidationError ......
Read more >
ValidationError Class (System.Workflow.ComponentModel ...
Creates a shallow copy of the current Object. (Inherited from Object). ToString(). Provides a string representation of this instance incorporating the error ...
Read more >
Handling Validation Errors - python-jsonschema
When an invalid instance is encountered, a ValidationError will be raised or returned, depending on which method or function is used.
Read more >
Form fields - Django documentation
If a Field has required=False and you pass clean() an empty value, then clean() will return a normalized empty value rather than raising...
Read more >
Improve Validation Errors with Adaptive Messages
This is pretty much as bad as it gets. The user is just told their input is invalid with no hints as to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found