Skip to content

Commit

Permalink
fix: ui: fix display of syntax and semantic errors
Browse files Browse the repository at this point in the history
Improve display of syntax and semantic errors.

Closes #281
  • Loading branch information
riddell-stan committed May 2, 2021
1 parent 3555144 commit 4fdbfc8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
15 changes: 14 additions & 1 deletion stan/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ async def go():
sampling_output.write_line("<info>Sampling:</info> <error>Initialization failed.</error>")
raise RuntimeError("Initialization failed.")
raise RuntimeError(message)

resp = await client.get(f"/{fit_name}")
if resp.status != 200:
raise RuntimeError((resp.json())["message"])
Expand Down Expand Up @@ -470,7 +471,19 @@ async def go():
resp = task.result()

if resp.status != 201:
raise RuntimeError(resp.json()["message"])
match = re.search(r"""ValueError\(['"](.*)['"]\)""", resp.json()["message"])
if not match: # unknown error, should not happen
raise RuntimeError(resp.json()["message"])
exception_body = match.group(1).encode().decode("unicode_escape")
error_type_match = re.match(r"(Semantic|Syntax) error", exception_body)
if error_type_match:
error_type = error_type_match.group(0)
exception_body_without_first_line = exception_body.split("\n", 1)[1]
building_output.write_line(f"<info>Building:</info> <error>{error_type}:</error>")
building_output.write_line(f"<error>{exception_body_without_first_line}</error>")
raise ValueError(error_type)
else:
raise RuntimeError(exception_body)
building_output.clear()
if model_in_cache:
building_output.write("<info>Building:</info> found in cache, done.")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_build_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Tests for build related exceptions."""
import pytest

import stan


def test_semantic_error():
# wrong number of arguments to `uniform`
program_code = "parameters { real y; } model { y ~ uniform(10, 20, 30); }" ""
with pytest.raises(ValueError, match=r"Semantic error"):
stan.build(program_code, random_seed=1)


def test_syntax_error():
program_code = "parameters { real y; } model { y ~ uniform(10| 20); }" ""
with pytest.raises(ValueError, match=r"Syntax error"):
stan.build(program_code, random_seed=1)
6 changes: 3 additions & 3 deletions tests/test_build_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def test_build_basic():

def test_stanc_no_such_distribution():
program_code = "parameters {real z;} model {z ~ no_such_distribution();}"
with pytest.raises(RuntimeError, match=r"Semantic error in"):
with pytest.raises(ValueError, match=r"Semantic error"):
stan.build(program_code=program_code)


def test_stanc_invalid_assignment():
program_code = "parameters {real z;} model {z = 3;}"
with pytest.raises(RuntimeError, match=r"Semantic error in"):
with pytest.raises(ValueError, match=r"Semantic error"):
stan.build(program_code=program_code)


Expand All @@ -34,5 +34,5 @@ def test_stanc_exception_semicolon():
z ~ normal(0, 1);
y ~ normal(0, 1);}
"""
with pytest.raises(RuntimeError, match=r"Syntax error in"):
with pytest.raises(ValueError, match=r"Syntax error"):
stan.build(program_code=program_code)

0 comments on commit 4fdbfc8

Please sign in to comment.