class Sensit::HttpClient::ErrorHandler
ErrorHanlder takes care of selecting the error message from response body
Public Class Methods
new(app)
click to toggle source
Calls superclass method
# File lib/sensit/http_client/error_handler.rb, line 8 def initialize(app) super(app) end
Public Instance Methods
call(env)
click to toggle source
# File lib/sensit/http_client/error_handler.rb, line 12 def call(env) @app.call(env).on_complete do |env| code = env[:response].status type = env[:response].headers["content-type"] case code when 500...599 raise Sensit::Error::ClientError.new "Error #{code}", code when 400...499 body = Sensit::HttpClient::ResponseHandler.get_body env[:response] message = "" # If HTML, whole body is taken if body.is_a? String message = body end # If JSON, a particular field is taken and used if type.include?("json") and body.is_a?(Hash) if body.has_key? "error" message = body["error"] else message = "Unable to select error message from json returned by request responsible for error" end end if message == "" message = "Unable to understand the content type of response returned by request responsible for error" end raise Sensit::Error::ClientError.new message, code end end end