Skip to content

Commit

Permalink
fix: revert structured err logging and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
heitorlessa committed Jul 21, 2018
1 parent 6068036 commit 1d96859
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,10 @@ def lambda_handler(event, context):
try:
ip = requests.get("http://checkip.amazonaws.com/")
except requests.RequestException as e:
error = {
"lambda_request_id": context.aws_request_id,
"lambda_log_group": context.log_group_name,
"lambda_log_stream": context.log_stream_name,
"apigw_request_id": event["requestContext"]["requestId"],
"error_message": str(e.args),
}

# Send some context about this error to Lambda Logs
print(json.dumps(error, indent=4))
print(e)

return {
"statusCode": 500,
"body": json.dumps(
{
"message": "Something went wrong :(",
"request_id": error["apigw_request_id"],
}
),
}
raise e

return {
"statusCode": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,6 @@
from hello_world import app


@pytest.fixture()
def lambda_context():
""" Generates a dummy Lamba context """

context = namedtuple(
"context",
[
"aws_request_id",
"client_context",
"function_name",
"function_version",
"identity",
"invoked_function_arn",
"log_group_name",
"log_stream_name",
"memory_limit_in_mb",
],
)

context.aws_request_id = "a1c0d36a-4371-457a-a8d4-8cda6958244e"
context.client_context = None
context.function_name = "test"
context.function_version = "$LATEST"
context.identity = None
context.invoked_function_arn = "arn:aws:lambda:eu-west-1:738236477645:function:test"
context.log_group_name = "/aws/lambda/test"
context.log_stream_name = "2018/7/12/[$LATEST]5979abf592037c9f"
context.memory_limit_in_mb = "128"

return context


@pytest.fixture()
def apigw_event():
""" Generates API GW Event"""
Expand Down Expand Up @@ -111,18 +79,3 @@ def test_lambda_handler(apigw_event, mocker):

data = json.loads(ret["body"])
assert data["message"] == "hello world"


def test_lambda_handler_error(apigw_event, lambda_context, mocker):

mocker.patch.object(app.requests, 'get',
side_effect=app.requests.RequestException)
ret = app.lambda_handler(apigw_event, lambda_context)

assert ret["statusCode"] == 500

for key in ("message", "request_id"):
assert key in ret["body"]

data = json.loads(ret["body"])
assert data["message"] == "Something went wrong :("

0 comments on commit 1d96859

Please sign in to comment.