summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/adapter/claude/errors_spec.rb
blob: 0d7632bfa30d3e6f9e374897ab4a1630bc4a2a17 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# frozen_string_literal: true

require "webmock/rspec"

RSpec.describe Dispatch::Adapter::Claude, "error mapping integration" do
  let(:model_id) { "claude-sonnet-4-5-20250929" }
  let(:api_key)  { "sk-ant-api03-test" }
  let(:base_url) { "https://api.anthropic.com" }

  subject(:adapter) do
    described_class.new(
      model: model_id,
      api_key: api_key,
      base_url: base_url
    )
  end

  let(:messages) do
    [Dispatch::Adapter::Message.new(
      role: "user",
      content: [Dispatch::Adapter::TextBlock.new(text: "Hello")]
    )]
  end

  before { WebMock.disable_net_connect! }
  after  { WebMock.reset! }

  # ── Helpers ───────────────────────────────────────────────────────────────

  def stub_api_error(status:, type:, message:, retry_after: nil)
    headers = { "Content-Type" => "application/json" }
    headers["Retry-After"] = retry_after.to_s if retry_after
    stub_request(:post, "#{base_url}/v1/messages")
      .to_return(
        status: status,
        body: JSON.generate({ "type" => "error", "error" => { "type" => type, "message" => message } }),
        headers: headers
      )
  end

  def stub_network_error(error_class)
    stub_request(:post, "#{base_url}/v1/messages").to_raise(error_class)
  end

  # ── 400 invalid_request_error → RequestError ─────────────────────────────

  describe "400 invalid_request_error" do
    before { stub_api_error(status: 400, type: "invalid_request_error", message: "Bad request body") }

    it "raises RequestError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::RequestError)
    end

    it "e.status_code == 400" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RequestError => e
      expect(e.status_code).to eq(400)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RequestError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RequestError => e
      expect(e.message).to include("Bad request body")
    end
  end

  # ── 401 authentication_error → AuthenticationError ───────────────────────

  describe "401 authentication_error" do
    before { stub_api_error(status: 401, type: "authentication_error", message: "Invalid API key") }

    it "raises AuthenticationError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::AuthenticationError)
    end

    it "e.status_code == 401" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::AuthenticationError => e
      expect(e.status_code).to eq(401)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::AuthenticationError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::AuthenticationError => e
      expect(e.message).to include("Invalid API key")
    end
  end

  # ── 403 permission_error → AuthenticationError ───────────────────────────

  describe "403 permission_error" do
    before { stub_api_error(status: 403, type: "permission_error", message: "Access denied") }

    it "raises AuthenticationError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::AuthenticationError)
    end

    it "e.status_code == 403" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::AuthenticationError => e
      expect(e.status_code).to eq(403)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::AuthenticationError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::AuthenticationError => e
      expect(e.message).to include("Access denied")
    end
  end

  # ── 422 unprocessable_entity → RequestError ──────────────────────────────

  describe "422 unprocessable_entity" do
    before { stub_api_error(status: 422, type: "unprocessable_entity", message: "Invalid field value") }

    it "raises RequestError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::RequestError)
    end

    it "e.status_code == 422" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RequestError => e
      expect(e.status_code).to eq(422)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RequestError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RequestError => e
      expect(e.message).to include("Invalid field value")
    end
  end

  # ── 429 rate_limit_error → RateLimitError w/ retry_after ─────────────────

  describe "429 rate_limit_error" do
    before { stub_api_error(status: 429, type: "rate_limit_error", message: "Too many requests", retry_after: 30) }

    it "raises RateLimitError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::RateLimitError)
    end

    it "e.status_code == 429" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RateLimitError => e
      expect(e.status_code).to eq(429)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RateLimitError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.retry_after == 30" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RateLimitError => e
      expect(e.retry_after).to eq(30)
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::RateLimitError => e
      expect(e.message).to include("Too many requests")
    end
  end

  # ── 500 internal_error → ServerError ─────────────────────────────────────

  describe "500 internal_error" do
    before { stub_api_error(status: 500, type: "internal_error", message: "Internal server error") }

    it "raises ServerError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::ServerError)
    end

    it "e.status_code == 500" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.status_code).to eq(500)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.message).to include("Internal server error")
    end
  end

  # ── 502 bad_gateway → ServerError ────────────────────────────────────────

  describe "502 bad_gateway" do
    before { stub_api_error(status: 502, type: "bad_gateway", message: "Bad gateway") }

    it "raises ServerError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::ServerError)
    end

    it "e.status_code == 502" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.status_code).to eq(502)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.message).to include("Bad gateway")
    end
  end

  # ── 503 service_unavailable → ServerError ────────────────────────────────

  describe "503 service_unavailable" do
    before { stub_api_error(status: 503, type: "service_unavailable", message: "Service temporarily unavailable") }

    it "raises ServerError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::ServerError)
    end

    it "e.status_code == 503" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.status_code).to eq(503)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ServerError => e
      expect(e.message).to include("Service temporarily unavailable")
    end
  end

  # ── 529 overloaded_error → OverloadedError (subclass of RateLimitError) ──

  describe "529 overloaded_error" do
    before { stub_api_error(status: 529, type: "overloaded_error", message: "Overloaded", retry_after: 60) }

    it "raises OverloadedError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::OverloadedError)
    end

    it "also caught by RateLimitError (OverloadedError is a subclass)" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::RateLimitError)
    end

    it "e.status_code == 529" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::OverloadedError => e
      expect(e.status_code).to eq(529)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::OverloadedError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.retry_after == 60" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::OverloadedError => e
      expect(e.retry_after).to eq(60)
    end

    it "e.message includes the error message from body" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::OverloadedError => e
      expect(e.message).to include("Overloaded")
    end
  end

  # ── Network connection refused → ConnectionError ──────────────────────────

  describe "network connection refused" do
    before { stub_network_error(Errno::ECONNREFUSED) }

    it "raises ConnectionError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::ConnectionError)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ConnectionError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end

    it "e.status_code is nil (no HTTP response)" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ConnectionError => e
      expect(e.status_code).to be_nil
    end

    it "e.message includes the provider name" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ConnectionError => e
      expect(e.message).to include("Anthropic (Claude)")
    end
  end

  # ── Network timeout → ConnectionError ────────────────────────────────────

  describe "network read timeout" do
    before { stub_network_error(Net::ReadTimeout) }

    it "raises ConnectionError" do
      expect { adapter.chat(messages) }.to raise_error(Dispatch::Adapter::ConnectionError)
    end

    it "e.provider == 'Anthropic (Claude)'" do
      adapter.chat(messages)
    rescue Dispatch::Adapter::ConnectionError => e
      expect(e.provider).to eq("Anthropic (Claude)")
    end
  end

  # ── OverloadedError class hierarchy ──────────────────────────────────────

  describe "OverloadedError class hierarchy" do
    it "OverloadedError.ancestors includes RateLimitError" do
      expect(Dispatch::Adapter::OverloadedError.ancestors).to include(Dispatch::Adapter::RateLimitError)
    end

    it "OverloadedError.ancestors includes Error" do
      expect(Dispatch::Adapter::OverloadedError.ancestors).to include(Dispatch::Adapter::Error)
    end

    it "OverloadedError.superclass is RateLimitError" do
      expect(Dispatch::Adapter::OverloadedError.superclass).to eq(Dispatch::Adapter::RateLimitError)
    end
  end
end