diff --git a/lambda_local/main.py b/lambda_local/main.py index f3ff166..1f5f83f 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -124,7 +124,7 @@ def execute(func, event, context): err = sys.exc_info() result = json.dumps({ "errorMessage": str(err[1]), - "stackTrace": traceback.extract_tb(err[2]), + "stackTrace": traceback.format_tb(err[2]), "errorType": err[0].__name__ }, indent=4, separators=(',', ': ')) err_type = ERR_TYPE_EXCEPTION diff --git a/tests/test_direct_invocations.py b/tests/test_direct_invocations.py index c496a4d..b49eaf7 100644 --- a/tests/test_direct_invocations.py +++ b/tests/test_direct_invocations.py @@ -1,5 +1,5 @@ ''' -python-lambda-local: Test Direct Inovactions +python-lambda-local: Test Direct Invocations (command-line and direct). Meant for use with py.test. @@ -13,6 +13,7 @@ import os from lambda_local.main import run as lambda_run from lambda_local.main import call as lambda_call +from lambda_local.main import ERR_TYPE_EXCEPTION from lambda_local.context import Context @@ -21,6 +22,10 @@ def my_lambda_function(event, context): return 42 +def my_failing_lambda_function(event, context): + raise Exception('Oh no') + + def test_function_call_for_pytest(): (result, error_type) = lambda_call( my_lambda_function, {}, Context(1)) @@ -30,6 +35,13 @@ def test_function_call_for_pytest(): assert result == 42 +def test_handle_exceptions_gracefully(): + (result, error_type) = lambda_call( + my_failing_lambda_function, {}, Context(1)) + + assert error_type is ERR_TYPE_EXCEPTION + + def test_check_command_line(): request = json.dumps({}) request_file = 'check_command_line_event.json' @@ -51,3 +63,26 @@ def test_check_command_line(): os.remove(request_file) assert p.exitcode == 0 + + +def test_check_command_line_error(): + request = json.dumps({}) + request_file = 'check_command_line_event.json' + with open(request_file, "w") as f: + f.write(request) + + args = argparse.Namespace(event=request_file, + file='tests/test_direct_invocations.py', + function='my_failing_lambda_function', + timeout=1, + environment_variables='', + library=None, + version_name='', + arn_string='' + ) + p = Process(target=lambda_run, args=(args,)) + p.start() + p.join() + + os.remove(request_file) + assert p.exitcode == 1