From c707cad1676ea3bd4e52366891ed786a1f3aa805 Mon Sep 17 00:00:00 2001 From: Alex Ioannidis Date: Mon, 4 Nov 2024 09:48:47 +0100 Subject: [PATCH] resources: make record error handlers configurable --- invenio_rdm_records/resources/config.py | 159 ++++++++++++------------ 1 file changed, 82 insertions(+), 77 deletions(-) diff --git a/invenio_rdm_records/resources/config.py b/invenio_rdm_records/resources/config.py index 192ab61ad..c34623f4e 100644 --- a/invenio_rdm_records/resources/config.py +++ b/invenio_rdm_records/resources/config.py @@ -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 @@ -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, + ) #