Skip to content

Commit

Permalink
SQLachemy error overriding
Browse files Browse the repository at this point in the history
Test

Lint

changelog

util

moved error_message

sqlglot
  • Loading branch information
neelasha23 committed Apr 20, 2023
1 parent 617333b commit 4d31eda
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,4 @@ Converted from an IPython Plugin to an Extension for 1.0 compatibility

*Release date: 21-Mar-2013*

* Initial release
* Initial release
33 changes: 33 additions & 0 deletions src/sql/error_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sqlglot


def detail(msg, query):
msg = str(msg)
return_msg = f"Looks like there is some syntax error " f"in your query : {query} \n"
if "syntax error" in msg:
try:
parse = sqlglot.transpile(query)
if query.upper().replace(";", "").strip() not in [
suggestion.upper() for suggestion in parse
]:
return return_msg + f"Did you mean : {parse} "
except sqlglot.errors.ParseError as e:
err = e.errors
for item in err:
return_msg += (
f"Syntax Error: {item['description']} at "
f"Line {item['line']}, Column {item['col']}\n"
)
return return_msg

if "fe_sendauth: no password supplied" in msg:
return (
"Looks 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"
)

return None
10 changes: 9 additions & 1 deletion src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ipywidgets import interact
except ModuleNotFoundError:
interact = None
from ploomber_core.exceptions import modify_exceptions
from ploomber_core.exceptions import modify_exceptions, COMMUNITY
from IPython.core.magic import (
Magics,
cell_magic,
Expand All @@ -27,6 +27,7 @@
from sql.magic_cmd import SqlCmdMagic
from ploomber_core.dependencies import check_installed

from sql.error_message import detail
from traitlets.config.configurable import Configurable
from traitlets import Bool, Int, Unicode, Dict, observe

Expand Down Expand Up @@ -406,10 +407,17 @@ def interactive_execute_wrapper(**kwargs):
# JA: added DatabaseError for MySQL
except (ProgrammingError, OperationalError, DatabaseError) as e:
# Sqlite apparently return all errors as OperationalError :/
detailed_msg = detail(e, command.sql)

if self.short_errors:
print(e)
if detailed_msg is not None:
print(detailed_msg)
print(COMMUNITY)
else:
if detailed_msg is not None:
print(detailed_msg)
print(COMMUNITY)
raise

legal_sql_identifier = re.compile(r"^[A-Za-z0-9#_$]+")
Expand Down
73 changes: 73 additions & 0 deletions src/tests/test_error_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest

from sqlalchemy.exc import OperationalError


def test_syntax_error_no_suggestion(ip, capsys):
ip.run_cell_magic(
"sql",
"",
"""
sqlite://
SELECT FROM author;
""",
)
out, _ = capsys.readouterr()
assert '(sqlite3.OperationalError) near "FROM": syntax error' in out
assert (
"If you need help solving this issue, "
"send us a message: https://ploomber.io/community" in out
)


def test_syntax_error_description(ip, capsys):
ip.run_cell_magic(
"sql",
"",
"""
sqlite://
SELECT first_(name FROM author;
""",
)
out, _ = capsys.readouterr()
assert '(sqlite3.OperationalError) near "FROM": syntax error' in out
assert "Syntax Error: Expecting ) at Line 1, Column 20" in out
assert (
"If you need help solving this issue, "
"send us a message: https://ploomber.io/community" in out
)


def test_syntax_error_suggestion(ip, capsys):
ip.run_cell_magic(
"sql",
"",
"""
sqlite://
ALTER TABLE author RENAME new_author;
""",
)
out, _ = capsys.readouterr()
assert "Did you mean : ['ALTER TABLE author RENAME TO new_author']" in out
assert (
"If you need help solving this issue, "
"send us a message: https://ploomber.io/community" in out
)


def test_query_syntax_error(ip, capsys):
ip.run_line_magic("config", "SqlMagic.short_errors = False")
with pytest.raises(OperationalError):
ip.run_cell_magic(
"sql",
"",
"""
sqlite://
SELECT FROM author;
""",
)
out, _ = capsys.readouterr()
assert (
"If you need help solving this issue, "
"send us a message: https://ploomber.io/community" in out
)
1 change: 1 addition & 0 deletions src/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import polars as pl
import pytest
from sqlalchemy import create_engine

from IPython.core.error import UsageError
from sql.connection import Connection
from sql.magic import SqlMagic
Expand Down

0 comments on commit 4d31eda

Please sign in to comment.