Class: FaradayMiddleware::RaiseHTTPException

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday/raise_http_exception.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RaiseHTTPException

Returns a new instance of RaiseHTTPException.



23
24
25
26
# File 'lib/faraday/raise_http_exception.rb', line 23

def initialize(app)
  super app
  @parser = nil
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/faraday/raise_http_exception.rb', line 7

def call(env)
  @app.call(env).on_complete do |response|
    response_hash = JSON.parse(response.body)
    msg = "#{response[:status]} #{response_hash['message']}"

    case response[:status]
    when 400 ; raise Postmates::BadRequest,          msg
    when 401 ; raise Postmates::Unauthorized,        msg
    when 403 ; raise Postmates::Forbidden,           msg
    when 404 ; raise Postmates::NotFound,            msg
    when 500 ; raise Postmates::InternalServerError, msg
    when 503 ; raise Postmates::ServiceUnavailable,  msg
    end
  end
end