Skip to content

Commit

Permalink
fix: add error handling to query
Browse files Browse the repository at this point in the history
  • Loading branch information
engisalor committed Nov 22, 2023
1 parent 9ce74be commit b98d0e2
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions pages/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dash
import dash_bootstrap_components as dbc
import pandas as pd
from aiohttp import ClientConnectionError
from dash import ALL, Input, Output, State, ctx, dcc, get_app, html
from flask import request
from sgex.job import Job
Expand Down Expand Up @@ -339,13 +340,15 @@ def send_requests(input_text, corpora, attribute):
j = Job(params=calls, **env.sgex)
j.run()
dfs = []
for call in j.data.freqs:
df = call.df_from_json()
if not df.empty:
df["params"] = json.dumps(call.params)
df["query"] = df["arg"].replace(query_map)
dfs.append(df)
return pd.concat(dfs)
if not j.errors:
for call in j.data.freqs:
df = call.df_from_json()
if not df.empty:
df["params"] = json.dumps(call.params)
df["query"] = df["arg"].replace(query_map)
dfs.append(df)
dfs = pd.concat(dfs)
return dfs, j.errors


@dash.callback(
Expand Down Expand Up @@ -383,12 +386,27 @@ def run_query(
return html.P("No duplicate queries", className="lead"), None, []
if len(queries) > env.MAX_QUERIES:
return (
html.P(f"{env.MAX_QUERIES} > queries supported", className="lead"),
html.P(f"{env.MAX_QUERIES} >= queries supported", className="lead"),
None,
[],
)
# get data
df = send_requests(input_text, corpora, attribute)
df, errors = send_requests(input_text, corpora, attribute)
# handle errors
if errors:
if isinstance(errors[0][0], ClientConnectionError):
return html.P("Server connection error", className="lead"), None, []
elif isinstance(errors[0][0], str):
return (
html.P(
f"Server syntax error: {set(([x[0] for x in errors]))}",
className="lead",
),
None,
[],
)
else:
return html.P("Unknown error", className="lead"), None, []
if df.empty:
return html.P("Nothing found", className="lead"), None, []
# manage filter
Expand Down

0 comments on commit b98d0e2

Please sign in to comment.