Fix linting
This commit is contained in:
@@ -19,18 +19,23 @@ from datetime import datetime
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
|
|
||||||
|
|
||||||
class ErrorCode(Enum):
|
class ErrorCode(Enum):
|
||||||
"""Error codes for MCP errors"""
|
"""Error codes for MCP errors"""
|
||||||
|
|
||||||
InvalidRequest = auto()
|
InvalidRequest = auto()
|
||||||
InternalError = auto()
|
InternalError = auto()
|
||||||
|
|
||||||
|
|
||||||
class McpError(Exception):
|
class McpError(Exception):
|
||||||
"""Base exception class for MCP errors"""
|
"""Base exception class for MCP errors"""
|
||||||
|
|
||||||
def __init__(self, code: ErrorCode, message: str):
|
def __init__(self, code: ErrorCode, message: str):
|
||||||
self.code = code
|
self.code = code
|
||||||
self.message = message
|
self.message = message
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
"server": {
|
"server": {
|
||||||
"host": "localhost",
|
"host": "localhost",
|
||||||
@@ -660,13 +665,17 @@ def test_generate_concurrent() -> None:
|
|||||||
try:
|
try:
|
||||||
async with session.post(url, json=data) as response:
|
async with session.post(url, json=data) as response:
|
||||||
if response.status != 200:
|
if response.status != 200:
|
||||||
error_msg = f"Request {request_id} failed with status {response.status}"
|
error_msg = (
|
||||||
|
f"Request {request_id} failed with status {response.status}"
|
||||||
|
)
|
||||||
if OutputControl.is_verbose():
|
if OutputControl.is_verbose():
|
||||||
print(f"\n{error_msg}")
|
print(f"\n{error_msg}")
|
||||||
raise McpError(ErrorCode.InternalError, error_msg)
|
raise McpError(ErrorCode.InternalError, error_msg)
|
||||||
result = await response.json()
|
result = await response.json()
|
||||||
if "error" in result:
|
if "error" in result:
|
||||||
error_msg = f"Request {request_id} returned error: {result['error']}"
|
error_msg = (
|
||||||
|
f"Request {request_id} returned error: {result['error']}"
|
||||||
|
)
|
||||||
if OutputControl.is_verbose():
|
if OutputControl.is_verbose():
|
||||||
print(f"\n{error_msg}")
|
print(f"\n{error_msg}")
|
||||||
raise McpError(ErrorCode.InternalError, error_msg)
|
raise McpError(ErrorCode.InternalError, error_msg)
|
||||||
@@ -681,7 +690,9 @@ def test_generate_concurrent() -> None:
|
|||||||
prompts = ["第一个问题", "第二个问题", "第三个问题", "第四个问题", "第五个问题"]
|
prompts = ["第一个问题", "第二个问题", "第三个问题", "第四个问题", "第五个问题"]
|
||||||
|
|
||||||
async with get_session() as session:
|
async with get_session() as session:
|
||||||
tasks = [make_request(session, prompt, i+1) for i, prompt in enumerate(prompts)]
|
tasks = [
|
||||||
|
make_request(session, prompt, i + 1) for i, prompt in enumerate(prompts)
|
||||||
|
]
|
||||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
|
||||||
# 收集成功和失败的结果
|
# 收集成功和失败的结果
|
||||||
@@ -692,7 +703,7 @@ def test_generate_concurrent() -> None:
|
|||||||
if isinstance(result, Exception):
|
if isinstance(result, Exception):
|
||||||
error_messages.append(f"Request {i+1} failed: {str(result)}")
|
error_messages.append(f"Request {i+1} failed: {str(result)}")
|
||||||
else:
|
else:
|
||||||
success_results.append((i+1, result))
|
success_results.append((i + 1, result))
|
||||||
|
|
||||||
# 如果有任何错误,在打印完所有结果后抛出异常
|
# 如果有任何错误,在打印完所有结果后抛出异常
|
||||||
if error_messages:
|
if error_messages:
|
||||||
@@ -706,7 +717,7 @@ def test_generate_concurrent() -> None:
|
|||||||
error_summary = "\n".join(error_messages)
|
error_summary = "\n".join(error_messages)
|
||||||
raise McpError(
|
raise McpError(
|
||||||
ErrorCode.InternalError,
|
ErrorCode.InternalError,
|
||||||
f"Some concurrent requests failed:\n{error_summary}"
|
f"Some concurrent requests failed:\n{error_summary}",
|
||||||
)
|
)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
@@ -721,7 +732,7 @@ def test_generate_concurrent() -> None:
|
|||||||
for i, result in enumerate(results, 1):
|
for i, result in enumerate(results, 1):
|
||||||
print(f"\nRequest {i} result:")
|
print(f"\nRequest {i} result:")
|
||||||
print_json_response(result)
|
print_json_response(result)
|
||||||
except McpError as e:
|
except McpError:
|
||||||
# 错误信息已经在之前打印过了,这里直接抛出
|
# 错误信息已经在之前打印过了,这里直接抛出
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user