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 an error message when an Invalid occurs when validating a controlpanel field #1771

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1771.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Returns an error message when an Invalid error occurs when validating a controlpanel field. Also translates the message. @wesleybl
14 changes: 11 additions & 3 deletions src/plone/restapi/deserializer/controlpanels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from zope.component import adapter
from zope.component import getUtility
from zope.component import queryMultiAdapter
from zope.i18n import translate
from zope.interface import implementer
from zope.interface.exceptions import Invalid
from zope.schema import getFields
from zope.schema.interfaces import ValidationError

Expand All @@ -32,7 +34,7 @@ def __init__(self, controlpanel):
self.context = self.controlpanel.context
self.request = self.controlpanel.request

def __call__(self):
def __call__(self, mask_validation_errors=True):
data = json_body(self.controlpanel.request)

proxy = self.registry.forInterface(self.schema, prefix=self.schema_prefix)
Expand Down Expand Up @@ -61,10 +63,10 @@ def __call__(self):
field.validate(value)
# Set the value.
setattr(proxy, name, value)
except ValueError as e:
errors.append({"message": str(e), "field": name, "error": e})
except ValidationError as e:
errors.append({"message": e.doc(), "field": name, "error": e})
except (ValueError, Invalid) as e:
errors.append({"message": str(e), "field": name, "error": e})
else:
field_data[name] = value

Expand All @@ -77,4 +79,10 @@ def __call__(self):
errors.append({"error": error, "message": str(error)})

if errors:
for error in errors:
if mask_validation_errors:
# Drop Python specific error classes in order to be able to better handle
# errors on front-end
error["error"] = "ValidationError"
error["message"] = translate(error["message"], context=self.request)
raise BadRequest(errors)
16 changes: 16 additions & 0 deletions src/plone/restapi/tests/test_services_controlpanels.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ def test_update_required(self):
self.assertIn("message", response)
self.assertIn("Required input is missing.", response["message"])

def test_update_validation(self):
response = self.api_session.patch(
"/@controlpanels/socialmedia", json={"twitter_username": "@test"}
)
response = response.json()
self.assertIn(
'Twitter username should not include the "@" prefix character.',
response["message"],
)

def test_update_validation_status(self):
response = self.api_session.patch(
"/@controlpanels/socialmedia", json={"twitter_username": "@test"}
)
self.assertEqual(response.status_code, 400)

def test_get_usergroup_control_panel(self):
# This control panel does not exist in Plone 5
response = self.api_session.get("/@controlpanels/usergroup")
Expand Down
Loading