Skip to content

Commit

Permalink
resources: make record error handlers configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
slint committed Nov 4, 2024
1 parent b5fbfd4 commit c707cad
Showing 1 changed file with 82 additions and 77 deletions.
159 changes: 82 additions & 77 deletions invenio_rdm_records/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,84 @@ def _bibliography_headers(obj_or_list, code, many=False):
"application/linkset+json": ResponseHandler(FAIRSignpostingProfileLvl2Serializer()),
}

error_handlers = {
**ErrorHandlersMixin.error_handlers,
DeserializerError: create_error_handler(
lambda exc: HTTPJSONException(
code=400,
description=exc.args[0],
)
),
StyleNotFoundError: create_error_handler(
HTTPJSONException(
code=400,
description=_("Citation string style not found."),
)
),
ReviewNotFoundError: create_error_handler(
HTTPJSONException(
code=404,
description=_("Review for draft not found"),
)
),
ReviewStateError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=str(e),
)
),
ReviewExistsError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=str(e),
)
),
InvalidRelationValue: create_error_handler(
lambda exc: HTTPJSONException(
code=400,
description=exc.args[0],
)
),
InvalidAccessRestrictions: create_error_handler(
lambda exc: HTTPJSONException(
code=400,
description=exc.args[0],
)
),
ValidationErrorWithMessageAsList: create_error_handler(
lambda e: HTTPJSONValidationWithMessageAsListException(e)
),
AccessRequestExistsError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=e.description,
)
),
RecordDeletedException: create_error_handler(
lambda e: (
HTTPJSONException(code=404, description=_("Record not found"))
if not e.record.tombstone.is_visible
else HTTPJSONException(
code=410,
description=_("Record deleted"),
tombstone=e.record.tombstone.dump(),
)
)
),
RecordSubmissionClosedCommunityError: create_error_handler(
lambda e: HTTPJSONException(
code=403,
description=e.description,
)
),
CommunityRequiredError: create_error_handler(
HTTPJSONException(
code=400,
description=_("Cannot publish without selecting a community."),
)
),
}


#
# Records and record versions
Expand Down Expand Up @@ -187,83 +265,10 @@ class RDMRecordResourceConfig(RecordResourceConfig, ConfiguratorMixin):
default=record_serializers,
)

error_handlers = {
**ErrorHandlersMixin.error_handlers,
DeserializerError: create_error_handler(
lambda exc: HTTPJSONException(
code=400,
description=exc.args[0],
)
),
StyleNotFoundError: create_error_handler(
HTTPJSONException(
code=400,
description=_("Citation string style not found."),
)
),
ReviewNotFoundError: create_error_handler(
HTTPJSONException(
code=404,
description=_("Review for draft not found"),
)
),
ReviewStateError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=str(e),
)
),
ReviewExistsError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=str(e),
)
),
InvalidRelationValue: create_error_handler(
lambda exc: HTTPJSONException(
code=400,
description=exc.args[0],
)
),
InvalidAccessRestrictions: create_error_handler(
lambda exc: HTTPJSONException(
code=400,
description=exc.args[0],
)
),
ValidationErrorWithMessageAsList: create_error_handler(
lambda e: HTTPJSONValidationWithMessageAsListException(e)
),
AccessRequestExistsError: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=e.description,
)
),
RecordDeletedException: create_error_handler(
lambda e: (
HTTPJSONException(code=404, description=_("Record not found"))
if not e.record.tombstone.is_visible
else HTTPJSONException(
code=410,
description=_("Record deleted"),
tombstone=e.record.tombstone.dump(),
)
)
),
RecordSubmissionClosedCommunityError: create_error_handler(
lambda e: HTTPJSONException(
code=403,
description=e.description,
)
),
CommunityRequiredError: create_error_handler(
HTTPJSONException(
code=400,
description=_("Cannot publish without selecting a community."),
)
),
}
error_handlers = FromConfig(
"RDM_RECORDS_ERROR_HANDLERS",
default=error_handlers,
)


#
Expand Down

0 comments on commit c707cad

Please sign in to comment.