Created
March 21, 2018 03:33
-
-
Save neeraj9/970e73412a893ddaed7be31e84199c29 to your computer and use it in GitHub Desktop.
BeamParticle Python Dynamic function for /post/ HTTP POST JSON handler with sample logs and exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
# | |
# py_test_stdout_stderr_with_exceptiuon | |
# | |
# Author: Neeraj Sharma | |
""" | |
Demonstrate logging via stdout and stderr. Take special note of | |
usage of traceback, which gives very nicely formatted call | |
traceback in case of errors. | |
""" | |
import json | |
import sys | |
import traceback | |
from Pyrlang import term | |
def main(data_binary, context_binary): | |
data = data_binary.bytes_.decode('utf-8') | |
context = context_binary.bytes_.decode('utf-8') | |
result = handle_event(data, context) | |
return term.Binary(result.encode('utf-8')) | |
def handle_event(data, context): | |
try: | |
json_value = json.loads(data) | |
message = json_value['message'] | |
sys.stdout.write('Sample log message on stdout. Received message={}\n'.format(message)) | |
json_response = process_freetext(message) | |
return json.dumps(json_response) | |
except Exception as e: | |
sys.stderr.write('Sample log message on stderr. Received message={}\n'.format(e)) | |
traceback_lines = traceback.format_exception(None, e, e.__traceback__) | |
print(''.join(traceback_lines), | |
file=sys.stderr, flush=True) | |
return str(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment