guidance-for-self-healing-code-on-aws
guidance-for-self-healing-code-on-aws copied to clipboard
func update_source_code need to be fixed
trafficstars
Fixed function code
def update_source_code(files, repo_dir, format_code=True):
"""Overwrite files in target repo."""
logger.info(f"Updating source code in {repo_dir}")
for file in files:
filename = os.path.join(repo_dir, file["filename"])
try:
if format_code:
contents = format(file["contents"])
else:
contents = file["contents"]
with open(filename, "w") as f:
logger.info(f'Writing to {file["filename"]}')
f.write(contents)
except Exception as e:
logger.error(f'Failed to update {filename}: {str(e)}')
Issue example (The reason)
when I tried to put an error code in my repo, and this workflow comes to fix_code.py, it returns a parse error:
def test_endpoint():
try:
result = 1 / 0 # here is the error test
logger.info('Received request on /api/test')
response = {'message': 'Hello, this is a test endpoint!'}
logger.info('Sending response: %s', response)
return jsonify(response)
and I got the parse error like the following screenshot:
[ERROR] InvalidInput: Cannot parse: 1:45: @app.route('/api/get_order', methods=['GET'])def test_endpoint(): try: result = 1 / 0 # 引發除以零的異常 logger.info('Received request on /api/test') response = {'message': 'Hello, this is a test endpoint!'} logger.info('Sending response: %s', response) return jsonify(response) except ZeroDivisionError: logger.error('Tried to divide by zero', exc_info=True) return jsonify({'error': 'Bad Request'}), 400 except Exception as e: logger.error('Error occurred: %s', str(e), exc_info=True) return jsonify({'error': 'Internal Server Error'}), 500
Traceback (most recent call last):
File "/var/task/handlers/fix_code.py", line 70, in handler
update_source_code(result["source_code"], target_repo_dir)
File "/var/task/source_code.py", line 33, in update_source_code
contents = format(file["contents"])
File "/var/task/source_code.py", line 41, in format
return format_str(content, mode=FileMode())
File "/var/task/black/__init__.py", line 1079, in format_str
dst_contents = _format_str_once(src_contents, mode=mode)
File "/var/task/black/__init__.py", line 1089, in _format_str_once
src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
File "/var/task/black/parsing.py", line 127, in lib2to3_parse
raise exc from None
Best Regards, eCloudValley Solutions Architect - Rich Guo [email protected]