Class: FaradayMiddleware::RaiseHttpException

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RaiseHttpException

Returns a new instance of RaiseHttpException.



29
30
31
32
# File 'lib/serrano/faraday.rb', line 29

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

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  @app.call(env).on_complete do |response|
    case response[:status].to_i
    when 400
      raise Serrano::BadRequest, error_message_400(response)
    when 404
      raise Serrano::NotFound, error_message_400(response)
    when 500
      raise Serrano::InternalServerError, error_message_500(response, 'Something is technically wrong.')
    when 502
      raise Serrano::BadGateway, error_message_500(response, 'The server returned an invalid or incomplete response.')
    when 503
      raise Serrano::ServiceUnavailable, error_message_500(response, 'Crossref is rate limiting your requests.')
    when 504
      raise Serrano::GatewayTimeout, error_message_500(response, '504 Gateway Time-out')
    end
  end
end