Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #56: catch more errors #57

Merged
merged 1 commit into from
May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/harlequin/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,47 @@ async def watch_query_text(self, query_text: str) -> None:
pane.show_table()
self.data = []

def watch_relation(self, relation: Union[duckdb.DuckDBPyRelation, None]) -> None:
async def watch_relation(
self, relation: Union[duckdb.DuckDBPyRelation, None]
) -> None:
"""
Only runs for select statements, except when first mounted.
"""
# invalidate results so watch_data runs even if the results are the same
self.data = []
if relation is not None:
table = self.query_one(ResultsViewer).get_table()
pane = self.query_one(ResultsViewer)
table = pane.get_table()
short_types = [short_type(t) for t in relation.dtypes]
table.add_columns(
*[
f"{name} [#888888]{type}[/]"
for name, type in zip(relation.columns, short_types)
]
)
self.fetch_relation_data(relation)
try:
worker = self.fetch_relation_data(relation)
await worker.wait()
except WorkerFailed as e:
self.push_screen(
ErrorModal(
title="DuckDB Error",
header=("DuckDB raised an error when " "running your query:"),
error=e.error,
)
)
pane.show_table()
# Textual fails to catch some duckdb Errors,
# so we need this mostly- redundant block.
except duckdb.Error as e:
self.push_screen(
ErrorModal(
title="DuckDB Error",
header=("DuckDB raised an error when " "running your query:"),
error=e,
)
)
pane.show_table()

async def watch_data(self, data: List[Tuple]) -> None:
if data:
Expand Down Expand Up @@ -187,7 +212,7 @@ def build_relation(self, query_text: str) -> Union[duckdb.DuckDBPyRelation, None
relation = self.connection.sql(query_text)
return relation

@work(exclusive=True)
@work(exclusive=True, exit_on_error=False) # type: ignore
def fetch_relation_data(self, relation: duckdb.DuckDBPyRelation) -> None:
log(f"fetch_relation_data {hash(relation)}")
data = relation.fetchall()
Expand Down