Skip to content

Commit baad6ce

Browse files
authored
Merge pull request #284 from stan-dev/update_exception_handling
raise exception from parent exception
2 parents 5a8597f + 6d09897 commit baad6ce

4 files changed

Lines changed: 13 additions & 13 deletions

File tree

cmdstanpy/cmdstan_args.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,12 @@ def validate(self) -> None:
591591
self._logger.info(
592592
'created output directory: %s', self.output_dir
593593
)
594-
except (RuntimeError, PermissionError):
594+
except (RuntimeError, PermissionError) as exc:
595595
raise ValueError(
596596
'invalid path for output files, no such dir: {}'.format(
597597
self.output_dir
598598
)
599-
)
599+
) from exc
600600
if not os.path.isdir(self.output_dir):
601601
raise ValueError(
602602
'specified output_dir not a directory: {}'.format(
@@ -608,11 +608,11 @@ def validate(self) -> None:
608608
with open(testpath, 'w+'):
609609
pass
610610
os.remove(testpath) # cleanup
611-
except Exception:
611+
except Exception as exc:
612612
raise ValueError(
613613
'invalid path for output files,'
614614
' cannot write to dir: {}'.format(self.output_dir)
615-
)
615+
) from exc
616616

617617
if self.seed is None:
618618
rng = RandomState()

cmdstanpy/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,13 @@ def generate_quantities(
850850
runset._csv_files = sample_csv_files
851851
sample_fit = CmdStanMCMC(runset)
852852
sample_drawset = sample_fit.draws_as_dataframe()
853-
except ValueError as e:
853+
except ValueError as exc:
854854
raise ValueError(
855855
'Invalid mcmc_sample, error:\n\t{}\n\t'
856856
' while processing files\n\t{}'.format(
857-
repr(e), '\n\t'.join(sample_csv_files)
857+
repr(exc), '\n\t'.join(sample_csv_files)
858858
)
859-
)
859+
) from exc
860860

861861
generate_quantities_args = GenerateQuantitiesArgs(
862862
csv_files=sample_csv_files

cmdstanpy/stanfit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ def save_csvfiles(self, dir: str = None) -> None:
261261
with open(test_path, 'w'):
262262
pass
263263
os.remove(test_path) # cleanup
264-
except (IOError, OSError, PermissionError):
265-
raise Exception('cannot save to path: {}'.format(dir))
264+
except (IOError, OSError, PermissionError) as exc:
265+
raise Exception('cannot save to path: {}'.format(dir)) from exc
266266

267267
for i in range(self.chains):
268268
if not os.path.exists(self._csv_files[i]):

cmdstanpy/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ def parse_rdump_value(rhs: str) -> Union[int, float, np.array]:
444444
val = float(rhs)
445445
else:
446446
val = int(rhs)
447-
except TypeError:
448-
raise ValueError('bad value in Rdump file: {}'.format(rhs))
447+
except TypeError as exc:
448+
raise ValueError('bad value in Rdump file: {}'.format(rhs)) from exc
449449
return val
450450

451451

@@ -691,10 +691,10 @@ def scan_metric(fd: TextIO, config_dict: Dict, lineno: int) -> int:
691691
)
692692
try:
693693
float(stepsize.strip())
694-
except ValueError:
694+
except ValueError as exc:
695695
raise ValueError(
696696
'line {}: invalid stepsize: {}'.format(lineno, stepsize)
697-
)
697+
) from exc
698698
line = fd.readline().strip()
699699
lineno += 1
700700
if not (

0 commit comments

Comments
 (0)