Class: Dropmark::Error::RaiseError

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/dropmark/error.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dropmark/error.rb', line 15

def on_complete(env)
  return if (status = env[:status]) < 400 # Ignore any non-error response codes
  
  message = parse(env[:body])
  case status
  when 400
    raise Dropmark::Error::BadRequest, message
  when 403
    raise Dropmark::Error::Forbidden, message
  when 404
    raise Dropmark::Error::NotFound, message
  else
    raise Dropmark::Error::ServerError, message # Treat any other errors as 500
  end
end

#parse(body) ⇒ Object



9
10
11
12
13
# File 'lib/dropmark/error.rb', line 9

def parse(body)
  if body = MultiJson.load(body, :symbolize_keys => true)
    body[:message] if body.has_key?(:message)
  end
end