Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 52a0f10

Browse files
Return more explicit status code errors on provider API (#962)
* Return more explicit status code errors on provider API Be more explicit handling exceptions on API * fix linting
1 parent 6f48528 commit 52a0f10

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/codegate/api/v1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ async def add_provider_endpoint(
123123
status_code=400,
124124
detail=str(e),
125125
)
126+
except provendcrud.ProviderModelsNotFoundError:
127+
raise HTTPException(status_code=401, detail="Provider models could not be found")
128+
except provendcrud.ProviderInvalidAuthConfigError:
129+
raise HTTPException(status_code=400, detail="Invalid auth configuration")
126130
except ValidationError as e:
127131
# TODO: This should be more specific
128132
raise HTTPException(
@@ -151,6 +155,10 @@ async def configure_auth_material(
151155
await pcrud.configure_auth_material(provider_id, request)
152156
except provendcrud.ProviderNotFoundError:
153157
raise HTTPException(status_code=404, detail="Provider endpoint not found")
158+
except provendcrud.ProviderModelsNotFoundError:
159+
raise HTTPException(status_code=401, detail="Provider models could not be found")
160+
except provendcrud.ProviderInvalidAuthConfigError:
161+
raise HTTPException(status_code=400, detail="Invalid auth configuration")
154162
except Exception:
155163
raise HTTPException(status_code=500, detail="Internal server error")
156164

src/codegate/api/v1_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class ProviderEndpoint(pydantic.BaseModel):
238238
description: str = ""
239239
provider_type: db_models.ProviderType
240240
endpoint: str = "" # Some providers have defaults we can leverage
241-
auth_type: Optional[ProviderAuthType] = ProviderAuthType.none
241+
auth_type: ProviderAuthType = ProviderAuthType.none
242242

243243
@staticmethod
244244
def from_db_model(db_model: db_models.ProviderEndpoint) -> "ProviderEndpoint":
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
from .crud import ProviderCrud, ProviderNotFoundError, initialize_provider_endpoints
1+
from .crud import (
2+
ProviderCrud,
3+
ProviderInvalidAuthConfigError,
4+
ProviderModelsNotFoundError,
5+
ProviderNotFoundError,
6+
initialize_provider_endpoints,
7+
)
28

3-
__all__ = ["ProviderCrud", "initialize_provider_endpoints", "ProviderNotFoundError"]
9+
__all__ = [
10+
"ProviderCrud",
11+
"initialize_provider_endpoints",
12+
"ProviderNotFoundError",
13+
"ProviderModelsNotFoundError",
14+
"ProviderInvalidAuthConfigError",
15+
]

src/codegate/providers/crud/crud.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class ProviderNotFoundError(Exception):
2121
pass
2222

2323

24+
class ProviderModelsNotFoundError(Exception):
25+
pass
26+
27+
28+
class ProviderInvalidAuthConfigError(Exception):
29+
pass
30+
31+
2432
class ProviderCrud:
2533
"""The CRUD operations for the provider endpoint references within
2634
Codegate.
@@ -87,12 +95,12 @@ async def add_endpoint(
8795

8896
models = []
8997
if endpoint.auth_type == apimodelsv1.ProviderAuthType.api_key and not endpoint.api_key:
90-
raise ValueError("API key must be provided for API auth type")
98+
raise ProviderInvalidAuthConfigError("API key must be provided for API auth type")
9199
if endpoint.auth_type != apimodelsv1.ProviderAuthType.passthrough:
92100
try:
93101
models = prov.models(endpoint=endpoint.endpoint, api_key=endpoint.api_key)
94102
except Exception as err:
95-
raise ValueError("Unable to get models from provider: {}".format(str(err)))
103+
raise ProviderModelsNotFoundError(f"Unable to get models from provider: {err}")
96104

97105
dbendpoint = await self._db_writer.add_provider_endpoint(dbend)
98106

@@ -143,22 +151,14 @@ async def configure_auth_material(
143151
):
144152
"""Add an API key."""
145153
if config.auth_type == apimodelsv1.ProviderAuthType.api_key and not config.api_key:
146-
raise ValueError("API key must be provided for API auth type")
154+
raise ProviderInvalidAuthConfigError("API key must be provided for API auth type")
147155
elif config.auth_type != apimodelsv1.ProviderAuthType.api_key and config.api_key:
148-
raise ValueError("API key provided for non-API auth type")
156+
raise ProviderInvalidAuthConfigError("API key provided for non-API auth type")
149157

150158
dbendpoint = await self._db_reader.get_provider_endpoint_by_id(str(provider_id))
151159
if dbendpoint is None:
152160
raise ProviderNotFoundError("Provider not found")
153161

154-
await self._db_writer.push_provider_auth_material(
155-
dbmodels.ProviderAuthMaterial(
156-
provider_endpoint_id=dbendpoint.id,
157-
auth_type=config.auth_type,
158-
auth_blob=config.api_key if config.api_key else "",
159-
)
160-
)
161-
162162
endpoint = apimodelsv1.ProviderEndpoint.from_db_model(dbendpoint)
163163
endpoint.auth_type = config.auth_type
164164
provider_registry = get_provider_registry()
@@ -169,7 +169,15 @@ async def configure_auth_material(
169169
try:
170170
models = prov.models(endpoint=endpoint.endpoint, api_key=config.api_key)
171171
except Exception as err:
172-
raise ValueError("Unable to get models from provider: {}".format(str(err)))
172+
raise ProviderModelsNotFoundError(f"Unable to get models from provider: {err}")
173+
174+
await self._db_writer.push_provider_auth_material(
175+
dbmodels.ProviderAuthMaterial(
176+
provider_endpoint_id=dbendpoint.id,
177+
auth_type=config.auth_type,
178+
auth_blob=config.api_key if config.api_key else "",
179+
)
180+
)
173181

174182
models_set = set(models)
175183

0 commit comments

Comments
 (0)