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

Returns "message" as an object instead of a string, when a BadRequest occurs #179

Closed
wants to merge 2 commits into from

Conversation

wesleybl
Copy link
Member

@wesleybl wesleybl commented Feb 21, 2024

This makes it possible for the error message to reach the client as an object, avoiding the need for string-to-object conversions.

plone/plone.restapi#1755 needs to be merge first.

Fixes #176

@mister-roboto
Copy link

@wesleybl thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

@wesleybl
Copy link
Member Author

@jenkins-plone-org please run jobs

@wesleybl
Copy link
Member Author

It will be necessary to fix two tests in plone.restapi.

@@ -57,7 +58,10 @@ def __call__(self):

def render_exception(self, exception):
name = type(exception).__name__
message = str(exception)
if isinstance(exception, BadRequest):
message = exception.args[0]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message it looked like this in the restapi test:

[{'message': 'Required input is missing.', 'field': 'email_charset', 'error': RequiredMissing('email_charset')}]

Later, we tried to do json.dumps on it:

result = json.dumps(data, indent=2, sort_keys=True)

As we have:

'error': RequiredMissing('email_charset')

an error occurs:

TypeError: Object of type RequiredMissing is not JSON serializable

We then need to make all the keys a string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this. Now the message is serializable.

occurs

This makes it possible for the error message to reach the client as an
object, avoiding the need for string-to-object conversions.
@wesleybl
Copy link
Member Author

@jenkins-plone-org please run jobs

If the exception is a BadRequest and the message is a list, return the
message with list. If the list elements are dictionaries, convert
non-serializable keys and values to string. If it is not a BadRequest,
returns the exception as a string.
wesleybl added a commit to plone/plone.restapi that referenced this pull request Feb 23, 2024
After:

plone/plone.rest#179

BadRequest errors are returned as an object and not as a string. This
fix here takes this into account. But for now the tests are skipped, so
that the merge can be done before the plone.rest change is in a Plone
version.
@wesleybl
Copy link
Member Author

@tisto @jensens @davisagli I fix the plone.restapi tests but for now I'm skipping them so that the tests there work with a version of Plone without this PR here.

Ready for review now.

@wesleybl
Copy link
Member Author

While I was testing this PR with Volto, I discovered that Volto has a function that can transform the error string into a list:

https://github.com/plone/volto/blob/81d0369e1628b3072f9e47d14831a2618d2633b6/packages/volto/src/helpers/FormValidation/FormValidation.js#L160

So I'll try to fix plone/volto#1868 without this PR here for now.

@wesleybl wesleybl marked this pull request as draft February 26, 2024 16:14
@wesleybl
Copy link
Member Author

wesleybl commented Mar 4, 2024

It was possible to fix the issue without this PR. See: plone/volto#5808

@wesleybl wesleybl closed this Mar 4, 2024
@wesleybl wesleybl deleted the error_message branch March 4, 2024 15:36
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

Successfully merging this pull request may close these issues.

Return error message as objects instead of string
2 participants