Skip to content

Commit

Permalink
refactor: wraps 500 and 504 erros to specific exceptions
Browse files Browse the repository at this point in the history
It was created specific exceptions for each status code: InternalServerErrorException for status 500 and GatewayTimeoutException for status 504. Additionally, it was created a base exception class, ServerErrorException, that both specific exceptions will inherit from.
  • Loading branch information
danielnsilva committed Jul 7, 2024
1 parent 4d88364 commit 76645ef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
10 changes: 7 additions & 3 deletions semanticscholar/ApiRequester.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from tenacity import retry_if_exception_type, stop_after_attempt, wait_fixed

from semanticscholar.SemanticScholarException import (
BadQueryParametersException, ObjectNotFoundException)
BadQueryParametersException, GatewayTimeoutException,
InternalServerErrorException, ObjectNotFoundException)

logger = logging.getLogger('semanticscholar')

Expand Down Expand Up @@ -132,9 +133,12 @@ async def _get_data_async(
raise ObjectNotFoundException(data['error'])
elif r.status_code == 429:
raise ConnectionRefusedError('HTTP status 429 Too Many Requests.')
elif r.status_code in [500, 504]:
elif r.status_code == 500:
data = r.json()
raise Exception(data['message'])
raise InternalServerErrorException(data['message'])
elif r.status_code == 504:
data = r.json()
raise GatewayTimeoutException(data['message'])

return data

Expand Down
9 changes: 9 additions & 0 deletions semanticscholar/SemanticScholarException.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ class ObjectNotFoundException(SemanticScholarException):

class NoMorePagesException(SemanticScholarException):
'''No more pages to fetch.'''

class ServerErrorException(SemanticScholarException):
'''A base class for HTTP Status Code 5xx errors.'''

class InternalServerErrorException(ServerErrorException):
'''HTTP Status Code 500.'''

class GatewayTimeoutException(ServerErrorException):
'''HTTP Status Code 504.'''
59 changes: 58 additions & 1 deletion tests/test_semanticscholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import sys
import unittest
from datetime import datetime
from unittest import mock

import httpx
import vcr
from httpx import TimeoutException

Expand All @@ -18,7 +20,9 @@
from semanticscholar.Reference import Reference
from semanticscholar.SemanticScholar import SemanticScholar
from semanticscholar.SemanticScholarException import (
BadQueryParametersException, NoMorePagesException, ObjectNotFoundException)
BadQueryParametersException, GatewayTimeoutException,
InternalServerErrorException, NoMorePagesException,
ObjectNotFoundException, ServerErrorException)
from semanticscholar.Tldr import Tldr

test_vcr = vcr.VCR(
Expand Down Expand Up @@ -580,6 +584,33 @@ def test_debug(self):
self.sch.get_papers(list_of_paper_ids)
self.assertIn(expected_output, log.output)

@mock.patch('httpx.AsyncClient.request')
def test_exception_internal_server_error(self, mock_request):
mock_response = httpx.Response(
status_code=500, json={'message': 'message'})
mock_request.return_value = mock_response
with self.assertRaises(InternalServerErrorException):
self.sch.get_paper('10.1093/mind/lix.236.433')

@mock.patch('httpx.AsyncClient.request')
def test_exception_gateway_timeout(self, mock_request):
mock_response = httpx.Response(
status_code=504, json={'message': 'message'})
mock_request.return_value = mock_response
with self.assertRaises(GatewayTimeoutException):
self.sch.get_paper('10.1093/mind/lix.236.433')

@mock.patch('httpx.AsyncClient.request')
def test_exception_server_error(self, mock_request):
error_status = [500, 504]
for status_code in error_status:
mock_response = httpx.Response(
status_code=status_code, json={'message': 'message'})
mock_request.return_value = mock_response
with self.assertRaises(ServerErrorException):
self.sch.get_paper('10.1093/mind/lix.236.433')


class AsyncSemanticScholarTest(unittest.IsolatedAsyncioTestCase):

def setUp(self) -> None:
Expand Down Expand Up @@ -938,6 +969,32 @@ async def test_limit_value_exceeded_async(self):
await method(query, limit=0)
self.assertEqual(str(context.exception), error_message)

@mock.patch('httpx.AsyncClient.request')
async def test_exception_internal_server_error_async(self, mock_request):
mock_response = httpx.Response(
status_code=500, json={'message': 'message'})
mock_request.return_value = mock_response
with self.assertRaises(InternalServerErrorException):
await self.sch.get_paper('10.1093/mind/lix.236.433')

@mock.patch('httpx.AsyncClient.request')
async def test_exception_gateway_timeout_async(self, mock_request):
mock_response = httpx.Response(
status_code=504, json={'message': 'message'})
mock_request.return_value = mock_response
with self.assertRaises(GatewayTimeoutException):
await self.sch.get_paper('10.1093/mind/lix.236.433')

@mock.patch('httpx.AsyncClient.request')
async def test_exception_server_error_async(self, mock_request):
error_status = [500, 504]
for status_code in error_status:
mock_response = httpx.Response(
status_code=status_code, json={'message': 'message'})
mock_request.return_value = mock_response
with self.assertRaises(ServerErrorException):
await self.sch.get_paper('10.1093/mind/lix.236.433')


if __name__ == '__main__':
unittest.main()

0 comments on commit 76645ef

Please sign in to comment.