Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rfc] use spree default calculator when taxjar is down #60

Closed
AdnanTheExcellent opened this issue Apr 13, 2021 · 6 comments
Closed

[rfc] use spree default calculator when taxjar is down #60

AdnanTheExcellent opened this issue Apr 13, 2021 · 6 comments

Comments

@AdnanTheExcellent
Copy link
Collaborator

So..taxjar is currently down. Getting a lot of Rack::Timeout::RequestTimeoutException errors. What do you think would be the best way to handle that? rescuing StandardError does not catch that error.

@AdnanTheExcellent
Copy link
Collaborator Author

AdnanTheExcellent commented Apr 13, 2021

Instead of returning no_tax on rescue, what are your thoughts on defaulting to the Spree::TaxCalculator::Default calculator? If not set up, that would return no tax anyway. If a store has it set up enough to capture somewhat accurate tax (such as importing taxjars estimated rates), it would be better than nothing.

@jarednorman
Copy link
Member

I'm certainly open to making that an option and it would be very easy to implement: just call the default calculator and return that.

@nvandoorn
Copy link
Member

Hey @AdnanTheExcellent thanks for the RFC!

How do you feel about something like this?

# config/initializers/taxjar.rb

SuperGood::SolidusTaxjar.exception_handler = ->(e) {
  Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
  # If TaxJar is down, we swap in the default tax calculator.
  if e.is_a?(HTTP::ConnectionError)
    Spree::Config.tax_calculator_class = Spree::TaxCalculator::Default
    # This job is responsible for checking if TaxJar comes back up and then
    # setting `Spree::Config.tax_calculator_class` to
    # `SuperGood::SolidusTaxjar::TaxCalculator`.
    TaxjarPingJob.perform_later
  end
}

We feel it may be undesirable to silently call the default tax calculator as there are lots of ways you could handle TaxJar being down (some stores may have other calculators to fallback on, special logging, etc).

Suppose we did add some behavior around fallback calculators it could look like this:

# config/initializers/taxjar.rb

SuperGood::SolidusTaxjar.fallback_calculator = SomeCustomCalculator

@AdnanTheExcellent
Copy link
Collaborator Author

AdnanTheExcellent commented Apr 19, 2021

Hey @AdnanTheExcellent thanks for the RFC!

How do you feel about something like this?

# config/initializers/taxjar.rb

SuperGood::SolidusTaxjar.exception_handler = ->(e) {
  Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
  # If TaxJar is down, we swap in the default tax calculator.
  if e.is_a?(HTTP::ConnectionError)
    Spree::Config.tax_calculator_class = Spree::TaxCalculator::Default
    # This job is responsible for checking if TaxJar comes back up and then
    # setting `Spree::Config.tax_calculator_class` to
    # `SuperGood::SolidusTaxjar::TaxCalculator`.
    TaxjarPingJob.perform_later
  end
}

So i don't think this will work. timeouts are not a StandardError, so Taxjar timing out wont be caught by the exception handler as it is now. I had to add Rack::Timeout::RequestTimeoutException (what taxjar throws on timeout) to the rescue for my fallback to work:

      rescue StandardError, Rack::Timeout::RequestTimeoutException => e
        exception_handler.call(e)
        ::SuperGood::SolidusTaxJar.fallback_tax_calculator.new(order).calculate
      end

the fallback calculator could literally be no_tax and this will be the same functionality as now

We feel it may be undesirable to silently call the default tax calculator as there are lots of ways you could handle TaxJar being down (some stores may have other calculators to fallback on, special logging, etc).

Suppose we did add some behavior around fallback calculators it could look like this:

# config/initializers/taxjar.rb

SuperGood::SolidusTaxjar.fallback_calculator = SomeCustomCalculator

this is exactly what i'm trying to do. my fallback calculators look something like:

    self.fallback_tax_calculator = ::Spree::TaxCalculator::Default
    self.fallback_tax_rate_calculator =  -> (address) { ::Spree::TaxRate.for_address(address)&.first&.amount }

@AdnanTheExcellent
Copy link
Collaborator Author

AdnanTheExcellent commented Apr 20, 2021

On a slightly related note, i have a custom Zipcode activerecord model that i have extended to be a zone member, so i can make tax rates based off zipcode. This allows the fallback calculator to be ~99% accurate when taxjar is down, or when i want to quote based off a zipcode instead of wasting taxjar API calls to do it.

@Noah-Silvera
Copy link
Member

@AdnanTheExcellent we think the request for a fallback calculator is reasonable and valuable. We have added two tickets to capture that work #61 #62 .

Let's continue the discussion on those tickets if you have more thoughts and close this RFC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants