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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make error message on invalid metadata friendlier #2767
Conversation
So the changes work, but now several tests fail expecting the old messages. |
Nope, we don't. The mapping comes from the I think the best way to add that would be to include them as |
Apparently |
Got it, I'm changing the expected error messages on the tests right now. |
Hey @di, I've only 5 errors left to fix, and all of them have to do with errors related to the I'm not sure how to proceed, should we write a different type error message for those cases? And not one like the |
@di I did indeed forget to push my last changes. Now I pushed them, but I accidentally the commits. I tried to do the |
@alanbato Coincidentally we have an open PR with some documentation updates for just this sort of thing, take a look at https://github.com/pypa/warehouse/pull/2760/files and see if that helps! |
08e7d69
to
e6260fc
Compare
It worked! Thank you. Funny how running those same commands in different order didn't help haha. Now the remaining failing tests should be the ones I told you about :) |
@alanbato The first two errors just seem to be that the tests haven't been updated to the new messages. The ones concerning This means we'll need to raise a different error message depending on whether the if field_name in form:
raise _exc_with_message(
HTTPBadRequest,
"{value!r} is an invalid value for {field},".format(
value=form[field_name].data,
field=form[field_name].label.text) +
"see https://packaging.python.org/specifications/core-metadata/",
)
else:
raise _exc_with_message(
HTTPBadRequest,
"Error: {}".format(form.errors[field_name][0])
) (note: I haven't tested that the above actually works) |
e6260fc
to
0e7e65d
Compare
Thanks @di, I suspected as much, and thought of doing something similar to handle it (although a little bit less elegant haha). All tests are passing locally, so I think this PR should be good to go :) Unless you or any other maintaner say otherwise, of course 馃槄 haha |
warehouse/forklift/legacy.py
Outdated
"Error: {}\n".format(form.errors[field_name][0]) + | ||
"see " | ||
"https://packaging.python.org/specifications/core-metadata/", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove the newlines from this error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the newline made the line longer than 80 characters, should I break it again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By newlines, I mean the "\n" characters you have in this string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally my bad, sorry! I just removed the \n
s 馃槄
warehouse/forklift/legacy.py
Outdated
"Error: {}\n".format(form.errors[field_name][0]) + | ||
"see " | ||
"https://packaging.python.org/specifications/core-metadata/", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these errors are not specific to a single field, it doesn't really make sense to include a link to the Core Metadata spec here.
warehouse/forklift/legacy.py
Outdated
@@ -384,47 +411,56 @@ class MetadataForm(forms.Form): | |||
), | |||
] | |||
) | |||
comment = wtforms.StringField(validators=[wtforms.validators.Optional()]) | |||
comment = wtforms.StringField( | |||
label="Comment", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field (and a number of others) don't correspond to actual core metadata fields as specified at https://packaging.python.org/specifications/core-metadata/.
I think it would be somewhat confusing if these fields failed to validate and returned an error message that looks like it's referring to a core metadata field (including linking to the spec) but that field doesn't actually exist.
I think we should probably drop the label
attribute from any of these that are not in the spec, and update the error message logic to only raise the "new" error message (which links to the spec) if the field has a label.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm, so do you think a good idea would be to change the code to something like this?
# Check both if the field is present or it has a label to show the 'new' error
if field_name in form and form[field_name].label:
raise _exc_with_message(
HTTPBadRequest,
"{value!r} is an invalid value for {field},".format(
value=form[field_name].data,
field=form[field_name].label.text) +
"see https://packaging.python.org/specifications/core-metadata/",
)
else:
# Fall back to the 'old' error in a more friendly manner
raise _exc_with_message(
HTTPBadRequest,
"Error: {}".format(form.errors[field_name][0])
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only issue with that is that it omits the field name that the original message had. I think there's three cases to consider here:
- the field name is in the form and has a label (it's a core metadata field) -> new error with link to spec
- the field name is in the form but doesn't have a label (it's one of these) -> original error
- the field name isn't in the form (it's
__all__
) -> new error without field name or link to spec
5025e37
to
9567218
Compare
warehouse/forklift/legacy.py
Outdated
value=form[field_name].data, | ||
field=form[field_name].label.text) + | ||
"Error: {} ".format(form.errors[field_name][0]) + | ||
"see " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you'll need to add something before "see" here, as there isn't a space between it and the error string. I'd recommend changing this to ", see"
.
Also, it looks like there are still newlines in the tests, as some are failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm changing the tests, I'm going to add those fixes to the test to the same commit as to stop the commit spamming I'm already doing haha.
As to the "see" part, I do have a space after the {}
9567218
to
94cd059
Compare
@alanbato Your changes look good, thanks! Looks like you'll need to add a test to cover the case where the field name is in the form, but it doesn't have a label. |
Thanks @di! I'm currently working on that test, and the only fields that fit that criteria and are also validated are the |
Sorry about that @alanbato, looks like I gave you some bad advice. Seems that by default fields will always have a
Looks like we can use
|
That's okay, I tried to look into it but couldn't get to a working experimento to validate that same hypothesis, haha. |
7c84c4f
to
e874cdb
Compare
e874cdb
to
8bb63da
Compare
Thanks @alanbato! |
Fixes #2668
I can't get the tests working due to #1536, so I'm 100% sure these changes work. 馃槄
Pinging @di for opinions on the message formatting, etc :)