Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9b2433e

Browse filesBrowse files
authored
Merge pull request #58 from danielthepope/handle-exceptions
Handle exceptions by using traceback.format_exc instead of extract_tb
2 parents b8a078c + 392cb6e commit 9b2433e
Copy full SHA for 9b2433e

File tree

Expand file treeCollapse file tree

2 files changed

+37
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+37
-2
lines changed

‎lambda_local/main.py

Copy file name to clipboardExpand all lines: lambda_local/main.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def execute(func, event, context):
124124
err = sys.exc_info()
125125
result = json.dumps({
126126
"errorMessage": str(err[1]),
127-
"stackTrace": traceback.extract_tb(err[2]),
127+
"stackTrace": traceback.format_tb(err[2]),
128128
"errorType": err[0].__name__
129129
}, indent=4, separators=(',', ': '))
130130
err_type = ERR_TYPE_EXCEPTION

‎tests/test_direct_invocations.py

Copy file name to clipboardExpand all lines: tests/test_direct_invocations.py
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
python-lambda-local: Test Direct Inovactions
2+
python-lambda-local: Test Direct Invocations
33
(command-line and direct).
44
55
Meant for use with py.test.
@@ -13,6 +13,7 @@
1313
import os
1414
from lambda_local.main import run as lambda_run
1515
from lambda_local.main import call as lambda_call
16+
from lambda_local.main import ERR_TYPE_EXCEPTION
1617
from lambda_local.context import Context
1718

1819

@@ -21,6 +22,10 @@ def my_lambda_function(event, context):
2122
return 42
2223

2324

25+
def my_failing_lambda_function(event, context):
26+
raise Exception('Oh no')
27+
28+
2429
def test_function_call_for_pytest():
2530
(result, error_type) = lambda_call(
2631
my_lambda_function, {}, Context(1))
@@ -30,6 +35,13 @@ def test_function_call_for_pytest():
3035
assert result == 42
3136

3237

38+
def test_handle_exceptions_gracefully():
39+
(result, error_type) = lambda_call(
40+
my_failing_lambda_function, {}, Context(1))
41+
42+
assert error_type is ERR_TYPE_EXCEPTION
43+
44+
3345
def test_check_command_line():
3446
request = json.dumps({})
3547
request_file = 'check_command_line_event.json'
@@ -51,3 +63,26 @@ def test_check_command_line():
5163

5264
os.remove(request_file)
5365
assert p.exitcode == 0
66+
67+
68+
def test_check_command_line_error():
69+
request = json.dumps({})
70+
request_file = 'check_command_line_event.json'
71+
with open(request_file, "w") as f:
72+
f.write(request)
73+
74+
args = argparse.Namespace(event=request_file,
75+
file='tests/test_direct_invocations.py',
76+
function='my_failing_lambda_function',
77+
timeout=1,
78+
environment_variables='',
79+
library=None,
80+
version_name='',
81+
arn_string=''
82+
)
83+
p = Process(target=lambda_run, args=(args,))
84+
p.start()
85+
p.join()
86+
87+
os.remove(request_file)
88+
assert p.exitcode == 1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.