Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/workbench_cli/utilities/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ def format_and_print_error(error: Exception, handler_name: str, params: argparse
print(f" • The API URL is correct: {getattr(params, 'api_url', '<not specified>')}")

elif isinstance(error, ApiError):
# Check for credential errors first
if "user_not_found_or_api_key_is_not_correct" in error_message:
print(f"\n❌ Invalid credentials")
print(f" The username or API token provided is incorrect.")
print(f"\n💡 Please check:")
print(f" • Your username: {getattr(params, 'api_user', '<not specified>')}")
print(f" • Your API token is correct and not expired")
print(f" • Your account has access to the Workbench instance")
print(f" • The API URL is correct: {getattr(params, 'api_url', '<not specified>')}")
return # Exit early to avoid showing generic API error details

print(f"\n❌ Workbench API error")
print(f" {error_message}")

Expand Down
28 changes: 27 additions & 1 deletion tests/unit/utilities/test_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,30 @@ def handler_with_args(workbench, params):
params.test_attr = "test_value"

result = handler_with_args(workbench, params)
assert result is True
assert result is True

@patch('builtins.print')
def test_format_and_print_error_credential_error(mock_print, mock_params):
"""Test that credential errors are formatted with user-friendly messages."""
# Set up mock params with credential info
mock_params.api_user = 'testuser'
mock_params.api_url = 'https://example.com/api.php'

# Create an API error with the credential error message
error = ApiError("Classes.FossID.user_not_found_or_api_key_is_not_correct")

# Call the error formatting function
format_and_print_error(error, "test_handler", mock_params)

# Check that print was called with credential-specific messages
print_calls = [call.args[0] for call in mock_print.call_args_list]

# Verify the credential-specific message is shown
assert any("❌ Invalid credentials" in call for call in print_calls)
assert any("The username or API token provided is incorrect" in call for call in print_calls)
assert any("testuser" in call for call in print_calls) # Should show the username
assert any("https://example.com/api.php" in call for call in print_calls) # Should show the API URL

# Verify generic API error message is NOT shown
assert not any("❌ Workbench API error" in call for call in print_calls)
assert not any("Classes.FossID.user_not_found_or_api_key_is_not_correct" in call for call in print_calls)
Loading