forked from catherinedevlin/ipython-sql
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
325 additions
and
250 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,39 @@ | ||
import sqlglot | ||
import sqlparse | ||
|
||
SYNTAX_ERROR = "\nLooks like there is a syntax error in your query." | ||
ORIGINAL_ERROR = "\nOriginal error message from DB driver:\n" | ||
CTE_MSG = ( | ||
"If using snippets, you may pass the --with argument explicitly.\n" | ||
"For more details please refer : " | ||
"https://jupysql.ploomber.io/en/latest/compose.html#with-argument" | ||
) | ||
|
||
|
||
def parse_sqlglot_error(e, q): | ||
def _is_syntax_error(error): | ||
""" | ||
Function to parse the error message from sqlglot | ||
Parameters | ||
---------- | ||
e: sqlglot.errors.ParseError, exception | ||
while parsing through sqlglot | ||
q : str, user query | ||
Returns | ||
------- | ||
str | ||
Formatted error message containing description | ||
and positions | ||
Function to detect whether error message from DB driver | ||
is related to syntax error in user query. | ||
""" | ||
err = e.errors | ||
position = "" | ||
for item in err: | ||
position += ( | ||
f"Syntax Error in {q}: {item['description']} at " | ||
f"Line {item['line']}, Column {item['col']}\n" | ||
) | ||
msg = "Possible reason: \n" + position if position else "" | ||
return msg | ||
error_lower = error.lower() | ||
return ( | ||
"syntax error" in error_lower | ||
or ("catalog error" in error_lower and "does not exist" in error_lower) | ||
or "error in your sql syntax" in error_lower | ||
or "incorrect syntax" in error_lower | ||
or "not found" in error_lower | ||
) | ||
|
||
|
||
def detail(original_error, query=None): | ||
def detail(original_error): | ||
original_error = str(original_error) | ||
return_msg = SYNTAX_ERROR | ||
if "syntax error" in original_error: | ||
query_list = sqlparse.split(query) | ||
for q in query_list: | ||
try: | ||
q = q.strip() | ||
q = q[:-1] if q.endswith(";") else q | ||
parse = sqlglot.transpile(q) | ||
suggestions = "" | ||
if q.upper() not in [suggestion.upper() for suggestion in parse]: | ||
suggestions += f"Did you mean : {parse}\n" | ||
return_msg = ( | ||
return_msg + "Possible reason: \n" + suggestions | ||
if suggestions | ||
else return_msg | ||
) | ||
|
||
except sqlglot.errors.ParseError as e: | ||
parse_msg = parse_sqlglot_error(e, q) | ||
return_msg = return_msg + parse_msg if parse_msg else return_msg | ||
|
||
return return_msg + "\n" + ORIGINAL_ERROR + original_error + "\n" | ||
if _is_syntax_error(original_error): | ||
return f"{CTE_MSG}\n\n{ORIGINAL_ERROR}{original_error}\n" | ||
|
||
if "fe_sendauth: no password supplied" in original_error: | ||
return ( | ||
"\nLooks like you have run into some issues. " | ||
"Review our DB connection via URL strings guide: " | ||
"https://jupysql.ploomber.io/en/latest/connecting.html ." | ||
" Using Ubuntu? Check out this guide: " | ||
"https://help.ubuntu.com/community/PostgreSQL#fe_sendauth:_" | ||
"no_password_supplied\n" + ORIGINAL_ERROR + original_error + "\n" | ||
) | ||
specific_error = """\nLooks like you have run into some issues. | ||
Review our DB connection via URL strings guide: | ||
https://jupysql.ploomber.io/en/latest/connecting.html . | ||
Using Ubuntu? Check out this guide: " | ||
https://help.ubuntu.com/community/PostgreSQL#fe_sendauth:_ | ||
no_password_supplied\n""" | ||
|
||
return f"{specific_error}\n{ORIGINAL_ERROR}{original_error}\n" | ||
|
||
return None |
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
Oops, something went wrong.