-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patherror_handling.php
More file actions
executable file
·182 lines (159 loc) · 5.45 KB
/
error_handling.php
File metadata and controls
executable file
·182 lines (159 loc) · 5.45 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env php
<?php
/**
* Error Handling Examples
*
* Demonstrates proper error handling with the Claude PHP SDK,
* matching Python SDK patterns for exception handling.
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/helpers.php';
use ClaudePhp\ClaudePhp;
use ClaudePhp\Exceptions\AnthropicException;
use ClaudePhp\Exceptions\APIError;
use ClaudePhp\Exceptions\AuthenticationError;
use ClaudePhp\Exceptions\RateLimitError;
use ClaudePhp\Exceptions\APIConnectionError;
use ClaudePhp\Exceptions\APITimeoutError;
loadEnv(__DIR__ . '/../.env');
$client = new ClaudePhp(apiKey: getApiKey());
echo "=== Error Handling Examples ===\n\n";
// Example 1: Basic try-catch
echo "Example 1: Basic Error Handling\n";
echo "--------------------------------\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Hello, Claude!']
]
]);
echo "Success! Response: ";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
echo $block['text'] . "\n";
}
}
} catch (AnthropicException $e) {
echo "Error occurred: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 2: Specific exception types
echo "Example 2: Handling Specific Error Types\n";
echo "-----------------------------------------\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 100,
'messages' => [
['role' => 'user', 'content' => 'What is PHP?']
]
]);
echo "Response received successfully.\n";
} catch (AuthenticationError $e) {
echo "Authentication failed: {$e->getMessage()}\n";
echo "Please check your API key.\n";
} catch (RateLimitError $e) {
echo "Rate limit exceeded: {$e->getMessage()}\n";
echo "Please wait before making more requests.\n";
} catch (APIConnectionError $e) {
echo "Connection error: {$e->getMessage()}\n";
echo "Please check your network connection.\n";
} catch (APITimeoutError $e) {
echo "Request timed out: {$e->getMessage()}\n";
echo "Please try again.\n";
} catch (APIError $e) {
echo "API error: {$e->getMessage()}\n";
} catch (AnthropicException $e) {
echo "Unexpected error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 3: Validation errors
echo "Example 3: Handling Invalid Parameters\n";
echo "---------------------------------------\n";
try {
// This will fail because max_tokens is required
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'messages' => [
['role' => 'user', 'content' => 'Hello!']
]
// Missing max_tokens - will cause an error
]);
} catch (\InvalidArgumentException $e) {
echo "✓ Validation error caught correctly\n";
echo "Error: {$e->getMessage()}\n";
echo "Remember to include required parameters!\n";
} catch (AnthropicException $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 4: Testing with invalid API key
echo "Example 4: Invalid API Key Handling\n";
echo "------------------------------------\n";
try {
$invalidClient = new ClaudePhp(apiKey: 'invalid-key-for-testing');
$response = $invalidClient->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 100,
'messages' => [
['role' => 'user', 'content' => 'Hello']
]
]);
} catch (AuthenticationError $e) {
echo "✓ Authentication error caught correctly\n";
echo "Error message: {$e->getMessage()}\n";
} catch (AnthropicException $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 5: Retry logic
echo "Example 5: Retry Logic Pattern\n";
echo "-------------------------------\n";
function makeRequestWithRetry($client, $maxRetries = 3) {
$attempt = 0;
while ($attempt < $maxRetries) {
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 100,
'messages' => [
['role' => 'user', 'content' => 'What is 2+2?']
]
]);
return $response;
} catch (RateLimitError $e) {
$attempt++;
if ($attempt < $maxRetries) {
$waitTime = pow(2, $attempt); // Exponential backoff
echo "Rate limited. Waiting {$waitTime} seconds before retry {$attempt}...\n";
sleep($waitTime);
} else {
throw $e;
}
} catch (APIConnectionError $e) {
$attempt++;
if ($attempt < $maxRetries) {
echo "Connection error. Retrying attempt {$attempt}...\n";
sleep(1);
} else {
throw $e;
}
}
}
throw new Exception("Max retries exceeded");
}
try {
$response = makeRequestWithRetry($client);
echo "✓ Request successful with retry logic\n";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
echo "Response: {$block['text']}\n";
}
}
} catch (Exception $e) {
echo "Failed after retries: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
echo "✓ Error handling examples completed!\n";