Add error handling for out-of-range relevant_lines_start and missing head_file in pr_code_suggestions.py

This commit is contained in:
mrT23
2024-08-13 12:57:31 +03:00
parent a2fb415c53
commit 26f3bd8900

View File

@ -450,8 +450,24 @@ class PRCodeSuggestions:
original_initial_line = None
for file in self.diff_files:
if file.filename.strip() == relevant_file:
if file.head_file: # in bitbucket, head_file is empty. toDo: fix this
original_initial_line = file.head_file.splitlines()[relevant_lines_start - 1]
if file.head_file:
file_lines = file.head_file.splitlines()
if len(file_lines) >= relevant_lines_start:
get_logger().warning(
"Could not dedent code snippet, because relevant_lines_start is out of range",
artifact={'filename': file.filename,
'file_content': file.head_file,
'relevant_lines_start': relevant_lines_start,
'new_code_snippet': new_code_snippet})
return new_code_snippet
else:
original_initial_line = file_lines[relevant_lines_start - 1]
else:
get_logger().warning("Could not dedent code snippet, because head_file is missing",
artifact={'filename': file.filename,
'relevant_lines_start': relevant_lines_start,
'new_code_snippet': new_code_snippet})
return new_code_snippet
break
if original_initial_line:
suggested_initial_line = new_code_snippet.splitlines()[0]
@ -461,7 +477,7 @@ class PRCodeSuggestions:
if delta_spaces > 0:
new_code_snippet = textwrap.indent(new_code_snippet, delta_spaces * " ").rstrip('\n')
except Exception as e:
get_logger().error(f"Could not dedent code snippet for file {relevant_file}, error: {e}")
get_logger().error(f"Error when dedenting code snippet for file {relevant_file}, error: {e}")
return new_code_snippet