diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index 03427f22de..95daff0f14 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -43,14 +43,15 @@ def index def create @donation = current_organization.donations.new(donation_params) - if DonationCreateService.call(@donation) - flash[:notice] = "Donation created and logged!" - redirect_to donations_path - else + begin + DonationCreateService.call(@donation) + flash[:notice] = "Donation created and logged!" + redirect_to donations_path + rescue => e load_form_collections @donation.line_items.build if @donation.line_items.count.zero? - flash[:error] = "There was an error starting this donation, try again?" - Rails.logger.error "[!] DonationsController#create Error: #{@donation.errors}" + flash[:error] = "There was an error starting this donation: #{e.message}" + Rails.logger.error "[!] DonationsController#create Error: #{e.message}" render action: :new end end diff --git a/app/controllers/purchases_controller.rb b/app/controllers/purchases_controller.rb index a77c34b018..4c1c263d8f 100644 --- a/app/controllers/purchases_controller.rb +++ b/app/controllers/purchases_controller.rb @@ -32,14 +32,15 @@ def index def create @purchase = current_organization.purchases.new(purchase_params) - if PurchaseCreateService.call(@purchase) + begin + PurchaseCreateService.call(@purchase) flash[:notice] = "New Purchase logged!" redirect_to purchases_path - else + rescue => e load_form_collections @purchase.line_items.build if @purchase.line_items.count.zero? - flash[:error] = "Failed to create purchase due to: #{@purchase.errors.full_messages}" - Rails.logger.error "[!] PurchasesController#create ERROR: #{@purchase.errors.full_messages}" + flash[:error] = "Failed to create purchase due to: #{e.message}" + Rails.logger.error "[!] PurchasesController#create ERROR: #{e.message}" render action: :new end end diff --git a/app/services/donation_create_service.rb b/app/services/donation_create_service.rb index 67d83aa73a..fb782ef7d8 100644 --- a/app/services/donation_create_service.rb +++ b/app/services/donation_create_service.rb @@ -2,10 +2,11 @@ module DonationCreateService class << self def call(donation) Donation.transaction do - if donation.save - donation.storage_location.increase_inventory(donation.line_item_values) - DonationEvent.publish(donation) + unless donation.save + raise donation.errors.full_messages.join("\n") end + donation.storage_location.increase_inventory(donation.line_item_values) + DonationEvent.publish(donation) end end end diff --git a/app/services/purchase_create_service.rb b/app/services/purchase_create_service.rb index 5ae9ddb8c1..16c1718bb5 100644 --- a/app/services/purchase_create_service.rb +++ b/app/services/purchase_create_service.rb @@ -2,10 +2,11 @@ class PurchaseCreateService class << self def call(purchase) Purchase.transaction do - if purchase.save - purchase.storage_location.increase_inventory(purchase.line_item_values) - PurchaseEvent.publish(purchase) + unless purchase.save + raise purchase.errors.full_messages.join("\n") end + purchase.storage_location.increase_inventory(purchase.line_item_values) + PurchaseEvent.publish(purchase) end end end