lib/washbullet/http_exception.rb in washbullet-0.3.1 vs lib/washbullet/http_exception.rb in washbullet-0.4.0
- old
+ new
@@ -7,23 +7,28 @@
class ServerError < StandardError; end
class HttpException < Faraday::Response::Middleware
def call(env)
@app.call(env).on_complete do |response|
- case response[:status].to_i
- when 400
- raise Washbullet::BadRequest, 'Often missing a required parameter'
- when 401
- raise Washbullet::Unauthorized, 'No valid API key provided'
- when 402
- raise Washbullet::RequestFailed, 'Parameters were valid but the request failed'
- when 403
- raise Washbullet::Forbidden, 'The API key is not valid for that request'
- when 404
- raise Washbullet::NotFound, 'The requested item doesn\'t exist'
- when 500..505
- raise Washbullet::ServerError, 'Something went wrong on PushBullet\'s side'
- end
+ exception =
+ case response.status
+ when 400 then Washbullet::BadRequest
+ when 401 then Washbullet::Unauthorized
+ when 402 then Washbullet::RequestFailed
+ when 403 then Washbullet::Forbidden
+ when 404 then Washbullet::NotFound
+ when 500..505 then Washbullet::ServerError
+ end
+
+ raise exception, error_message(response.body) if exception
end
+ end
+
+ private
+
+ def error_message(response_body)
+ hash = JSON.parse(response_body)['error']
+
+ [hash['message'], hash['cat']].join(' ')
end
end
end