Skip to content

Catch specific exceptions instead of using bare except - #69

Merged
namurphy merged 3 commits into
PlasmaPy:mainfrom
namurphy:specific-exceptions
Oct 7, 2026
Merged

namurphy merged 3 commits into
PlasmaPy:mainfrom
namurphy:specific-exceptions

Conversation

@namurphy

@namurphy namurphy commented Oct 7, 2026

Copy link
Copy Markdown
Member

Replaces every bare except: in src/pyfaradaycup/decommutator/ with the exceptions that can be expected at that point. The resulting errors now have messages that say what failed, and they chain the original exception.

Note

This branch is based on #67, so it includes that commit until #67 is merged. The change for this PR is the last commit.

Bug fix

main had except "CDFError":, which catches a string. In Python 3, that line raises TypeError whenever any exception reaches it. A CDF that could not be created therefore crashed the run with a confusing error instead of being logged and skipped. It now catches spacepy.pycdf.CDFError.

ccsds_reader_pipeline.py

  • No packets found: the try/except around pkt_inds[:, 0] (3 places) is replaced with an explicit check for an empty array.
  • Choosing the SC_HK file in read_file_sc:
    • Catches ValueError and KeyError from the packet header, and includes the reason in the printed message.
    • Then catches StopIteration and KeyError when falling back to the date in the path.
  • _parse_pkt:
    • Catches ValueError for packets that are too short.
    • Raises RuntimeError naming the mnemonic and APID that could not be decoded. These raise statements previously had no message.
  • _get_layout and _get_layout_sc:
    • Catch OSError when reading the .blk file.
    • Catch ValueError when parsing it, and raise RuntimeError with the file name, line number, and line.
    • _get_layout no longer tries to open the same file twice.

swp_spc_l02l1.py

  • main:
    • Catches Exception when filling a CDF and logs the exception with the APID. It still moves on to the next APID.
    • An unreadable L0 file now raises RuntimeError with the file name.
  • _cdf35e_35f and _cdf351_353_354:
    • A variable in the CDF skeleton that is missing from the L0 data now gives its own message.
    • Other failures when writing a variable (CDFError, TypeError, ValueError) raise RuntimeError with the variable name.
  • _cdf352:
    • Corrupt packets (ValueError) and variables that cannot be written are still logged and skipped, now with the reason.
    • The outer catch-all is except Exception with a generic message.
  • _get_newest_kernel: when no kernel files are found, the error now names the file pattern and directory.

Documentation

  • Adds Raises sections to:
    • main
    • _get_layout and _get_layout_sc
    • _cdf35e_35f, _cdf351_353_354, and _cdf352
  • Notes in main that errors for a single APID are logged and skipped, and that a partly filled CDF may be left behind.
  • Removes the "Remove the bare except: blocks" and "Raise exceptions with messages" items from the decommutator README.md.

Testing

  • pre-commit run --all-files passes.
  • nox -s tests-3.9 and nox -s tests-3.14 pass.
  • nox -s docs builds with --nitpicky --fail-on-warning.
  • The existing test only covers successful conversion. I checked the _get_layout_sc error paths for a missing file and a malformed line, plus _wrapper_status on a file with no packets, by calling them directly.

🤖 Generated with Claude Code

namurphy and others added 3 commits October 6, 2026 19:54
Remove unused verbose, ptp, and ccsds_head parameters from private
helper functions, along with unreachable code after raise statements,
unused variables, and commented-out code. Keep verbose on _cdf35e_35f
so that main can call each CDF writer with the same arguments, and
include the failing key in its RuntimeError message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace every bare except in the decommutator with the exceptions that
can be expected at that point, and give the resulting errors messages
that say what failed, chaining the original exception. Where any error
should be reported, catch Exception with a generic message.

Fix main catching the string "CDFError", which raised TypeError instead
of catching spacepy.pycdf.CDFError. Document the important exceptions
in docstrings, and remove the two items in the decommutator README that
this addresses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	src/pyfaradaycup/decommutator/ccsds_reader_pipeline.py
#	src/pyfaradaycup/decommutator/swp_spc_l02l1.py
@namurphy
namurphy merged commit 21fccb9 into PlasmaPy:main Oct 7, 2026
15 checks passed
@namurphy
namurphy deleted the specific-exceptions branch October 7, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant