Commit f3342f90 authored by Pablo Panero's avatar Pablo Panero
Browse files

views: add mapper parsing exceptions handling

parent ef58f1b7
...@@ -13,6 +13,7 @@ import ast ...@@ -13,6 +13,7 @@ import ast
import copy import copy
import os import os
from invenio_oauthclient.contrib import cern from invenio_oauthclient.contrib import cern
from invenio_records_rest import config as irr_config
from .modules.cernsearch.permissions import (record_read_permission_factory, from .modules.cernsearch.permissions import (record_read_permission_factory,
record_create_permission_factory, record_create_permission_factory,
...@@ -113,6 +114,11 @@ RECORDS_REST_ENDPOINTS = dict( ...@@ -113,6 +114,11 @@ RECORDS_REST_ENDPOINTS = dict(
) )
) )
RECORDS_REST_ELASTICSEARCH_ERROR_HANDLERS = copy.deepcopy(
irr_config.RECORDS_REST_ELASTICSEARCH_ERROR_HANDLERS)
RECORDS_REST_ELASTICSEARCH_ERROR_HANDLERS['mapper_parsing_exception'] = \
'cern_search_rest_api.modules.cernsearch.views:elasticsearch_mapper_parsing_exception_handler'
# App # App
# === # ===
......
#!/usr/bin/python
#
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019, CERN
# This software is distributed under the terms of the GNU General Public
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as Intergovernmental Organization
# or submit itself to any jurisdiction.
from invenio_rest.errors import RESTValidationError
class InvalidRecordFormatError(RESTValidationError):
"""Invalid query syntax."""
code = 400
description = 'Invalid query syntax.'
\ No newline at end of file
...@@ -31,9 +31,18 @@ from invenio_records_rest.views import need_record_permission, pass_record ...@@ -31,9 +31,18 @@ from invenio_records_rest.views import need_record_permission, pass_record
from invenio_rest import ContentNegotiatedMethodView from invenio_rest import ContentNegotiatedMethodView
from invenio_search import current_search_client from invenio_search import current_search_client
from invenio_indexer.utils import default_record_to_index from invenio_indexer.utils import default_record_to_index
from cern_search_rest_api.modules.cernsearch.errors import InvalidRecordFormatError
from cern_search_rest_api.modules.cernsearch.search import RecordCERNSearch from cern_search_rest_api.modules.cernsearch.search import RecordCERNSearch
def elasticsearch_mapper_parsing_exception_handler(error):
"""Handle mapper parsing exceptions from ElasticSearch."""
description = 'The format of the record is invalid. {reason}.' \
'{caused_by}'.format(
reason=error.info['error']['root_cause'],
caused_by=error.info['error']['caused_by']['reason']
)
return InvalidRecordFormatError(description=description).get_response()
def create_error_handlers(blueprint): def create_error_handlers(blueprint):
"""Create error handlers on blueprint.""" """Create error handlers on blueprint."""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment