From 2fadd3aacb5570bda8f978af91265dbc2d6b97cd Mon Sep 17 00:00:00 2001 From: b3nw Date: Tue, 11 Aug 2026 20:51:39 -0400 Subject: [PATCH] fix(#7): remove double 'API error:' prefix from error messages (#10) Client._request no longer prepends 'API error:' to the exception message since _format_error in server.py already adds that prefix. Closes #7 --- src/npm_mcp/client.py | 2 +- tests/test_client.py | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/npm_mcp/client.py b/src/npm_mcp/client.py index a90d13f..c914e48 100644 --- a/src/npm_mcp/client.py +++ b/src/npm_mcp/client.py @@ -171,7 +171,7 @@ class NpmClient: raise NpmNotFoundError(f"Resource not found: {endpoint}") if response.status_code >= 400: - raise NpmApiError(f"API error: {response.text}", status_code=response.status_code) + raise NpmApiError(response.text, status_code=response.status_code) return response diff --git a/tests/test_client.py b/tests/test_client.py index 508ed41..9609d00 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -4,7 +4,7 @@ import pytest from httpx import Response from npm_mcp.client import NpmClient -from npm_mcp.exceptions import NpmAuthenticationError, NpmConnectionError +from npm_mcp.exceptions import NpmApiError, NpmAuthenticationError, NpmConnectionError @pytest.fixture @@ -227,3 +227,29 @@ class TestNpmClientEndpoints: assert host.forward_port == 3000 assert host.ssl_forced is True assert host.certificate_id == 24 + + @pytest.mark.asyncio + async def test_api_error_no_double_prefix(self, httpx_mock, mock_token_response): + """Test that API errors don't have a duplicated 'API error:' prefix.""" + httpx_mock.add_response( + method="POST", + url="http://localhost:81/api/tokens", + json=mock_token_response, + ) + httpx_mock.add_response( + method="GET", + url="http://localhost:81/api/nginx/proxy-hosts?expand=owner%2Ccertificate", + json={"error": {"message": "Something went wrong"}}, + status_code=400, + ) + + async with NpmClient( + base_url="http://localhost:81/api", + identity="test@test.com", + secret="password", + ) as client: + with pytest.raises(NpmApiError) as exc_info: + await client.get_proxy_hosts() + + msg = str(exc_info.value) + assert msg.count("API error") == 0, f"Client should not prefix error: {msg}"