Skip to content

Commit 9883e80

Browse files
author
matdev83
committed
Manual Merge PR #647
1 parent def9b62 commit 9883e80

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

src/connectors/openrouter.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from src.core.common.exceptions import (
1111
AuthenticationError,
1212
BackendError,
13+
ConfigurationError,
1314
ServiceUnavailableError,
1415
)
1516
from src.core.config.app_config import AppConfig
@@ -92,9 +93,24 @@ def _try_provider_call(*args: Any) -> dict[str, str] | None:
9293
try:
9394
result = provider(*args)
9495
except (AttributeError, TypeError) as exc:
96+
logger.error(
97+
"OpenRouter headers provider call failed with attribute/type error",
98+
exc_info=True,
99+
)
100+
errors.append(exc)
101+
return None
102+
except (ValueError, KeyError, IndexError) as exc:
103+
logger.error(
104+
"OpenRouter headers provider call failed with data error",
105+
exc_info=True,
106+
)
95107
errors.append(exc)
96108
return None
97109
except Exception as exc:
110+
logger.error(
111+
"OpenRouter headers provider call failed with unexpected error",
112+
exc_info=True,
113+
)
98114
errors.append(exc)
99115
return None
100116

@@ -150,12 +166,27 @@ def get_headers(self, identity: IAppIdentityConfig | None = None) -> dict[str, s
150166
if identity is not None:
151167
try:
152168
identity_headers = identity.get_resolved_headers(None)
153-
except Exception:
154-
identity_headers = {}
155-
else:
156169
identity_headers = dict(identity_headers)
157-
if identity_headers:
158-
headers.update(identity_headers)
170+
if identity_headers:
171+
headers.update(identity_headers)
172+
except (AttributeError, TypeError, ValueError) as exc:
173+
logger.error(
174+
"Failed to resolve identity headers in get_headers()",
175+
exc_info=True,
176+
)
177+
raise ConfigurationError(
178+
message="Failed to resolve identity configuration",
179+
details={"identity_error": str(exc)},
180+
) from exc
181+
except Exception as exc:
182+
logger.error(
183+
"Unexpected error resolving identity headers in get_headers()",
184+
exc_info=True,
185+
)
186+
raise ConfigurationError(
187+
message="Unexpected error resolving identity configuration",
188+
details={"unexpected_error": str(exc)},
189+
) from exc
159190
logger.info(
160191
f"OpenRouter headers: Authorization: Bearer {self.api_key[:20]}..., HTTP-Referer: {headers.get('HTTP-Referer', 'NOT_SET')}, X-Title: {headers.get('X-Title', 'NOT_SET')}"
161192
)
@@ -259,6 +290,16 @@ async def chat_completions( # type: ignore[override]
259290
headers_override = dict(self._resolve_headers_from_provider())
260291
except AuthenticationError:
261292
headers_override = None
293+
except Exception as exc:
294+
logger.error(
295+
"Unexpected error resolving headers from provider in chat_completions()",
296+
exc_info=True,
297+
)
298+
raise BackendError(
299+
message="Failed to resolve headers from provider",
300+
backend_name="openrouter",
301+
details={"provider_error": str(exc)},
302+
) from exc
262303

263304
if headers_override is None:
264305
headers_override = {}
@@ -269,10 +310,26 @@ async def chat_completions( # type: ignore[override]
269310
if identity is not None:
270311
try:
271312
identity_headers = identity.get_resolved_headers(None)
272-
except Exception:
273-
identity_headers = {}
274-
if identity_headers:
275-
headers_override.update(identity_headers)
313+
if identity_headers:
314+
headers_override.update(identity_headers)
315+
except (AttributeError, TypeError, ValueError) as exc:
316+
logger.error(
317+
"Failed to resolve identity headers in chat_completions()",
318+
exc_info=True,
319+
)
320+
raise ConfigurationError(
321+
message="Failed to resolve identity configuration",
322+
details={"identity_error": str(exc)},
323+
) from exc
324+
except Exception as exc:
325+
logger.error(
326+
"Unexpected error resolving identity headers in chat_completions()",
327+
exc_info=True,
328+
)
329+
raise ConfigurationError(
330+
message="Unexpected error resolving identity configuration",
331+
details={"unexpected_error": str(exc)},
332+
) from exc
276333

277334
if not headers_override:
278335
headers_override = None

0 commit comments

Comments
 (0)