Repository: ploomber/jupysql
Pull request #817 (2023-08-19 15:14:44)

fixes error that was converted into print
## Describe your changes

## Issue number

Closes #X

## Checklist before requesting a review

- [ ] Performed a self-review of my code
- [ ] Formatted my code with [`pkgmt format`](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#linting-formatting)
- [ ] Added [tests](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#testing) (when necessary).
- [ ] Added [docstring](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#documenting-changes-and-new-features) documentation and update the [changelog](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#changelog) (when needed)



<!-- readthedocs-preview jupysql start -->
----
:books: Documentation preview :books:: https://jupysql--817.org.readthedocs.build/en/817/

<!-- readthedocs-preview jupysql end -->


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 912483d..4a55c6c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@
 * [API Change] When loading connections from a `.ini` file via `%sql --section section_name`, the section name is set as the connection alias
 * [API Change] Starting connections from a `.ini` file via `%sql [section_name]` has been deprecated
 * [Doc] Fixes documentation inaccuracy that said `:variable` was deprecated (we brought it back in `0.9.0`)
+* [Fix] Fix an error that caused a connection error message to be turned into a `print` statement
 * [Fix] Fix Twice message printing when switching to the current connection (#772)
 * [Fix] Error when using %sqlplot in snowflake (#697)
 
diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py
index fb4f78f..ec5ae5f 100644
--- a/src/sql/connection/connection.py
+++ b/src/sql/connection/connection.py
@@ -879,9 +879,9 @@ class SQLAlchemyConnection(AbstractConnection):
         except OperationalError as e:
             detailed_msg = detail(e)
             if detailed_msg is not None:
-                raise exceptions.UsageError(detailed_msg)
+                raise exceptions.RuntimeError(detailed_msg) from e
             else:
-                print(e)
+                raise exceptions.RuntimeError(str(e)) from e
         except Exception as e:
             raise _error_invalid_connection_info(e, connect_str) from e
 
diff --git a/src/sql/parse.py b/src/sql/parse.py
index 9c361de..cead924 100644
--- a/src/sql/parse.py
+++ b/src/sql/parse.py
@@ -30,7 +30,7 @@ def connection_str_from_dsn_section(section, config):
         cfg_content = dsn_file.read_text()
     except FileNotFoundError as e:
         raise exceptions.FileNotFoundError(
-            f"%config SqlMagic.dsn_filename ({config.dsn_filename!r}) not found."
+            f"%config SqlMagic.dsn_filename ({str(config.dsn_filename)!r}) not found."
             " Ensure the file exists or change the configuration: "
             "%config SqlMagic.dsn_filename = 'path/to/file.ini'"
         ) from e


Test patch:
diff --git a/src/tests/test_connection.py b/src/tests/test_connection.py
index eb3fc55..3dd170d 100644
--- a/src/tests/test_connection.py
+++ b/src/tests/test_connection.py
@@ -1022,3 +1022,23 @@ def test_ignore_operationalerror_if_it_doesnt_match_the_selected_patterns(monkey
     assert "(test_connection.SomeError) message" in str(excinfo.value)
     assert isinstance(excinfo.value.orig, SomeError)
     assert str(excinfo.value.orig) == "message"
+
+
+@pytest.mark.parametrize(
+    "uri, expected",
+    [
+        (
+            "sqlite:///path/to.db",
+            "unable to open database file",
+        ),
+        (
+            "duckdb:///path/to.db",
+            "Cannot open file",
+        ),
+    ],
+)
+def test_database_in_directory_that_doesnt_exist(tmp_empty, uri, expected):
+    with pytest.raises(UsageError) as excinfo:
+        SQLAlchemyConnection(engine=create_engine(uri))
+
+    assert expected in str(excinfo.value)