Skip to content

osc.py: Preserve SystemExit context when re-raising ToolError in run() (B904) #14

@coderabbitai

Description

@coderabbitai

Summary

In src/rhos_ls_mcps/osc.py, the run() method of MyOpenStackShell catches SystemExit as e and raises a ToolError without chaining the original exception, which drops the SystemExit as the cause:

except SystemExit as e:
    raise ToolError(
        f"OpenStack failed {e.code}: {self.stdout.getvalue() or self.stderr.getvalue()}"
    )

This should be changed to:

except SystemExit as e:
    raise ToolError(
        f"OpenStack failed {e.code}: {self.stdout.getvalue() or self.stderr.getvalue()}"
    ) from e

Why

Without from e, the original SystemExit is silently discarded as the exception cause. Using raise ... from e preserves the full exception chain, which aids debugging. This is also flagged by Ruff rule B904 (Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling).

Context

Identified during review of PR #11 (Enable ruff and pre-commit). Deferred to a follow-up PR to keep #11 focused on tooling/formatting changes.

Related comment: #11 (comment)

Requested by @lpiwowar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions