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

Send runtime error to the frontend #359

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask_limiter.util import get_remote_address
from flask_talisman import Talisman
from . import csp
from os import environ

limiter = Limiter(
key_func=get_remote_address, default_limits=["2 per second", "1000 per day"]
Expand All @@ -24,6 +25,7 @@ def create_app():
app,
content_security_policy=csp.csp,
content_security_policy_nonce_in=["script-src"],
force_https=not environ.get("FLASK_ENV") == "development",
)

from . import config, routes
Expand Down
15 changes: 13 additions & 2 deletions src/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ def fluff_results():
sql = "\n".join(sql.splitlines()) + "\n"

dialect = request.args["dialect"]
linted = lint(sql, dialect=dialect)
fixed_sql = fix(sql, dialect=dialect)
try:
linted = lint(sql, dialect=dialect)
fixed_sql = fix(sql, dialect=dialect)
except RuntimeError as e:
linted = [
{
"start_line_no": 1,
"start_line_pos": 1,
"code": "RuntimeError",
"description": str(e),
}
]
fixed_sql = sql
return render_template(
"index.html",
results=True,
Expand Down
15 changes: 15 additions & 0 deletions test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from app.routes import sql_encode
from bs4 import BeautifulSoup
from unittest.mock import patch


@pytest.fixture
Expand Down Expand Up @@ -95,6 +96,20 @@ def test_newlines_in_error(client):
)


@patch("app.routes.lint")
def test_runtime_error(mock_lint, client):
"""Test that a runtime error is handled."""
mock_lint.side_effect = RuntimeError("This is a test error")
sql_encoded = sql_encode("select * from table")
rv = client.get("/fluffed", query_string=f"""dialect=ansi&sql={sql_encoded}""")
html = rv.data.decode().lower()
assert "sqlfluff online" in html
assert "fixed sql" in html
assert "select * from table" in html
assert "runtimeerror" in html
assert "this is a test error" in html


def test_security_headers(client):
"""Test flask-talisman is setting the security headers"""
rv = client.get("/")
Expand Down