Skip to content

Commit

Permalink
Merge pull request #4 from cuenca-mx/refactoring_error_handling
Browse files Browse the repository at this point in the history
Solved response making
  • Loading branch information
Ricardo committed May 5, 2020
2 parents ed000e5 + 34b1645 commit 2843fa6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
16 changes: 8 additions & 8 deletions chalicelib/resources/arcus_read_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from arcus.client import Client
from arcus.exc import InvalidAuth
from chalice import Response

from .base import app

Expand All @@ -12,10 +13,6 @@
topup_secret_key: str = os.environ['TOPUP_SECRET_KEY']


def make_response(status_code: int, body: dict) -> dict:
return {'statusCode': status_code, 'body': json.dumps(body)}


@app.route('/{event}', methods=['GET'])
def lambda_handler(event: str) -> dict:
request = app.current_request
Expand All @@ -38,10 +35,13 @@ def lambda_handler(event: str) -> dict:
response['topup'] = vars(response['topup'])
else:
response = client.get(f'/{path}')
return response
return Response(body=response, status_code=200,)
except InvalidAuth:
return make_response(401, dict(message='Invalid Authentication Token'))
return Response(
body=dict(message='Invalid Authentication Token'), status_code=401,
)
except Exception as ex:
return make_response(
400, dict(message='Bad Request', exception=str(ex))
return Response(
body=dict(message='Bad Request', exception=str(ex)),
status_code=400,
)
14 changes: 4 additions & 10 deletions tests/test_read_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ def test_catch_exception(client: RequestHandler) -> None:
headers: dict = {'X-ARCUS-SANDBOX': 'true'}
response = client.get('/test?page=1', headers=headers)

assert response.status_code == 200
assert response.json["statusCode"] == 400
assert "body" in response.json
resp: dict = json.loads(response.json["body"])
assert resp["message"] == "Bad Request"
assert response.status_code == 400
assert response.json["message"] == "Bad Request"


def test_catch_exception_auth(client: RequestHandler) -> None:
Expand All @@ -75,8 +72,5 @@ def test_catch_exception_auth(client: RequestHandler) -> None:
headers: dict = {'X-ARCUS-SANDBOX': 'true'}
response = client.get('/test?page=1', headers=headers)

assert response.status_code == 200
assert response.json["statusCode"] == 401
assert "body" in response.json
resp: dict = json.loads(response.json["body"])
assert resp["message"] == "Invalid Authentication Token"
assert response.status_code == 401
assert response.json["message"] == "Invalid Authentication Token"

0 comments on commit 2843fa6

Please sign in to comment.