Skip to content

Commit

Permalink
Merge pull request #343 from ral-facilities/bugfix/validation-error-c…
Browse files Browse the repository at this point in the history
…ode-#319

Improve Search API Error Handling
  • Loading branch information
MRichards99 authored Feb 28, 2022
2 parents a3943d1 + 1e42981 commit 982518a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
30 changes: 21 additions & 9 deletions datagateway_api/src/search_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import json
import logging

from pydantic import ValidationError

from datagateway_api.src.common.exceptions import (
BadRequestError,
MissingRecordError,
SearchAPIError,
)
from datagateway_api.src.common.filter_order_handler import FilterOrderHandler
from datagateway_api.src.search_api.filters import (
Expand Down Expand Up @@ -36,20 +39,29 @@ def search_api_error_handling(method):
def wrapper_error_handling(*args, **kwargs):
try:
return method(*args, **kwargs)
except (ValueError, TypeError, AttributeError) as e:
except ValidationError as e:
log.exception(msg=e.args)
assign_status_code(e, 500)
raise SearchAPIError(create_error_message(e))
except (ValueError, TypeError, AttributeError, KeyError) as e:
log.exception(msg=e.args)
raise BadRequestError(create_error_message(BadRequestError()))
assign_status_code(e, 400)
raise BadRequestError(create_error_message(e))
except Exception as e:
log.exception(msg=e.args)
try:
e.status_code
except AttributeError:
# If no status code exists (for non-API defined exceptions), defensively
# assign a 500
e.status_code = 500

# Defensively assign a 500 if the exception doesn't already have a status
# code
assign_status_code(e, 500)
raise type(e)(create_error_message(e))

def assign_status_code(e, status_code):
try:
# If no status code exists (for non-API defined exceptions), assign a status
# code
e.status_code
except AttributeError:
e.status_code = status_code

def create_error_message(e):
return {
"error": {
Expand Down
8 changes: 7 additions & 1 deletion test/search_api/test_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
SearchAPIError,
)
from datagateway_api.src.search_api.helpers import search_api_error_handling
from datagateway_api.src.search_api.models import Document


class TestErrorHandling:
Expand All @@ -24,14 +25,19 @@ class TestErrorHandling:
pytest.param(ValueError, BadRequestError, 400, id="Value error"),
pytest.param(AttributeError, BadRequestError, 400, id="Attribute error"),
pytest.param(ImportError, ImportError, 500, id="Import error"),
pytest.param(Document, SearchAPIError, 500, id="Validation error"),
],
)
def test_valid_error_raised(
self, raised_exception, expected_exception, status_code,
):
@search_api_error_handling
def raise_exception():
raise raised_exception()
if isinstance(raised_exception(), Exception):
raise raised_exception()
else:
# To raise Pydantic ValidationError from object creation
raised_exception()

try:
raise_exception()
Expand Down

0 comments on commit 982518a

Please sign in to comment.