Module: Leggy::ErrorHandler

Included in:
Resource::App, Resource::Crawl, Resource::Result, Resource::Url, Resource::User
Defined in:
lib/leggy/error_handler.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/leggy/error_handler.rb', line 4

def self.included(base)
  base.send(:resources) do

    default_handler do |response|
      case response.status
      when 200...299
        next
      when 400
        raise Leggy::Exception::BadRequest.new("#{response.status}: #{response.body}")
      when 401
        raise Leggy::Exception::Unauthorized.new("#{response.status}: #{response.body}")
      when 404
        raise Leggy::Exception::NotFound.new("#{response.status}: #{response.body}")
      when 422
        raise Leggy::Exception::UnprocessableEntity.new("#{response.status}: #{response.body}")
      when 523
        raise Leggy::Exception::ServiceUnavailable.new("#{response.status}: #{response.body}")
      else
        raise Leggy::Exception::ServerError.new("#{response.status}: #{response.body}")
      end
    end

  end
end