Class: DoorLoop::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/doorloop/error_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger, client) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



5
6
7
8
# File 'lib/doorloop/error_handler.rb', line 5

def initialize(logger, client)
  @logger = logger
  @client = client
end

Instance Method Details

#handle(exception) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/doorloop/error_handler.rb', line 10

def handle(exception)
  case exception.http_code
  when 401
    @logger.error('Unauthorized access')
    raise DoorLoop::UnauthorizedError, 'Unauthorized access'
  when 403
    @logger.error('Forbidden access')
    raise DoorLoop::ForbiddenError, 'Forbidden access'
  when 404
    @logger.error('Resource not found')
    raise DoorLoop::NotFoundError, 'Resource not found'
  when 429
    handle_rate_limit(exception)
  else
    @logger.error("Unexpected error: #{exception}")
    raise DoorLoop::Error, "Unexpected error: #{exception}"
  end
end

#handle_rate_limit(exception) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/doorloop/error_handler.rb', line 29

def handle_rate_limit(exception)
  retry_after = exception.response.headers[:retry_after].to_i
  if @client.retry_on_rate_limit
    begin
      @logger.warn("Rate limit exceeded, retrying in #{retry_after} seconds...")
      sleep(retry_after)
    rescue DoorLoop::TooManyRequestsError
      retry
    end
  else
    @logger.warn("Rate limit exceeded, retry after #{retry_after} seconds.")
    raise DoorLoop::TooManyRequestsError, 'Rate limit exceeded'
  end
end