Skip to content

Commit 1e3f066

Browse files
authored
Simplify error reporting during compile() (#620)
* Simplify error reporting during compile() * Add import to issue template * Update test
1 parent 1c761ff commit 1e3f066

File tree

3 files changed

+17
-47
lines changed

3 files changed

+17
-47
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ Describe the issue as clearly as possible.
1010
Provide any additional information here.
1111

1212
#### Current Version:
13-
Please include the output of `cmdstanpy.show_versions()`, or
13+
Please include the output of `import cmdstanpy; cmdstanpy.show_versions()`, or
1414
at least the cmdstan and cmdstanpy versions used.

cmdstanpy/model.py

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,6 @@ def __init__(
210210

211211
if compile and self._exe_file is None:
212212
self.compile(force=str(compile).lower() == 'force')
213-
if self._exe_file is None:
214-
raise ValueError(
215-
'Unable to compile Stan model file: {}.'.format(
216-
self._stan_file
217-
)
218-
)
219213

220214
def __repr__(self) -> str:
221215
repr = 'CmdStanModel: name={}'.format(self._name)
@@ -531,45 +525,26 @@ def compile(
531525
get_logger().info(
532526
'compiled model executable: %s', self._exe_file
533527
)
534-
if compilation_failed or 'Warning' in console:
528+
if 'Warning' in console:
535529
lines = console.split('\n')
536530
warnings = [x for x in lines if x.startswith('Warning')]
537-
syntax_errors = [
538-
x for x in lines if x.startswith('Syntax error')
539-
]
540-
semantic_errors = [
541-
x for x in lines if x.startswith('Semantic error')
542-
]
543-
exceptions = [
544-
x
545-
for x in lines
546-
if 'Uncaught exception' in x or 'fatal error' in x
547-
]
548-
if (
549-
len(syntax_errors) > 0
550-
or len(semantic_errors) > 0
551-
or len(exceptions) > 0
552-
):
553-
get_logger().error('Stan program failed to compile:')
554-
get_logger().warning(console)
555-
elif len(warnings) > 0:
556-
get_logger().warning(
557-
'Stan compiler has produced %d warnings:',
558-
len(warnings),
559-
)
560-
get_logger().warning(console)
561-
elif (
562-
'PCH file' in console
563-
or 'model_header.hpp.gch' in console
564-
or 'precompiled header' in console
565-
):
531+
get_logger().warning(
532+
'Stan compiler has produced %d warnings:',
533+
len(warnings),
534+
)
535+
get_logger().warning(console)
536+
if compilation_failed:
537+
if 'PCH' in console or 'precompiled header' in console:
566538
get_logger().warning(
567539
"CmdStan's precompiled header (PCH) files "
568540
"may need to be rebuilt."
569-
"If your model failed to compile please run "
570-
"cmdstanpy.rebuild_cmdstan().\nIf the "
571-
"issue persists please open a bug report"
541+
"Please run cmdstanpy.rebuild_cmdstan().\n"
542+
"If the issue persists please open a bug report"
572543
)
544+
raise ValueError(
545+
f"Failed to compile Stan model '{self._stan_file}'. "
546+
f"Console:\n{console}"
547+
)
573548

574549
def optimize(
575550
self,

test/test_model.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,8 @@ def test_model_file_does_not_exist(self):
299299

300300
def test_model_syntax_error(self):
301301
stan = os.path.join(DATAFILES_PATH, 'bad_syntax.stan')
302-
with LogCapture(level=logging.WARNING) as log:
303-
logging.getLogger()
304-
with self.assertRaises(ValueError):
305-
CmdStanModel(stan_file=stan)
306-
log.check_present(
307-
('cmdstanpy', 'WARNING', StringComparison(r'(?s).*Syntax error.*'))
308-
)
302+
with self.assertRaisesRegex(ValueError, r'.*Syntax error.*'):
303+
CmdStanModel(stan_file=stan)
309304

310305
def test_repr(self):
311306
model = CmdStanModel(stan_file=BERN_STAN)

0 commit comments

Comments
 (0)