Skip to content

Commit

Permalink
Returns an error message when an Invalid occurs when validating a con…
Browse files Browse the repository at this point in the history
…trolpanel field (#1771)

* Returns an error message when an Invalid occurs when validating a
controlpanel field

* Translates controlpanel validation error message

* Does not return the "error" key as an object in controlpanel

When deserializing the controlpanel, do not return the "error" key as an
object, but rather as a string "ValidationError". On the front end, we
are unable to convert an object to json. So we need to return a string.
This is the same way content deserialization does.

* Improves error handling

---------

Co-authored-by: David Glick <[email protected]>
  • Loading branch information
wesleybl and davisagli authored Apr 30, 2024
1 parent f2e3653 commit 5e989dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
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

0 comments on commit 5e989dd

Please sign in to comment.