Class: SearchSpring::Errors::RequestError

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/search_spring/errors.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/search_spring/errors.rb', line 11

def call(env)
  @request_data = {
    url: env.url.to_s,
    method: env.method,
    header: env[:request_headers],
    body: env.body
  }
  @app.call(env).on_complete do |environment|
    on_complete(environment)
  end
end

#on_complete(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/search_spring/errors.rb', line 23

def on_complete(env)
  # Ignore any non-error response codes
  return if (status = env[:status]) < 400
  case status
  when 404
    raise Errors::NotFound, response_values(env)
  when 422
    raise Errors::UnprocessableEntity, response_values(env)
  when 401
    raise Errors::NotAuthorized, response_values(env)
  when 407
    # mimic the behavior that we get with proxy requests with HTTPS
    raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
  when 408...600
    raise Errors::InternalServerError, response_values(env) # Treat any other errors as 500
  end
end

#response_values(env) ⇒ Object



41
42
43
44
45
46
# File 'lib/search_spring/errors.rb', line 41

def response_values(env)
  { status: env.status,
    headers: env.response_headers,
    body: env.body,
    request: (@request_data || {}) }
end