Class: CiscoMSE::Middleware::ExceptionRaiser

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/cisco-mse-client/middleware/exception_raiser.rb

Overview

Raise beautiful exceptions

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

For handling errors, the message that gets returned is of the following format: => env, :headers => env, :body => env



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cisco-mse-client/middleware/exception_raiser.rb', line 19

def call(env)
  begin
    @app.call(env)
  rescue Faraday::Error::ClientError => e
    if e.response
      exception =
        case e.response[:status]
        when 404
          CiscoMSE::NotFoundError
        when 500
          CiscoMSE::ServiceError
        else
          CiscoMSE::GenericException
        end
      raise exception, e.response
    else
      raise CiscoMSE::GenericException, e
    end
  rescue Saddle::TimeoutError => e
    raise CiscoMSE::TimeoutError, e.response
  end
end