-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror_handling_example.rb
More file actions
220 lines (193 loc) · 6.08 KB
/
error_handling_example.rb
File metadata and controls
220 lines (193 loc) · 6.08 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'claude_agent_sdk'
require 'async'
# Example: Handling errors with AssistantMessage.error
# The error field can contain values like:
# - authentication_failed: API key or auth issue
# - billing_error: Account billing problem
# - rate_limit: Too many requests
# - invalid_request: Malformed request
# - server_error: Claude server issue
# - unknown: Unclassified error
puts "=== Error Handling Example ==="
puts "Demonstrating AssistantMessage.error field handling\n\n"
# Helper to display error information
def display_error(error_type)
case error_type
when 'authentication_failed'
puts " -> Authentication failed. Check your API key."
puts " Action: Verify ANTHROPIC_API_KEY environment variable"
when 'billing_error'
puts " -> Billing error. Check your account status."
puts " Action: Visit console.anthropic.com to check billing"
when 'rate_limit'
puts " -> Rate limited. Too many requests."
puts " Action: Implement exponential backoff and retry"
when 'invalid_request'
puts " -> Invalid request format."
puts " Action: Check request parameters"
when 'server_error'
puts " -> Claude server error."
puts " Action: Retry after a short delay"
when 'unknown'
puts " -> Unknown error occurred."
puts " Action: Check logs for details"
else
puts " -> Unhandled error type: #{error_type}"
end
end
# Example 1: Basic error checking
puts "--- Example 1: Basic Error Checking ---"
options = ClaudeAgentSDK::ClaudeAgentOptions.new(
max_turns: 1
)
ClaudeAgentSDK.query(
prompt: "Hello, how are you?",
options: options
) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
if message.error
puts "Error detected: #{message.error}"
display_error(message.error)
else
message.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
end
when ClaudeAgentSDK::ResultMessage
if message.is_error
puts "\nResult indicates error occurred"
else
puts "\nCompleted successfully"
end
end
end
puts "\n" + "=" * 50 + "\n"
# Example 2: Error handling with retry logic
puts "\n--- Example 2: Error Handling with Retry ---"
def query_with_retry(prompt, max_retries: 3, base_delay: 1)
retries = 0
success = false
while retries < max_retries && !success
puts "Attempt #{retries + 1}/#{max_retries}..."
ClaudeAgentSDK.query(prompt: prompt) do |message|
case message
when ClaudeAgentSDK::AssistantMessage
if message.error
case message.error
when 'rate_limit'
delay = base_delay * (2**retries)
puts "Rate limited. Waiting #{delay}s before retry..."
sleep(delay)
retries += 1
when 'server_error'
delay = base_delay * (2**retries)
puts "Server error. Waiting #{delay}s before retry..."
sleep(delay)
retries += 1
when 'authentication_failed', 'billing_error'
puts "Non-retryable error: #{message.error}"
display_error(message.error)
return false
else
puts "Error: #{message.error}"
retries += 1
end
else
message.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
end
when ClaudeAgentSDK::ResultMessage
success = !message.is_error
end
end
end
success
end
result = query_with_retry("What is 1 + 1?")
puts result ? "Query succeeded!" : "Query failed after retries"
puts "\n" + "=" * 50 + "\n"
# Example 3: Client with comprehensive error handling
puts "\n--- Example 3: Client with Comprehensive Error Handling ---"
Async do
client = ClaudeAgentSDK::Client.new
begin
puts "Connecting..."
client.connect
puts "Connected!\n"
queries = [
"What is Ruby?",
"What is Python?",
"What is JavaScript?"
]
queries.each_with_index do |query, idx|
puts "\n--- Query #{idx + 1}: #{query} ---"
begin
client.query(query)
client.receive_response do |msg|
case msg
when ClaudeAgentSDK::AssistantMessage
if msg.error
puts "Error in response: #{msg.error}"
display_error(msg.error)
# Decide whether to continue based on error type
case msg.error
when 'authentication_failed', 'billing_error'
puts "Fatal error - stopping further queries"
raise "Fatal API error: #{msg.error}"
when 'rate_limit'
puts "Will retry after delay..."
sleep(2)
end
else
msg.content.each do |block|
puts block.text if block.is_a?(ClaudeAgentSDK::TextBlock)
end
end
when ClaudeAgentSDK::ResultMessage
if msg.is_error
puts "Query #{idx + 1} completed with error"
else
puts "Query #{idx + 1} completed successfully"
puts "Cost: $#{msg.total_cost_usd}" if msg.total_cost_usd
end
end
end
rescue StandardError => e
puts "Exception during query: #{e.message}"
break
end
end
rescue StandardError => e
puts "Client error: #{e.message}"
ensure
puts "\nDisconnecting..."
client.disconnect
puts "Done!"
end
end.wait
puts "\n" + "=" * 50 + "\n"
# Example 4: Error type constants reference
puts "\n--- Error Type Reference ---"
puts "Available error types (from ASSISTANT_MESSAGE_ERRORS):"
ClaudeAgentSDK::ASSISTANT_MESSAGE_ERRORS.each do |error_type|
puts " - #{error_type}"
end
puts "\nUsage pattern:"
puts <<~EXAMPLE
ClaudeAgentSDK.query(prompt: "...") do |message|
if message.is_a?(ClaudeAgentSDK::AssistantMessage) && message.error
case message.error
when 'rate_limit'
# Handle rate limiting
when 'authentication_failed'
# Handle auth errors
# ... etc
end
end
end
EXAMPLE