vajapravin / pushbullet

PushBullet's API enables developers to push to devices that have installed the PushBullet Android app.
http://www.pushbullet.com
MIT License
10 stars 11 forks source link

Pushbullet client swallows error messages #5

Open shalecraig opened 8 years ago

shalecraig commented 8 years ago

In the http_exception code, this gem swallows exceptions from pushbullet ("The param 'device_iden' has an invalid value") and offers helpful messages like 'Often missing a required parameter'.

For myself, I overrode the Pushbullet::HttpException exception to get better errors:

module Pushbullet
  class HttpException < Faraday::Response::Middleware
    def call(env)
      @app.call(env).on_complete do |response|
        binding.pry unless response[:status].to_i == 200
        attempted_error_message = response[:body]
        attempted_error_message = JSON.parse(attempted_error_message) if attempted_error_message
        attempted_error_message = attempted_error_message['error'] if attempted_error_message
        attempted_error_message = attempted_error_message['message'] if attempted_error_message
        case response[:status]
        when 400
          raise Pushbullet::BadRequest,    attempted_error_message || 'Often missing a required parameter'
        when 401
          raise Pushbullet::Unauthorized,  attempted_error_message || 'No valid API key provided'
        when 402
          raise Pushbullet::RequestFailed, attempted_error_message || 'Parameters were valid but the request failed'
        when 403
          raise Pushbullet::Forbidden,     attempted_error_message || 'The API key is not valid for that request'
        when 404
          raise Pushbullet::NotFound,      attempted_error_message || 'The requested item doesn\'t exist'
        when 500..505
          raise Pushbullet::ServerError,   attempted_error_message || 'Something went wrong on PushBullet\'s side'
        end
      end
    end
  end
end