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
Custom (user-friendly) ValidatorError message #753
Conversation
…x and length string validators
Thanks! |
+1 Thanks |
why use i'd prefer to keep the new string validators out of mongoose and instead have those available as a plugin. can you add tests for these changes including cast error tests? then merge with latest master please. thanks! |
Actually there's no need for Sure, I'll add tests soon. Cheers |
great! |
@ManInTheBox what do you think about #887 ? |
@kof { message: 'Validation failed',
name: 'ValidationError',
errors:
{ 'name.username':
{ message: 'Username cannot be blank.',
name: 'ValidatorError',
path: 'name.username',
type: 'required' },
email:
{ message: 'E-mail cannot be blank.',
name: 'ValidatorError',
path: 'email',
type: 'required' },
password:
{ message: 'Password cannot be blank.',
name: 'ValidatorError',
path: 'password',
type: 'required' } } } For { message: 'Test must be a valid date.',
name: 'CastError',
type: 'date',
value: 'asdf' } If you need to find out what caused an error you can check if (err) {
if (err.name === 'ValidationError') {
// display validation errors to user
} else if (err.name === 'CastError') {
// display cast error message to user
} else {
// something related to db happened... crap!
}
} If you want to display nice error summary box, just iterate through
real example: I think this will fit your needs. btw @aheckmann I've stuck for a while with |
I am still missing the value of the field in the error, this is what I expect to find in logs:
In this example if you don't get the email string, you will not be able to understand why is the issue happened. An email regex will not say it to you. |
Yup, you're right! |
fine, the second thing is ValidatorError.prototype.toString method. It should return all error informations, not just the message, for me it is fine if you just use util.format so the result will look like in your example in previous comment (formatted json). The reason is to make it convinent (no need to log errors array separately) and consistent relative to other error handlings:
Error constructor will point in stack trace to this code and will call #toString method so get the stringified version of ValidationError. I believe this is the way how the most user handle errors in node. |
I am not sure if the stringified version of error should/can be mulitiline formatted. Probably "ValidationError: Validation failed. {email:{message:'E-mailisnotvalide.',name:'ValidatorError',path:'email',type:'required',value:'123234r@web.c'},password:{message:'Passwordcannotbeblank.',name:'ValidatorError',path:'password',type:'required'}}}" is fine. |
probably something like this
|
@kof what's the difference between this: if (err) return next(err); and this: if (err) return next(new Error(err)); ???
I'm using first approach in If you want to print Let's hear what others think about this. Cheers |
You need detailed information about the error if the error is happened. If it is happend and logged message is not complete you need to add detailed information in code and wait until the error happens again. This process is messy, everyone want to get the whole description at the moment error is happened. One could log .errors array in one central place, but there is this new Error(err) thingy, which will only pass the stringified version of the error. Sometimes one can pass just the original err, but very often you want to know which function has got the error instance. |
of course one could call everywere captureStackTrace instead of new Error(), then pass just the original error instance, so the endpoint logger can check for err.errors and log them all, but this is not handy.
|
@kof are you saying you want error objects to record where they were logged? |
@aheckmann yes, but this is not because I want to know where they are logged, but to know in which callback they were passed, to see which function has called the function which has passed to my callback the error object. need example? |
The point is we need both:
The 1. one is surely the original Error instance, but you haven't the 2. one if you don't add call points to stack. Actually it is a misuse of Error constructor in this case, because we actually don't want to create a new error, we just want to add a stack to this point, but it is handy and it wokred for all errors which return complete information from #toString. |
@ManInTheBox thanks, really waiting for this one :) |
sounds good. i was planning on revisiting this pull and a few others after 3.0 stable is out. |
@aheckmann congrats to v3 release! |
just merged locally but there were a ton of failing tests. this looks pretty good but we need all tests to pass and be backwards compatible before we can merge it in. |
@aheckmann yeah, i will provide all passing tests (don't have spare time right now). please try/review now b/c i fixed existing tests broken by this feature. also i will write better docs according to this pull. i have few failing test and IMO they aren't related to this pull: |
I just had the case, where knowing the query which was send to mongo would save a lot of time. Can we add it to the err object? (conditions, options, new values) This could be also the same string like one we get if mongoose.set('debug', true) |
Hi all, guys! Just wanted to ask - when can we wait for this pull request being merged to master? I'd like to use this awesome feature of User-Friendly errors stuff, without checking out some non-master branches. Thanks in advance! |
Hi, |
Thanks for a quick reply! |
No update news? |
+1 |
+1 This is we needed. |
We need this very much. |
+1 |
Can i ask what is the status of this? |
I would also like to know this please |
Such kind of feature should be built in |
+1 |
2 similar comments
+1 |
+1 |
Related to #747
Validators now accepts custom error message or if not provided, built-in error message will be used.
Error messages are borrowed from Yii PHP Framework and are stored in
errormessages.json
.This will be translated as:
Also,
CastError
is improved with built-in cast error message like:Age must be a number.
I added three new String validators,
min
,max
andlength
, because I think these are common.