Skip to content

Commit 8884a0d

Browse files
committed
Improve error handling
1 parent 631746f commit 8884a0d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

backend/backend.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,20 @@ def run_sql_query(query_str, db):
6565
env={**os.environ,"LINGODB_PARALLELISM": "4"})
6666
# Parse output and skip first and last 4 lines
6767
splitted = output.split("\n")
68+
execution_has_error=False
69+
if len(splitted) < 2:
70+
execution_has_error=True
6871
header_list = splitted[-2].split()
72+
if not "total" in header_list or not "name" in header_list:
73+
execution_has_error=True
74+
if execution_has_error:
75+
cleaned_output = "\n".join(output.split("\n")[1:])
76+
cleaned_output= cleaned_output.replace(f.name+":", "")
77+
cleaned_output= cleaned_output.replace(f.name, "<query_file>")
78+
raise HTTPException(status_code=400, detail=cleaned_output)
6979
times_list = splitted[-1].split()
7080
query_opt_time=float(times_list[1])
71-
execution_time=float(times_list[-1])
81+
execution_time=float(times_list[-2])
7282
compilation_time = sum(float(t) for t in times_list[2:-1])
7383
result = "\n".join(splitted[1:-4])
7484
table_as_json = table_to_json(raw_table=result)
@@ -81,7 +91,9 @@ def run_sql_query(query_str, db):
8191
except subprocess.CalledProcessError as e:
8292
# Print error message to stderr
8393
print(e.output, file=sys.stderr)
84-
raise HTTPException(status_code=400, detail="Query could not be executed:\n" + e.output)
94+
cleaned_output = e.output.replace(f.name + ":", "")
95+
cleaned_output = cleaned_output.replace(f.name, "<query_file>")
96+
raise HTTPException(status_code=400, detail="Query execution failed (not expected) with following error:\n"+cleaned_output)
8597
except subprocess.TimeoutExpired as e:
8698
raise HTTPException(status_code=400, detail="Query took too long")
8799

0 commit comments

Comments
 (0)