Skip to content

Commit 1a16c19

Browse files
committed
refactor(exception): replace InvalidArgumentException with ValidationError and ProtocolError for improved error handling
1 parent 9bb11b4 commit 1a16c19

40 files changed

+179
-170
lines changed

docs/cn/server/http-server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function createFileUploadTool(): RegisteredTool {
195195
// 验证文件名
196196
$filename = basename($filename);
197197
if (empty($filename)) {
198-
throw new InvalidArgumentException('无效的文件名');
198+
throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('无效的文件名');
199199
}
200200

201201
// 如果目录不存在则创建
@@ -207,7 +207,7 @@ function createFileUploadTool(): RegisteredTool {
207207
// 解码并保存文件
208208
$decodedContent = base64_decode($content);
209209
if ($decodedContent === false) {
210-
throw new InvalidArgumentException('无效的 base64 内容');
210+
throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('无效的 base64 内容');
211211
}
212212

213213
$filepath = $uploadDir . '/' . $filename;

docs/cn/server/hyperf-integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ class UserService
873873
];
874874

875875
if (!isset($users[$userId])) {
876-
throw new \InvalidArgumentException("用户 {$userId} 不存在");
876+
throw ValidationError::requiredFieldMissing('user', '用户 {$userId} 不存在');
877877
}
878878

879879
return ['user' => $users[$userId]];

docs/cn/server/stdio-server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ function createCalculatorTool(): RegisteredTool {
277277
case 'subtract': $result = $a - $b; break;
278278
case 'multiply': $result = $a * $b; break;
279279
case 'divide':
280-
if ($b == 0) throw new InvalidArgumentException('除零错误');
280+
if ($b == 0) throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('除零错误');
281281
$result = $a / $b;
282282
break;
283283
default:
284-
throw new InvalidArgumentException('未知操作: ' . $operation);
284+
throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('未知操作: ' . $operation);
285285
}
286286

287287
return [

docs/en/server/http-server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function createFileUploadTool(): RegisteredTool {
195195
// Validate filename
196196
$filename = basename($filename);
197197
if (empty($filename)) {
198-
throw new InvalidArgumentException('Invalid filename');
198+
throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('Invalid filename');
199199
}
200200

201201
// Create directory if it doesn't exist
@@ -207,7 +207,7 @@ function createFileUploadTool(): RegisteredTool {
207207
// Decode and save file
208208
$decodedContent = base64_decode($content);
209209
if ($decodedContent === false) {
210-
throw new InvalidArgumentException('Invalid base64 content');
210+
throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('Invalid base64 content');
211211
}
212212

213213
$filepath = $uploadDir . '/' . $filename;

docs/en/server/hyperf-integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ class UserService
873873
];
874874

875875
if (!isset($users[$userId])) {
876-
throw new \InvalidArgumentException("User {$userId} not found");
876+
throw ValidationError::requiredFieldMissing('user', 'User {$userId} not found');
877877
}
878878

879879
return ['user' => $users[$userId]];

docs/en/server/stdio-server.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ function createCalculatorTool(): RegisteredTool {
280280
case 'subtract': $result = $a - $b; break;
281281
case 'multiply': $result = $a * $b; break;
282282
case 'divide':
283-
if ($b == 0) throw new InvalidArgumentException('Division by zero');
283+
if ($b == 0) throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('Division by zero');
284284
$result = $a / $b;
285285
break;
286286
default:
287-
throw new InvalidArgumentException('Unknown operation: ' . $operation);
287+
throw new \Dtyq\PhpMcp\Shared\Exceptions\ValidationError('Unknown operation: ' . $operation);
288288
}
289289

290290
return [

examples/http-server-test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Dtyq\PhpMcp\Server\FastMcp\Resources\RegisteredResourceTemplate;
1212
use Dtyq\PhpMcp\Server\FastMcp\Tools\RegisteredTool;
1313
use Dtyq\PhpMcp\Server\McpServer;
14+
use Dtyq\PhpMcp\Shared\Exceptions\ValidationError;
1415
use Dtyq\PhpMcp\Shared\Kernel\Application;
1516
use Dtyq\PhpMcp\Types\Constants\MessageConstants;
1617
use Dtyq\PhpMcp\Types\Content\TextContent;
@@ -287,12 +288,12 @@ function createCalculatorTool(): RegisteredTool
287288
break;
288289
case 'divide':
289290
if ($b == 0) {
290-
throw new InvalidArgumentException('Division by zero');
291+
throw ValidationError::invalidFieldValue('divisor', 'cannot be zero');
291292
}
292293
$result = $a / $b;
293294
break;
294295
default:
295-
throw new InvalidArgumentException('Unknown operation: ' . $operation);
296+
throw ValidationError::invalidFieldValue('operation', 'unknown operation: ' . $operation);
296297
}
297298

298299
return [

examples/stdio-server-test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Dtyq\PhpMcp\Server\FastMcp\Resources\RegisteredResourceTemplate;
1212
use Dtyq\PhpMcp\Server\FastMcp\Tools\RegisteredTool;
1313
use Dtyq\PhpMcp\Server\McpServer;
14+
use Dtyq\PhpMcp\Shared\Exceptions\ValidationError;
1415
use Dtyq\PhpMcp\Shared\Kernel\Application;
1516
use Dtyq\PhpMcp\Types\Constants\MessageConstants;
1617
use Dtyq\PhpMcp\Types\Content\TextContent;
@@ -140,12 +141,12 @@ function createCalculatorTool(): RegisteredTool
140141
break;
141142
case 'divide':
142143
if ($b == 0) {
143-
throw new InvalidArgumentException('Division by zero');
144+
throw ValidationError::invalidFieldValue('divisor', 'cannot be zero');
144145
}
145146
$result = $a / $b;
146147
break;
147148
default:
148-
throw new InvalidArgumentException('Unknown operation: ' . $operation);
149+
throw ValidationError::invalidFieldValue('operation', 'unknown operation: ' . $operation);
149150
}
150151

151152
return [

examples/stdio_sequential-thinking-test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require_once __DIR__ . '/../vendor/autoload.php';
88

99
use Dtyq\PhpMcp\Client\McpClient;
10+
use Dtyq\PhpMcp\Shared\Exceptions\SystemException;
1011
use Dtyq\PhpMcp\Shared\Kernel\Application;
1112
use Psr\Container\ContainerInterface;
1213
use Psr\EventDispatcher\EventDispatcherInterface;
@@ -80,7 +81,7 @@ public function dispatch(object $event): object
8081
public function get(string $id): object
8182
{
8283
if (! isset($this->services[$id])) {
83-
throw new RuntimeException("Service {$id} not found");
84+
throw new SystemException("Service {$id} not found");
8485
}
8586

8687
return $this->services[$id];

src/Client/Core/AbstractSession.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Dtyq\PhpMcp\Types\Core\JsonRpcResponseInterface;
1818
use Dtyq\PhpMcp\Types\Core\RequestInterface;
1919
use Exception;
20-
use InvalidArgumentException;
2120

2221
/**
2322
* Abstract base class for MCP client session implementations.
@@ -202,7 +201,7 @@ protected function generateRequestId(): string
202201
protected function setDefaultTimeout(float $timeout): void
203202
{
204203
if ($timeout <= 0) {
205-
throw new InvalidArgumentException('Timeout must be greater than 0');
204+
throw new ProtocolError('Timeout must be greater than 0');
206205
}
207206
$this->defaultTimeout = $timeout;
208207
}

0 commit comments

Comments
 (0)