Class: DilisensePepClient::ErrorFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/dilisense_pep_client/errors.rb

Overview

Error factory for consistent error creation

Class Method Summary collapse

Class Method Details

.create_from_response(response, endpoint: nil, request_id: nil) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/dilisense_pep_client/errors.rb', line 393

def create_from_response(response, endpoint: nil, request_id: nil)
  case response.status
  when 401
    AuthenticationError.new(
      "API authentication failed",
      status: response.status,
      body: response.body,
      headers: response.headers,
      endpoint: endpoint,
      request_id: request_id
    )
  when 429
    retry_after = extract_retry_after(response.headers)
    RateLimitError.new(
      "API rate limit exceeded",
      status: response.status,
      body: response.body,
      headers: response.headers,
      retry_after: retry_after,
      endpoint: endpoint,
      request_id: request_id
    )
  when 400..499
    APIError.new(
      "Client error: #{response.status}",
      status: response.status,
      body: response.body,
      headers: response.headers,
      endpoint: endpoint,
      request_id: request_id
    )
  when 500..599
    APIError.new(
      "Server error: #{response.status}",
      status: response.status,
      body: response.body,
      headers: response.headers,
      endpoint: endpoint,
      request_id: request_id
    )
  else
    APIError.new(
      "Unexpected response: #{response.status}",
      status: response.status,
      body: response.body,
      headers: response.headers,
      endpoint: endpoint,
      request_id: request_id
    )
  end
end

.create_network_error(original_error, context: {}) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/dilisense_pep_client/errors.rb', line 445

def create_network_error(original_error, context: {})
  case original_error
  when ::Faraday::TimeoutError
    TimeoutError.new(
      "Request timeout",
      network_error: original_error,
      context: context
    )
  when ::Faraday::ConnectionFailed
    NetworkError.new(
      "Connection failed",
      network_error: original_error,
      context: context
    )
  else
    NetworkError.new(
      "Network error: #{original_error.class.name}",
      network_error: original_error,
      context: context
    )
  end
end