Skip to content
Open
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 docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog
applied. Uninstalling from the same panel removes the PAS plugin
and the per-user JWT signing secrets.

- #99 Report the reason when object creation fails
- #98 Accept REST verbs PUT/PATCH/DELETE on the action route
- #97 Extract create/update/delete helpers to api/mutation
- #96 Add typed exception subclasses for the JSON API error envelope
Expand Down
11 changes: 10 additions & 1 deletion src/senaite/jsonapi/api/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def create_items(portal_type=None, uid=None, endpoint=None, **kw):
records = req.get_request_data()

results = []
errors = []
for record in records:
if portal_type is None:
portal_type = record.pop("portal_type", None)
Expand All @@ -111,9 +112,17 @@ def create_items(portal_type=None, uid=None, endpoint=None, **kw):
except Exception as e:
sp.rollback()
logger.exception("Error while creating object: %s", e)
errors.append(str(e))

if not results:
raise BadRequestError("No Objects could be created")
# Surface the underlying reason(s) -- e.g. missing required
# fields reported by the object's validation ({"field": "required
# field"}) -- instead of a generic failure, so the caller sees
# exactly what to fix.
detail = "; ".join(filter(None, errors))
raise BadRequestError(
"No objects could be created: {}".format(detail)
if detail else "No objects could be created")

return make_items_for(results, endpoint=endpoint)

Expand Down
Loading