Skip to content

Commit

Permalink
Handle timeouts inside a view for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Sep 6, 2023
1 parent f138055 commit d1d3c25
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ def assert_method(method)
# wrap an api call in a timeout
def api_call_timeout(&block)
Timeout.timeout(Settings.api_timeout, Timeout::Error, &block)
rescue ActionView::Template::Error => e
e = e.cause

if e.is_a?(Timeout::Error) ||
(e.is_a?(ActiveRecord::StatementInvalid) && e.message.include?("execution expired"))
ActiveRecord::Base.connection.raw_connection.cancel
raise OSM::APITimeoutError
else
raise
end
rescue Timeout::Error
ActiveRecord::Base.connection.raw_connection.cancel
raise OSM::APITimeoutError
Expand Down

0 comments on commit d1d3c25

Please sign in to comment.