diff --git a/pyproject.toml b/pyproject.toml index cb58158..d7769dc 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "rushdb" -version = "3.4.0" +version = "3.4.1" description = "RushDB SDK for Python: the client for RushDB, the database for AI (RushDB Cloud or self-hosted)" authors = [ {name = "RushDB Team", email = "hi@rushdb.com"} diff --git a/src/rushdb/client.py b/src/rushdb/client.py index c0585b6..078de3c 100755 --- a/src/rushdb/client.py +++ b/src/rushdb/client.py @@ -21,7 +21,7 @@ from .api.relationships import RelationsAPI from .api.text_indexes import TextIndexesAPI from .api.transactions import TransactionsAPI -from .common import RushDBError +from .common import RushDBError, parse_error_body from .direct import DirectEngine from .direct import router as direct_router from .utils.token_prefix import extract_mixed_properties_from_token @@ -288,17 +288,16 @@ def _make_request( with urllib.request.urlopen(request) as response: return json.loads(response.read().decode("utf-8")) except urllib.error.HTTPError as e: - # Error bodies are usually JSON, but a gateway/proxy may return HTML or an - # empty body — without this guard the JSONDecodeError raised here would - # escape as-is instead of surfacing as a RushDBError. + # Error bodies are usually JSON, but a gateway/proxy may return HTML, + # plain text or an empty body; all of them surface as a RushDBError. try: - error_body = json.loads(e.read().decode("utf-8")) - except (json.JSONDecodeError, UnicodeDecodeError): - error_body = {} - message = error_body.get("message") - if isinstance(message, list): - message = "; ".join(str(item) for item in message) - raise RushDBError(message or f"HTTP {e.code}", error_body, status=e.code) + raw = e.read() + except OSError: + raw = b"" + message, error_body, code = parse_error_body(raw) + raise RushDBError( + message or f"HTTP {e.code}", error_body, status=e.code, code=code + ) except urllib.error.URLError as e: raise RushDBError(f"Connection error: {str(e)}") except json.JSONDecodeError as e: @@ -340,18 +339,15 @@ def _send_raw( except urllib.error.HTTPError as e: # Storage answers XML; the status says enough then. try: - error_body = json.loads(e.read().decode("utf-8")) - except (json.JSONDecodeError, UnicodeDecodeError, OSError): - error_body = {} - if not isinstance(error_body, dict): - error_body = {} - message = error_body.get("message") or error_body.get("error") - if isinstance(message, list): - message = "; ".join(str(item) for item in message) + raw = e.read() + except OSError: + raw = b"" + message, error_body, code = parse_error_body(raw) raise RushDBError( - str(message) if message else f"{what} (HTTP {e.code})", + message or f"{what} (HTTP {e.code})", error_body, status=e.code, + code=code, ) except (urllib.error.URLError, OSError) as e: raise RushDBError(f"{what}: {e}") diff --git a/src/rushdb/common.py b/src/rushdb/common.py index 8c0516f..73aff84 100755 --- a/src/rushdb/common.py +++ b/src/rushdb/common.py @@ -1,4 +1,59 @@ -from typing import Dict, Optional +import json +import re +from typing import Any, Dict, Optional, Tuple + +# A bare ``{"error": "unauthorized"}`` names a code; ``"route not found"`` does not. +_CODE_PATTERN = re.compile(r"[a-z][a-z0-9_]*") + + +def parse_error_body(raw: Any) -> Tuple[Optional[str], Dict[str, Any], Optional[str]]: + """Read an HTTP error body into ``(message, details, code)``. + + Accepts every shape the engine and the API answer with: + + - the protocol envelope ``{"protocolVersion": 1, "error": {"code", "message", + "retryable"}}``; + - a bare string ``{"error": "unauthorized"}`` (engine auth failures); + - flat ``{"code", "message", "retryable"}`` (engine boot) and the API's + ``{"statusCode", "message", "error"}``, where ``message`` may be a list; + - plain text, HTML from a proxy, or an empty body. + + ``message`` is ``None`` when the body has nothing worth showing; callers + fall back to a message built from the HTTP status. ``details`` is the + parsed JSON object (``{}`` otherwise). + """ + if isinstance(raw, bytes): + raw = raw.decode("utf-8", errors="replace") + text = (raw or "").strip() + if not text: + return None, {}, None + try: + parsed = json.loads(text) + except json.JSONDecodeError: + # Plain text is the message; an HTML error page from a proxy is not. + if text.startswith("<"): + return None, {}, None + return text[:500], {}, None + if not isinstance(parsed, dict): + return None, {}, None + + message: Any = parsed.get("message") + code: Any = parsed.get("code") + error = parsed.get("error") + if isinstance(error, dict): + message = error.get("message") or message + code = error.get("code") or code + elif isinstance(error, str) and error and not message: + message = error + if not code and _CODE_PATTERN.fullmatch(error): + code = error + if isinstance(message, list): + message = "; ".join(str(item) for item in message) + return ( + str(message) if message else None, + parsed, + code if isinstance(code, str) and code else None, + ) class RushDBError(Exception): diff --git a/src/rushdb/direct/engine.py b/src/rushdb/direct/engine.py index e955ffc..189fd92 100755 --- a/src/rushdb/direct/engine.py +++ b/src/rushdb/direct/engine.py @@ -12,7 +12,7 @@ import urllib.request from typing import Any, Dict, Optional -from ..common import RushDBError +from ..common import RushDBError, parse_error_body class DirectEngine: @@ -65,15 +65,20 @@ def execute(self, operation: Dict[str, Any]) -> Dict[str, Any]: with urllib.request.urlopen(req, timeout=120) as resp: # noqa: S310 body = json.loads(resp.read().decode("utf-8") or "{}") except urllib.error.HTTPError as exc: - raw = exc.read().decode("utf-8", errors="replace") try: - parsed = json.loads(raw) - message = parsed.get("error", {}).get("message", raw) - except json.JSONDecodeError: - message = raw or f"engine request failed ({exc.code})" - raise RushDBError(message, status=exc.code) from exc + raw = exc.read() + except OSError: + raw = b"" + message, details, code = parse_error_body(raw) + raise RushDBError( + message or f"engine request failed (HTTP {exc.code})", + details, + status=exc.code, + code=code, + ) from exc except urllib.error.URLError as exc: raise RushDBError(f"Connection error: {exc}") from exc if body.get("error"): - raise RushDBError(body["error"].get("message", "engine request failed")) + message, details, code = parse_error_body(json.dumps(body)) + raise RushDBError(message or "engine request failed", details, code=code) return body.get("response", {}) diff --git a/tests/test_direct_mode_live.py b/tests/test_direct_mode_live.py index 5c3e3cc..10566d8 100644 --- a/tests/test_direct_mode_live.py +++ b/tests/test_direct_mode_live.py @@ -5,15 +5,22 @@ rushdb serve --data-dir /tmp/rushdb-test --port 3877 --languages cypher RUSHDB_TEST_ENGINE_URL=http://127.0.0.1:3877 pytest tests/test_direct_mode_live.py + +The read-only capability test also needs the engine's +``--capability-secret`` in ``RUSHDB_TEST_ENGINE_CAPABILITY_SECRET``. """ +import base64 +import hashlib +import hmac +import json import os import time import uuid import pytest -from rushdb import CloudOnlyError, DirectModeError, RushDB +from rushdb import CloudOnlyError, DirectModeError, RushDB, RushDBError ENGINE_URL = os.environ.get("RUSHDB_TEST_ENGINE_URL") pytestmark = pytest.mark.skipif( @@ -213,3 +220,39 @@ def test_cloud_features_are_refused(db): db.relationships.patterns.list() with pytest.raises(CloudOnlyError): db.settings.get() + + +def test_bad_token_raises_rushdb_error(): + db = RushDB("not-the-token", direct=True, engine_url=ENGINE_URL) + with pytest.raises(RushDBError) as failure: + db.records.create(BOOK, {"title": "Refused"}) + assert failure.value.status == 401 + assert failure.value.code == "unauthorized" + + +def capability(secret, scope): + def encode(data): + return base64.urlsafe_b64encode(data).rstrip(b"=").decode() + + now = int(time.time()) + claims = encode( + json.dumps( + {"sub": "sdk-test", "scope": scope, "iat": now, "exp": now + 600} + ).encode() + ) + signature = hmac.new(bytes.fromhex(secret), claims.encode(), hashlib.sha256) + return f"{claims}.{encode(signature.digest())}" + + +@pytest.mark.skipif( + not os.environ.get("RUSHDB_TEST_ENGINE_CAPABILITY_SECRET"), + reason="RUSHDB_TEST_ENGINE_CAPABILITY_SECRET is not set", +) +def test_write_with_read_only_capability_raises_rushdb_error(db): + token = capability(os.environ["RUSHDB_TEST_ENGINE_CAPABILITY_SECRET"], "read") + reader = RushDB(token, direct=True, engine_url=ENGINE_URL) + assert reader.records.find({"labels": [BOOK]}).total >= 3 + with pytest.raises(RushDBError) as failure: + reader.records.create(BOOK, {"title": "Refused"}) + assert failure.value.status == 401 + assert failure.value.code == "unauthorized" diff --git a/tests/test_error_bodies.py b/tests/test_error_bodies.py new file mode 100644 index 0000000..c76b0ce --- /dev/null +++ b/tests/test_error_bodies.py @@ -0,0 +1,127 @@ +"""Every error body the engine and the API answer with surfaces as a +RushDBError carrying the message, the code where the body names one, and the +HTTP status.""" + +import io +import json +import urllib.error +from unittest import mock + +import pytest + +from rushdb import RushDB, RushDBError +from rushdb.common import parse_error_body + +ENVELOPE = { + "protocolVersion": 1, + "error": { + "code": "unauthorized", + "message": "capability lacks the write permission", + "retryable": False, + }, +} + +# (body, expected message or None for the status fallback, expected code) +SHAPES = { + "envelope": ( + json.dumps(ENVELOPE), + "capability lacks the write permission", + "unauthorized", + ), + "bare string error": ('{"error":"unauthorized"}', "unauthorized", "unauthorized"), + "bare string that is no code": ( + '{"error":"route not found"}', + "route not found", + None, + ), + "flat boot error": ( + '{"code":"unavailable","message":"node is starting (replay)","retryable":true}', + "node is starting (replay)", + "unavailable", + ), + "api error": ( + '{"statusCode":401,"message":"Invalid token","error":"Unauthorized"}', + "Invalid token", + None, + ), + "api validation list": ( + '{"statusCode":400,"message":["a is required","b must be a string"]}', + "a is required; b must be a string", + None, + ), + "plain text": ("Unauthorized\n", "Unauthorized", None), + "html from a proxy": ("502 Bad Gateway", None, None), + "json that is no object": ('["unauthorized"]', None, None), + "empty": ("", None, None), +} + + +@pytest.mark.parametrize("body,message,code", SHAPES.values(), ids=SHAPES.keys()) +def test_parse_error_body(body, message, code): + parsed_message, details, parsed_code = parse_error_body(body.encode("utf-8")) + assert parsed_message == message + assert parsed_code == code + assert isinstance(details, dict) + + +def refusing(status, body): + def refuse(request, timeout=None): + raise urllib.error.HTTPError( + request.full_url, status, "error", {}, io.BytesIO(body.encode("utf-8")) + ) + + return refuse + + +@pytest.mark.parametrize("body,message,code", SHAPES.values(), ids=SHAPES.keys()) +def test_direct_mode_raises_rushdb_error(body, message, code): + db = RushDB("BAD", direct=True, engine_url="http://engine:3000") + with mock.patch("urllib.request.urlopen", side_effect=refusing(401, body)): + with pytest.raises(RushDBError) as failure: + db.records.create("Book", {"title": "Dune"}) + assert str(failure.value) == (message or "engine request failed (HTTP 401)") + assert failure.value.code == code + assert failure.value.status == 401 + + +@pytest.mark.parametrize("body,message,code", SHAPES.values(), ids=SHAPES.keys()) +def test_cloud_client_raises_rushdb_error(body, message, code): + db = RushDB("BAD", base_url="http://cloud:3000") + with mock.patch("urllib.request.urlopen", side_effect=refusing(401, body)): + with pytest.raises(RushDBError) as failure: + db.records.find({"labels": ["Book"]}) + assert str(failure.value) == (message or "HTTP 401") + assert failure.value.code == code + assert failure.value.status == 401 + + +@pytest.mark.parametrize("body,message,code", SHAPES.values(), ids=SHAPES.keys()) +def test_raw_upload_raises_rushdb_error(body, message, code): + db = RushDB("BAD", base_url="http://cloud:3000") + with mock.patch("urllib.request.urlopen", side_effect=refusing(403, body)): + with pytest.raises(RushDBError) as failure: + db._send_raw("PUT", "http://storage/part", b"x", what="upload failed") + assert str(failure.value) == (message or "upload failed (HTTP 403)") + assert failure.value.code == code + assert failure.value.status == 403 + + +@pytest.mark.parametrize( + "error", [ENVELOPE["error"], "unauthorized"], ids=["object", "string"] +) +def test_direct_mode_error_in_a_200_body(error): + class Response(io.BytesIO): + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + def answer(request, timeout=None): + return Response(json.dumps({"protocolVersion": 1, "error": error}).encode()) + + db = RushDB("BAD", direct=True, engine_url="http://engine:3000") + with mock.patch("urllib.request.urlopen", side_effect=answer): + with pytest.raises(RushDBError) as failure: + db.records.create("Book", {"title": "Dune"}) + assert failure.value.code == "unauthorized" diff --git a/uv.lock b/uv.lock index bc19fe6..0e22752 100755 --- a/uv.lock +++ b/uv.lock @@ -710,7 +710,7 @@ wheels = [ [[package]] name = "rushdb" -version = "3.4.0" +version = "3.4.1" source = { editable = "." } [package.dev-dependencies]