summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-21 20:57:29 -0800
committerJared Norman <jared@super.gd>2019-01-21 21:08:50 -0800
commit40b63f8f069b6d869619bbfcb1554a7e7cbcbe57 (patch)
tree32a576e4f61e59a36111912b232622fd4f094780
parentd5b84c70cfad6bcb98bee422ce2865dbf8ae386f (diff)
Handle errors gracefully
-rw-r--r--README.md36
-rw-r--r--lib/super_good/solidus_taxjar/tax_calculator.rb8
-rw-r--r--spec/super_good/solidus_taxjar/tax_calculator_spec.rb28
3 files changed, 66 insertions, 6 deletions
diff --git a/README.md b/README.md
index ad11782..3e2c0a7 100644
--- a/README.md
+++ b/README.md
@@ -6,15 +6,39 @@ This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spr
## Installation
-Add this line to your application's Gemfile:
+1. Add this line to your application's Gemfile:
-```ruby
-gem 'super_good-solidus_taxjar'
-```
+ ```ruby
+ gem 'super_good-solidus_taxjar'
+ ```
-And then execute:
+ And then execute:
- $ bundle
+ $ bundle
+
+2. Next, configure Solidus to use this gem:
+
+ ```ruby
+ # Put this in config/initializers/solidus.rb
+
+ Spree.config do |config|
+ config.tax_calculator_class = SuperGood::SolidusTaxJar::TaxCalculator
+ end
+ ```
+
+3. Also, configure your error handling:
+
+ ```ruby
+ # Put this in config/initializers/taxjar.rb
+
+ SuperGood::SolidusTaxJar::TaxCalculator.exception_handler = ->(e) {
+ # Report exceptions in here. For example, if you were using the Sentry's
+ # raven-ruby gem to report errors, you might do this:
+ Raven.capture_exception(exception)
+ }
+ ```
+
+4. Finally, make sure that the `TAXJAR_API_KEY` environment variable is set to a your TaxJar API key and make sure that you have a `Spree::TaxRate` with the name "Sales Tax". This will be used as the source for the tax adjustments that Solidus creates.
## Development
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb
index d683cf6..474f762 100644
--- a/lib/super_good/solidus_taxjar/tax_calculator.rb
+++ b/lib/super_good/solidus_taxjar/tax_calculator.rb
@@ -1,6 +1,11 @@
module SuperGood
module SolidusTaxJar
class TaxCalculator
+ class_attribute :exception_handler
+ self.exception_handler = ->(e) {
+ Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
+ }
+
def self.default_api
::SuperGood::SolidusTaxJar::API.new
end
@@ -22,6 +27,9 @@ module SuperGood
shipment_taxes: shipment_taxes
)
end
+ rescue StandardError => e
+ exception_handler.(e)
+ no_tax
end
private
diff --git a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb
index 875f428..87d30c4 100644
--- a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb
+++ b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb
@@ -71,6 +71,34 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do
end
end
+ context "when the API encounters an error" do
+ let(:address) do
+ ::Spree::Address.new(
+ first_name: "Ronnie James",
+ country: ::Spree::Country.new(iso: "US")
+ )
+ end
+
+ before do
+ allow(dummy_api).to receive(:tax_for).with(order).and_raise("A bad thing happened.")
+ end
+
+ it "calls the configured error handler" do
+ expect(described_class.exception_handler).to receive(:call) do |e|
+ expect(e).to be_a StandardError
+ expect(e.message).to eq "A bad thing happened."
+ end
+
+ subject
+ end
+
+ it "returns no taxes" do
+ expect(subject.order_id).to eq order.id
+ expect(subject.shipment_taxes).to be_empty
+ expect(subject.line_item_taxes).to be_empty
+ end
+ end
+
context "when the order has a non-empty tax address" do
let(:address) do
::Spree::Address.new(