diff --git a/news/1837.bugfix b/news/1837.bugfix new file mode 100644 index 0000000000..08502b6c36 --- /dev/null +++ b/news/1837.bugfix @@ -0,0 +1 @@ +URL Management control panel: Fix error handling in CSV upload. @davisagli diff --git a/src/plone/restapi/services/aliases/add.py b/src/plone/restapi/services/aliases/add.py index e54ccc0c67..2894d25b22 100644 --- a/src/plone/restapi/services/aliases/add.py +++ b/src/plone/restapi/services/aliases/add.py @@ -101,12 +101,22 @@ def _reply_csv(self): raise BadRequest("Uploaded file is not a valid CSV file") controlpanel = RedirectsControlPanel(self.context, self.request) + csv_errors = controlpanel.csv_errors = [] storage = getUtility(IRedirectionStorage) status = IStatusMessage(self.request) portal = getSite() controlpanel.upload(file, portal, storage, status) file.close() + if csv_errors: + self.request.response.setHeader("Content-Type", "application/json") + self.request.response.setStatus(BadRequest) + return { + "type": "BadRequest", + "message": f"Found {len(csv_errors)} errors in CSV file.", + # Skip first item which is a notice about the delimiter + "csv_errors": csv_errors[1:], + } if err := status.show(): if err[0].type == "error": raise BadRequest(err[0].message)