Class: Tacokit::Middleware::RaiseError

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/tacokit/middleware/raise_error.rb

Constant Summary collapse

CLIENT_ERROR_STATUSES =
400...600

Instance Method Summary collapse

Instance Method Details

#error_message(env) ⇒ Object



22
23
24
# File 'lib/tacokit/middleware/raise_error.rb', line 22

def error_message(env)
  "Server returned #{env[:status]}: #{env.body}. Headers #{env.response_headers.inspect}"
end

#on_complete(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tacokit/middleware/raise_error.rb', line 6

def on_complete(env)
  case env[:status]
  when 401
    raise Tacokit::Error::Unauthorized, error_message(env)
  when 404
    raise Tacokit::Error::ResourceNotFound, error_message(env)
  when 407
    # mimic the behavior that we get with proxy requests with HTTPS
    raise Tacokit::Error::ConnectionFailed, %(407 "Proxy Authentication Required ")
  when 408
    raise Tacokit::Error::TimeoutError, error_message(env)
  when CLIENT_ERROR_STATUSES
    raise Tacokit::Error::ClientError, error_message(env)
  end
end