diff --git a/src/plone/rest/errors.py b/src/plone/rest/errors.py index ac6014c..40adc12 100644 --- a/src/plone/rest/errors.py +++ b/src/plone/rest/errors.py @@ -6,6 +6,7 @@ from Products.Five.browser import BrowserView from urllib.parse import quote from urllib.parse import unquote +from zExceptions import BadRequest from zExceptions import NotFound from zope.component import adapter from zope.component import queryMultiAdapter @@ -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] + else: + message = str(exception) result = {"type": name, "message": message} policy = queryMultiAdapter((self.context, self.request), ICORSPolicy) diff --git a/src/plone/rest/testing.py b/src/plone/rest/testing.py index bcd4eeb..e1ce335 100644 --- a/src/plone/rest/testing.py +++ b/src/plone/rest/testing.py @@ -37,3 +37,12 @@ def __call__(self): {}, None, ) + + +class BadRequestService(Service): + def __call__(self): + from zExceptions import BadRequest + + errors = [{"error": "ValidationError", "message": "message error"}] + + raise BadRequest(errors) diff --git a/src/plone/rest/testing.zcml b/src/plone/rest/testing.zcml index 2033039..38945a3 100644 --- a/src/plone/rest/testing.zcml +++ b/src/plone/rest/testing.zcml @@ -167,4 +167,12 @@ expose_headers="X-My-Header" /> + + diff --git a/src/plone/rest/tests/test_error_handling.py b/src/plone/rest/tests/test_error_handling.py index 32711a4..f53d2ec 100644 --- a/src/plone/rest/tests/test_error_handling.py +++ b/src/plone/rest/tests/test_error_handling.py @@ -107,3 +107,14 @@ def test_500_traceback_only_for_manager_users(self): self.assertRegex( traceback[0], r'^File "[^"]*", line \d*, in (publish|transaction_pubevents)' ) + + def test_bad_request_error_message(self): + response = requests.get( + f"{self.portal_url}/bad-request-error", + headers={"Accept": "application/json"}, + auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD), + ) + self.assertEquals( + response.json()["message"], + [{"error": "ValidationError", "message": "message error"}], + )