-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path01-basic-error-handling.php
More file actions
120 lines (97 loc) · 3.77 KB
/
Copy path01-basic-error-handling.php
File metadata and controls
120 lines (97 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
/**
* Tutorial 1: Basic Error Handling
*
* This example demonstrates the fundamentals of using the ErrorHandlingService
* to convert technical API errors into user-friendly messages.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
use ClaudeAgents\Services\ServiceManager;
use ClaudeAgents\Services\ServiceType;
use ClaudeAgents\Services\ErrorHandling\ErrorHandlingServiceFactory;
use ClaudePhp\ClaudePhp;
echo "Tutorial 1: Basic Error Handling\n";
echo str_repeat('=', 70) . "\n\n";
// Step 1: Get the ErrorHandlingService from ServiceManager
echo "Step 1: Getting ErrorHandlingService...\n";
$serviceManager = ServiceManager::getInstance();
$serviceManager->registerFactory(new ErrorHandlingServiceFactory());
$errorService = $serviceManager->get(ServiceType::ERROR_HANDLING);
echo "✓ Service retrieved successfully\n\n";
// Step 2: Test with invalid API key (triggers authentication error)
echo "Step 2: Testing Authentication Error Conversion...\n";
echo str_repeat('-', 70) . "\n";
$client = new ClaudePhp(apiKey: 'invalid_test_key');
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4',
'max_tokens' => 100,
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
} catch (\Throwable $e) {
// Technical error (what developers see)
echo "Technical Error:\n";
echo " Exception: " . get_class($e) . "\n";
echo " Message: " . $e->getMessage() . "\n";
echo "\n";
// User-friendly message (what end users see)
$userMessage = $errorService->convertToUserFriendly($e);
echo "User-Friendly Message:\n";
echo " {$userMessage}\n";
echo "\n";
}
// Step 3: Get detailed error information
echo "Step 3: Extracting Detailed Error Information...\n";
echo str_repeat('-', 70) . "\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4',
'max_tokens' => 100,
'messages' => [['role' => 'user', 'content' => 'Test']],
]);
} catch (\Throwable $e) {
$details = $errorService->getErrorDetails($e);
echo "Detailed Error Information:\n";
echo " Exception Class: {$details['exception_class']}\n";
echo " Error Code: {$details['code']}\n";
echo " Location: {$details['file']}:{$details['line']}\n";
if (isset($details['status_code'])) {
echo " HTTP Status: {$details['status_code']}\n";
}
if (isset($details['suggested_action'])) {
echo " Suggested Action: {$details['suggested_action']}\n";
}
echo "\n";
}
// Step 4: Test with valid API key (if available)
echo "Step 4: Testing with Valid API Key...\n";
echo str_repeat('-', 70) . "\n";
if ($apiKey = getenv('ANTHROPIC_API_KEY')) {
$validClient = new ClaudePhp(apiKey: $apiKey);
try {
$response = $validClient->messages()->create([
'model' => 'claude-sonnet-4',
'max_tokens' => 20,
'messages' => [
['role' => 'user', 'content' => 'Say hello in one word'],
],
]);
echo "✓ Request succeeded!\n";
echo " Response: " . $response->content[0]->text . "\n";
} catch (\Throwable $e) {
echo "✗ Request failed:\n";
echo " " . $errorService->convertToUserFriendly($e) . "\n";
}
} else {
echo "⚠ Set ANTHROPIC_API_KEY to test with valid credentials\n";
}
echo "\n" . str_repeat('=', 70) . "\n";
echo "Tutorial 1 Complete!\n\n";
echo "Key Takeaways:\n";
echo "• Use ServiceManager to get the ErrorHandlingService\n";
echo "• convertToUserFriendly() transforms technical errors into readable messages\n";
echo "• getErrorDetails() provides rich debugging information\n";
echo "• The service handles all Claude SDK exceptions automatically\n";