improve error handling.

This commit is contained in:
2013-06-03 00:18:12 +01:00
parent e245cac624
commit a00e0b4c51
3 changed files with 25 additions and 8 deletions

View File

@@ -6,3 +6,4 @@ gem 'spree_auth_devise', github: 'spree/spree_auth_devise', :branch => '2-0-stab
gemspec gemspec
gem 'httparty' gem 'httparty'
gem 'json'

View File

@@ -8,7 +8,7 @@ module BurstNet
self.hostname = params['hostname'] self.hostname = params['hostname']
self.order_id = params['order'] self.order_id = params['order']
self.status = params['status'] self.status = params['status']
self.product_id = params['pid '] self.product_id = params['pid '] # Not a typo - the field really comes like this
self.dedicatedip = params['dedicatedip'] self.dedicatedip = params['dedicatedip']
self.assignedips = params['assignedips'] self.assignedips = params['assignedips']
self.username = params['username'] self.username = params['username']

View File

@@ -32,6 +32,10 @@ require 'json'
PUT service/:id PUT service/:id
=end =end
module BurstNet module BurstNet
class APIResponseError < StandardError
end
class API class API
include HTTParty include HTTParty
base_uri 'https://rsapi.burst.net' base_uri 'https://rsapi.burst.net'
@@ -55,8 +59,11 @@ module BurstNet
if response.success? if response.success?
response.parsed_response response.parsed_response
else else
raise response.response raise APIResponseError.new(response.response)
end end
rescue TypeError
Rails.logger.error(response.inspect)
raise APIResponseError.new('Error in response. Please check your logs.')
end end
def post(path, options = {}) def post(path, options = {})
@@ -64,8 +71,11 @@ module BurstNet
if response.success? if response.success?
response.parsed_response response.parsed_response
else else
raise response.response raise APIResponseError.new(response.response)
end end
rescue TypeError
Rails.logger.error(response.inspect)
raise APIResponseError.new('Error in response. Please check your logs.')
end end
def put(path, options = {}) def put(path, options = {})
@@ -73,8 +83,11 @@ module BurstNet
if response.success? if response.success?
response.parsed_response response.parsed_response
else else
raise response.response raise APIResponseError.new(response.response)
end end
rescue TypeError
Rails.logger.error(response.inspect)
raise APIResponseError.new('Error in response. Please check your logs.')
end end
def delete(path, options = {}) def delete(path, options = {})
@@ -82,8 +95,11 @@ module BurstNet
if response.success? if response.success?
response.parsed_response response.parsed_response
else else
raise response.response raise APIResponseError.new(response.response)
end end
rescue TypeError
Rails.logger.error(response.inspect)
raise APIResponseError.new('Error in response. Please check your logs.')
end end
def get_products def get_products
@@ -134,7 +150,7 @@ module BurstNet
def get_services_by_status(status) def get_services_by_status(status)
possible_status = %w(active cancelled fraud pending suspended terminated) possible_status = %w(active cancelled fraud pending suspended terminated)
raise('Unknown service status') unless possible_status.include?(status) raise ArgumentError.new('Unknown service status') unless possible_status.include?(status)
self.get("/service/#{status}") self.get("/service/#{status}")
end end
@@ -144,7 +160,7 @@ module BurstNet
def get_orders_by_status(status) def get_orders_by_status(status)
possible_status = %w(active cancelled fraud pending) possible_status = %w(active cancelled fraud pending)
raise('Unknown order status') unless possible_status.include?(status) raise ArgumentError.new('Unknown order status') unless possible_status.include?(status)
self.get("/order/#{status}") self.get("/order/#{status}")
end end