Skip to content

Project.save_as_json silently fails when the file exists; broad excepts swallow real errors #374

@rozyczko

Description

@rozyczko

Summary

src/easyreflectometry/project.py:822-833:

if self.path_json.exists() and overwrite:
    ...unlink...
try:
    ...
    with open(self.path_json, mode='x') as file:   # 'x' = exclusive create
        ...
except Exception as exception:
    print(exception)

With overwrite=False and an existing file, the FileExistsError from mode 'x' is swallowed by the blanket except and the method returns as if it succeeded. The same blanket except turns disk-full, permission, and serialization errors into a stdout line. tests/test_project.py::test_save_as_json_dont_overwrite only checks the mtime is unchanged, so it passes while masking that no error is surfaced.

Related broad excepts in project.py

  • :568-572 count_datasets_in_file: any corrupt file silently reports 1 dataset.
  • :592-596 load_all_experiments_from_file: masks the ORSO error, then re-attempts a different loader, obscuring the original cause.
  • Error-channel inconsistency across the codebase: mixes print('ERROR: ...'), warnings.warn, logger, and raises; e.g. remove_material (:797-802) prints and silently does nothing.

Suggested fix

Use 'w' after the explicit unlink (or handle FileExistsError specifically and raise), let unexpected exceptions propagate, and standardize on exceptions + logging instead of print.

Found during deep code review (DEEP_ANALYSIS.md §5.5, §6).

Metadata

Metadata

Assignees

No one assigned

    Labels

    [priority] mediumNormal/default priority[scope] bugBug report or fix (major.minor.PATCH)
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions