From d2d6e835375c9e02baa1d11a0160f153f528d317 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 11 Apr 2024 19:10:57 +0200 Subject: [PATCH] [Misc] Fix an exception during exception logging This broke after the upgrade to rails 7.1 because of https://github.com/rails/rails/pull/46535 --- app/models/exception_log.rb | 2 +- test/unit/exception_log_test.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/unit/exception_log_test.rb diff --git a/app/models/exception_log.rb b/app/models/exception_log.rb index 027392b49..8fd3c9b2d 100644 --- a/app/models/exception_log.rb +++ b/app/models/exception_log.rb @@ -21,7 +21,7 @@ class ExceptionLog < ApplicationRecord if unwrapped_exception.is_a?(ActiveRecord::QueryCanceled) extra_params[:sql] = {} extra_params[:sql][:query] = unwrapped_exception&.sql || "[NOT FOUND?]" - extra_params[:sql][:binds] = unwrapped_exception&.binds + extra_params[:sql][:binds] = unwrapped_exception&.binds&.map(&:value_for_database) end create!( diff --git a/test/unit/exception_log_test.rb b/test/unit/exception_log_test.rb new file mode 100644 index 000000000..2f32dac94 --- /dev/null +++ b/test/unit/exception_log_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "test_helper" + +class ExceptionLogTest < ActiveSupport::TestCase + self.use_transactional_tests = false + + should "log for query timeout errors with bind parameters" do + e = assert_raises(ActiveRecord::QueryCanceled) do + Post.connection.execute("SET STATEMENT_TIMEOUT = 50") + Post.from("pg_sleep(1), posts").where(description: "bind param").count + end + log = ExceptionLog.add(e, 1, ActionDispatch::TestRequest.new("rack.input" => "abc", "REMOTE_ADDR" => "127.0.0.1")) + assert_equal(["bind param"], log.extra_params["sql"]["binds"]) + ensure + Post.connection.execute("SET STATEMENT_TIMEOUT = 3000") + ExceptionLog.destroy_all + end +end