Skip to content

Report invalid B2 URI arguments without a traceback - #1224

Open
Eliot5566 wants to merge 1 commit into
Backblaze:masterfrom
Eliot5566:fix/1216-b2-uri-argument-error
Open

Eliot5566 wants to merge 1 commit into
Backblaze:masterfrom
Eliot5566:fix/1216-b2-uri-argument-error

Conversation

@Eliot5566

Copy link
Copy Markdown

Fixes #1216

get_b2_uri_from_arg (used by commands such as file hide with the deprecated bucketName fileName form) raises argparse.ArgumentError after parsing when the B2 URI is invalid or is given together with a file name. run_command didn't handle it, so it fell into the generic except Exception: raise and the user got a traceback.

run_command now catches argparse.ArgumentError, prints it as ERROR: argument B2_URI: ... and returns 2, the same exit code argparse uses for its own argument errors (as in test_file_hide__cannot_hide_by_b2id).

$ b2 file hide notauri
ERROR: argument B2_URI: Incorrect B2 URI was provided, expected `b2://bucketName/fileName`, but got 'notauri'

Tests:

  • New test_file_hide__invalid_b2_uri_argument covers both messages. It fails on master and passes with the fix, for --cli b2v3, b2v4 and b2v5.
  • test/unit for b2v3, b2v4 and b2v5: the only failures are the same 7 autocomplete tests that fail on master in my environment.
  • ruff check and ruff format --check (0.8.6, as in uv.lock) are clean; changelog fragment changelog.d/1216.fixed.md added.

I used Claude to help write and test this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3

get_b2_uri_from_arg raises argparse.ArgumentError for an invalid B2 URI
or a B2 URI given together with a file name, but run_command re-raised
it, so `b2 file hide notauri` ended in a traceback. Print it as an
error and exit with 2, like other argument errors.

Fixes Backblaze#1216

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

ConsoleTool.run_command now handles command-time argparse.ArgumentError by logging the error, printing its message to stderr, and returning status 2. Parameterized tests cover two invalid file-hide argument cases and verify that the file remains listed. The changelog describes the error and exit behavior.

Severity of issue fixed: Low

Merge Risk: Low · up to 67f59

Invalid B2 URI arguments now produce a short error and exit code 2 instead of a traceback. With --verbose, a traceback may still appear, consistent with other error handlers. This is low risk and mergeable, with owner awareness of the verbose-mode behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: invalid B2 URI arguments are now reported as a short error instead of a traceback. The raw summary confirms that run_command now catches argparse.ArgumentError, print…
Description check Passed The description explains the cause: get_b2_uri_from_arg raises argparse.ArgumentError for an invalid B2 URI or a URI given with a file name, and run_command did not handle it, so users saw a traceback…
Linked Issues check Passed Issue #1216 asks that invalid B2 URI arguments, or a B2 URI given together with a file name, produce a short error instead of a traceback. The whole-PR diff (merge base to head) adds an `except argpar…
Out of Scope Changes check Passed The whole-PR changes are limited to three areas. (1) b2/_internal/console_tool.py: the new except argparse.ArgumentError branch in run_command. This is the direct fix for #1216. (2) test/unit/cons…
Full details: Title check

Explanation

The title states the main change: invalid B2 URI arguments are now reported as a short error instead of a traceback. The raw summary confirms that run_command now catches argparse.ArgumentError, prints an ERROR message, and returns exit status 2, which matches the title.

Full details: Description check

Explanation

The description explains the cause: get_b2_uri_from_arg raises argparse.ArgumentError for an invalid B2 URI or a URI given with a file name, and run_command did not handle it, so users saw a traceback. It states the fix (catching the error, printing an ERROR line, returning 2), gives an example, and lists the tests added. It relates directly to the changeset.

Full details: Linked Issues check

Explanation

Issue #1216 asks that invalid B2 URI arguments, or a B2 URI given together with a file name, produce a short error instead of a traceback. The whole-PR diff (merge base to head) adds an except argparse.ArgumentError branch to ConsoleTool.run_command in b2/_internal/console_tool.py. The branch prints ERROR: {e} to stderr and returns 2. The exception text comes from get_b2_uri_from_arg, so the output has the form ERROR: argument B2_URI: .... The new test test_file_hide__invalid_b2_uri_argument in test/unit/console_tool/test_file_hide.py checks both cases: a URI with a file name, and the invalid string notauri. It expects the matching stderr text and status 2. The issue example commands are covered by this path. The issue does not fix an exit code; the PR uses 2, which matches argparse argument errors. The PR author reports the test results in the PR description. I did not run the tests. The requirement is met by the code and test at the reviewed head.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes are limited to three areas. (1) b2/_internal/console_tool.py: the new except argparse.ArgumentError branch in run_command. This is the direct fix for #1216. (2) test/unit/console_tool/test_file_hide.py: a parameterized test for the two invalid URI cases in #1216. (3) changelog.d/1216.fixed.md: a changelog fragment for the fix. All three changes connect directly to #1216. No unrelated code changes appear in the reviewed diff for console_tool.py or test_file_hide.py. The changelog file was not inspected directly; its content comes from the change summary only.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments: 1

b2/_internal/console_tool.py-5593-5593: 1

5593-5593: Minor · Functional Correctness · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Do not log a traceback for an argument error.

When the user supplies --verbose with an invalid URI, the stderr logging handler prints the traceback from logger.exception. The command therefore still shows a traceback despite returning status 2. Log the error without exception information, and cover the verbose case in the test.

As per the PR objectives, invalid URI arguments should produce a short error instead of a traceback.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4efcad87-e840-4885-a2a2-673bee930b55
Commits

Reviewing files that changed from the base of the PR and between 9194ec5 and 67f590b.

Files selected for processing: 3

  • b2/_internal/console_tool.py
  • changelog.d/1216.fixed.md
  • test/unit/console_tool/test_file_hide.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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.

Bad B2 URI arguments (for example "file hide notauri") end in a traceback

1 participant