Skip to content

Commit 4463531

Browse files
committed
Merge pull request 'fix: do not raise error while not login' (#424) from oap_store into development
Reviewed-on: https://git.biggo.com/Funmula/dive-mcp-host/pulls/424
2 parents f70246b + 0ab46ef commit 4463531

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

dive_mcp_host/oap_plugin/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ class BaseResponse[T](BaseModel):
3232
status: Literal["success", "error"]
3333
error: str | None = None
3434
data: T | None = None
35+
36+
37+
class TokenNotSetError(Exception):
38+
"""Token not set error."""
39+
40+
message: str = "Token is not set"
41+
42+
def __str__(self) -> str:
43+
"""Return the error message."""
44+
return self.message

dive_mcp_host/oap_plugin/store.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mimetypes
22
import uuid
33
from collections.abc import AsyncGenerator
4-
from contextlib import asynccontextmanager
4+
from contextlib import asynccontextmanager, suppress
55
from logging import getLogger
66
from pathlib import Path
77

@@ -10,6 +10,7 @@
1010
from pydantic import BaseModel
1111

1212
from dive_mcp_host.host.store.base import StoreProtocol
13+
from dive_mcp_host.oap_plugin.models import TokenNotSetError
1314

1415
logger = getLogger(__name__)
1516

@@ -37,7 +38,7 @@ def __init__(self) -> None:
3738
async def _get_http_client(self) -> AsyncGenerator[httpx.AsyncClient, None]:
3839
"""Get the HTTP client."""
3940
if self._token is None:
40-
raise RuntimeError("Token is not set")
41+
raise TokenNotSetError
4142

4243
async with httpx.AsyncClient(
4344
headers={"Authorization": f"Bearer {self._token}"},
@@ -75,21 +76,24 @@ async def save_file(self, file: UploadFile | str) -> str | None:
7576
logger.debug("Saving as file %s", new_name)
7677
files = {"file": (new_name, file.file, file.content_type)}
7778

78-
async with self._get_http_client() as client:
79-
response = await client.post(
80-
f"{self._store_url}/upload_file",
81-
files=files,
82-
)
83-
if not response.is_success:
84-
logger.error("Failed to save file to the OAP store: %s", response.text)
85-
return None
86-
87-
json_resp = response.json()
88-
result = UploadFileResponse.model_validate(json_resp)
89-
if result.result:
90-
logger.debug("File saved to the OAP store: %s", result.url)
91-
return result.url
92-
return None
79+
with suppress(TokenNotSetError):
80+
async with self._get_http_client() as client:
81+
response = await client.post(
82+
f"{self._store_url}/upload_file",
83+
files=files,
84+
)
85+
if not response.is_success:
86+
logger.error(
87+
"Failed to save file to the OAP store: %s", response.text
88+
)
89+
return None
90+
91+
json_resp = response.json()
92+
result = UploadFileResponse.model_validate(json_resp)
93+
if result.result:
94+
logger.debug("File saved to the OAP store: %s", result.url)
95+
return result.url
96+
return None
9397

9498
async def get_file(self, file_path: str) -> bytes:
9599
"""Get file from the store."""

0 commit comments

Comments
 (0)