Skip to content

Commit

Permalink
Improved exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Benyaming committed Jul 7, 2021
1 parent 9b1a65c commit 272a36c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
16 changes: 16 additions & 0 deletions israel_transport_api/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pydantic import BaseModel


class ErrorCode:
stop_not_found: 1
route_not_found: 2


class Error(BaseModel):
code: int
message: str


class ApiException(Exception, BaseModel):
code: int
message: str
9 changes: 5 additions & 4 deletions israel_transport_api/gtfs/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import HTTPException
from pydantic import BaseModel


class GtfsFileNotFound(Exception):
Expand All @@ -7,11 +8,11 @@ class GtfsFileNotFound(Exception):

class StopNotFound(Exception):
def __init__(self, stop_code):
msg = f'Stop with code [{stop_code}] not found!'
raise HTTPException(404, msg)
raise HTTPException(422, {'message': f'Stop with code [{stop_code}] not found!', 'code': 1})


class RouteNotFound(Exception):
def __init__(self, stop_code):
msg = f'Route with id [{stop_code}] not found!'
raise HTTPException(404, msg)
raise HTTPException(422, {'message': f'Route with id [{stop_code}] not found!', 'code': 2})


2 changes: 1 addition & 1 deletion israel_transport_api/gtfs/gtfs_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
async def _download_gtfs_data_from_ftp() -> io.BytesIO:
logger.debug(f'Trying to establish ftp connection with {GTFS_URL}...')

async with aioftp.Client.context(GTFS_URL, ) as ftp:
async with aioftp.Client.context(GTFS_URL) as ftp:
bio = io.BytesIO()

logger.debug('Downloading zip file...')
Expand Down
8 changes: 6 additions & 2 deletions israel_transport_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ async def on_startup():

if __name__ == '__main__':
uvicorn.run(
app, host='0.0.0.0' if os.getenv('DOCKER_MODE') else '127.0.0.1', port=8000, use_colors=True,
log_level=logging.DEBUG, log_config='../uvicorn_logger.json'
app,
host='0.0.0.0' if os.getenv('DOCKER_MODE') else '127.0.0.1',
port=8000,
use_colors=True,
log_level=logging.DEBUG,
log_config='../uvicorn_logger.json'
)
8 changes: 4 additions & 4 deletions israel_transport_api/siri/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from israel_transport_api.config import SIRI_URL, API_KEY
from israel_transport_api.gtfs.repository import routes_repository, stops_repository
from israel_transport_api.misc import http_client
from israel_transport_api.siri.exceptions import SiriException
from israel_transport_api.siri.models import IncomingRoute, IncomingRoutesResponse
from israel_transport_api.siri.siri_models import MonitoredStopVisit

Expand All @@ -25,12 +26,11 @@ async def _make_request(stop_code: int, monitoring_interval: int) -> List[Monito
raw_stop_data: List[dict] = raw_data.get('Siri', {}).get('ServiceDelivery', {}).get('StopMonitoringDelivery', [])

if len(raw_stop_data) == 0:
print('no data')
raise ValueError()
raise SiriException('No data received', 3)

if raw_stop_data[0]['Status'] != 'true':
print('error', raw_stop_data)
raise ValueError()
message = raw_stop_data[0].get('ErrorCondition', {}).get('Description')
raise SiriException(message, 1)

parsed_data = parse_obj_as(List[MonitoredStopVisit], raw_stop_data[0]['MonitoredStopVisit']) # currently support for one stop code
return parsed_data
Expand Down
7 changes: 7 additions & 0 deletions israel_transport_api/siri/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fastapi import HTTPException


class SiriException(Exception):
def __init__(self, message: str, code: int):
raise HTTPException(422, {'message': message, 'code': code})

0 comments on commit 272a36c

Please sign in to comment.