Skip to content

Commit

Permalink
Use multi_logger gem for logging Fastbill-related errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Oct 13, 2015
1 parent 4076f22 commit 05a44dd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ gem 'prawn_rails' # pdf generation
gem 'newrelic_rpm', group: [:production, :staging]
gem 'rack-mini-profiler'
gem 'lograge'
gem 'multi_logger'

# ---------- API ----------

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ GEM
money (~> 6.5.0)
railties (>= 3.0)
multi_json (1.10.1)
multi_logger (0.1.0)
railties
multipart-post (2.0.0)
nenv (0.2.0)
nested_form (0.3.2)
Expand Down Expand Up @@ -734,6 +736,7 @@ DEPENDENCIES
modernizr-rails
monetize
money-rails (> 0.12.0)
multi_logger
newrelic_rpm
nkss-rails!
nokogiri
Expand Down
38 changes: 24 additions & 14 deletions app/objects/service/fastbill_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ def update_profile user
def fastbill_create_customer
unless @seller.fastbill_id
User.observers.disable :user_observer do
attributes = attributes_for(@seller)
attributes[:customer_number] = @seller.id
customer = Fastbill::Automatic::Customer.create(attributes)
@seller.update_attribute :fastbill_id, customer.customer_id
begin
attributes = attributes_for(@seller)
attributes[:customer_number] = @seller.id
customer = Fastbill::Automatic::Customer.create(attributes)
@seller.update_attribute :fastbill_id, customer.customer_id
Rails.logger.fastbill.info { "Fastbill profile created for user #{@seller.id}" }
rescue => e
Rails.logger.fastbill.error { "#{e.inspect} #{e.backtrace}" }
end
end
end
end
Expand Down Expand Up @@ -95,16 +100,21 @@ def fastbill_create_subscription
[:fee, :fair, :discount, :refund_fee, :refund_fair].each do |type|
define_method "fastbill_#{ type }" do
unless @bt.send("billed_for_#{ type }")
Fastbill::Automatic::Subscription.setusagedata(
subscription_id: @seller.fastbill_subscription_id,
article_number: article_number_for(type),
quantity: quantity_for(type),
unit_price: unit_price_for(type),
description: description_for(type),
usage_date: @bt.sold_at.strftime('%Y-%m-%d %H:%M:%S')
)
@bt.send("billed_for_#{ type }=", true)
@bt.save
begin
Fastbill::Automatic::Subscription.setusagedata(
subscription_id: @seller.fastbill_subscription_id,
article_number: article_number_for(type),
quantity: quantity_for(type),
unit_price: unit_price_for(type),
description: description_for(type),
usage_date: @bt.sold_at.strftime('%Y-%m-%d %H:%M:%S')
)
@bt.send("billed_for_#{ type }=", true)
@bt.save
Rails.logger.fastbill.info { "Fastbill usage data sent for business transaction #{@bt.id}" }
rescue => e
Rails.logger.fastbill.error { "#{e.inspect} #{e.backtrace}" }
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions config/initializers/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MultiLogger.add_logger('fastbill')
18 changes: 18 additions & 0 deletions test/objects/fastbill_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
api.expects(:fastbill_create_subscription)
api.fastbill_chain
end

it 'should log error if creating the profile raises an exception' do
db_business_transaction # to trigger observers before
api = FastbillAPI.new db_business_transaction
Fastbill::Automatic::Customer.stub :create, -> (_arg) { raise StandardError.new } do
Rails.logger.fastbill.expects(:error)
api.fastbill_chain
end
end
end

it 'should set usage data for subscription' do
Expand All @@ -52,6 +61,15 @@
Fastbill::Automatic::Subscription.expects(:setusagedata).twice
api.fastbill_chain
end

it 'should log error if set usage data raises exception' do
db_business_transaction # to trigger observers before
api = FastbillAPI.new db_business_transaction
Fastbill::Automatic::Subscription.stub :setusagedata, -> (_arg) { raise StandardError.new } do
Rails.logger.fastbill.expects(:error).twice
api.fastbill_chain
end
end
end

describe 'article price is 0 Euro' do
Expand Down

0 comments on commit 05a44dd

Please sign in to comment.