diff --git a/src/workbench_cli/utilities/error_handling.py b/src/workbench_cli/utilities/error_handling.py index c6e1ee9..1a6cca8 100644 --- a/src/workbench_cli/utilities/error_handling.py +++ b/src/workbench_cli/utilities/error_handling.py @@ -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', '')}") 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', '')}") + 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', '')}") + return # Exit early to avoid showing generic API error details + print(f"\nāŒ Workbench API error") print(f" {error_message}") diff --git a/tests/unit/utilities/test_error_handling.py b/tests/unit/utilities/test_error_handling.py index 44c9fa5..c9c560d 100644 --- a/tests/unit/utilities/test_error_handling.py +++ b/tests/unit/utilities/test_error_handling.py @@ -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 \ No newline at end of file + 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) \ No newline at end of file