Coverage for src/lilbee/server/anthropic_api/errors.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-14 11:46 +0000

1"""Anthropic error envelope over the shared provider-error classification.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any 

6 

7from lilbee.server.chat_completions_api.errors import CompletionsErrorCode 

8 

9_FALLBACK_ERROR_TYPE = "invalid_request_error" 

10 

11# The status/code classification is shared with the completions surface 

12# (classify_provider_error); only the envelope vocabulary differs. 

13_ANTHROPIC_ERROR_TYPES: dict[CompletionsErrorCode, str] = { 

14 CompletionsErrorCode.INVALID_REQUEST: "invalid_request_error", 

15 CompletionsErrorCode.MODEL_NOT_FOUND: "not_found_error", 

16 CompletionsErrorCode.MODEL_DOES_NOT_SUPPORT_TOOLS: "invalid_request_error", 

17 CompletionsErrorCode.CONTEXT_LENGTH_EXCEEDED: "invalid_request_error", 

18 CompletionsErrorCode.INVALID_API_KEY: "authentication_error", 

19 CompletionsErrorCode.RATE_LIMIT_EXCEEDED: "rate_limit_error", 

20 CompletionsErrorCode.INTERNAL_ERROR: "api_error", 

21} 

22 

23 

24def anthropic_error_type(code: CompletionsErrorCode) -> str: 

25 """The Anthropic ``error.type`` for a shared error code.""" 

26 # .get, not a subscript: a new enum member must degrade to a handled 4xx 

27 # envelope, not surface as a 500. 

28 return _ANTHROPIC_ERROR_TYPES.get(code, _FALLBACK_ERROR_TYPE) 

29 

30 

31def anthropic_error_body(error_type: str, message: str) -> dict[str, Any]: 

32 """Build the Anthropic JSON error envelope.""" 

33 return {"type": "error", "error": {"type": error_type, "message": message}}