From f72a7674c011bc12a4f1053c1b8f3c430b48c726 Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Wed, 5 Feb 2025 15:24:43 +0400 Subject: [PATCH 1/7] Fix obscure errors by introducing FULL_TEXT_ERRORS --- lib/react_on_rails/prerender_error.rb | 12 ++++++++++-- lib/react_on_rails/utils.rb | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/react_on_rails/prerender_error.rb b/lib/react_on_rails/prerender_error.rb index bb9c617c93..b7a98ea2ad 100644 --- a/lib/react_on_rails/prerender_error.rb +++ b/lib/react_on_rails/prerender_error.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "rainbow" + # rubocop:disable: Layout/IndentHeredoc module ReactOnRails class PrerenderError < ::ReactOnRails::Error @@ -51,11 +53,17 @@ def calc_message(component_name, console_messages, err, js_code, props) message << <<~MSG Encountered error: - #{err} + #{err.inspect} MSG - backtrace = err.backtrace.first(15).join("\n") + backtrace = if ENV["FULL_TEXT_ERRORS"] == "true" + err.backtrace.join("\n") + else + "#{err.backtrace.first(15).join("\n")}\n" + + Rainbow("The rest of the backtrace is hidden. " \ + "To see the full backtrace, set FULL_TEXT_ERRORS=true.").red + end else backtrace = nil end diff --git a/lib/react_on_rails/utils.rb b/lib/react_on_rails/utils.rb index bd50dd8da5..e849d15283 100644 --- a/lib/react_on_rails/utils.rb +++ b/lib/react_on_rails/utils.rb @@ -8,7 +8,9 @@ module ReactOnRails module Utils - TRUNCATION_FILLER = "\n... TRUNCATED ...\n" + TRUNCATION_FILLER = "\n... TRUNCATED #{ + Rainbow('To see the full output, set FULL_TEXT_ERRORS=true.').red + } ...\n".freeze # https://forum.shakacode.com/t/yak-of-the-week-ruby-2-4-pathname-empty-changed-to-look-at-file-size/901 # return object if truthy, else return nil @@ -186,6 +188,7 @@ def self.react_on_rails_pro_version def self.smart_trim(str, max_length = 1000) # From https://stackoverflow.com/a/831583/1009332 str = str.to_s + return str if ENV["FULL_TEXT_ERRORS"] == "true" return str unless str.present? && max_length >= 1 return str if str.length <= max_length From f480ad7a7d31239c85eb1ff86607c12fc9beee28 Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Wed, 5 Feb 2025 16:20:55 +0400 Subject: [PATCH 2/7] add rspec tests --- spec/react_on_rails/prender_error_spec.rb | 25 ++++++++++ spec/react_on_rails/utils_spec.rb | 59 +++++++++++++++-------- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/spec/react_on_rails/prender_error_spec.rb b/spec/react_on_rails/prender_error_spec.rb index f824cf5351..08c77c2203 100644 --- a/spec/react_on_rails/prender_error_spec.rb +++ b/spec/react_on_rails/prender_error_spec.rb @@ -45,5 +45,30 @@ module ReactOnRails expect(expected_error.raven_context).to eq(expected_error_info) end end + + describe "error message formatting" do + context "when FULL_TEXT_ERRORS is true" do + before { ENV["FULL_TEXT_ERRORS"] = "true" } + after { ENV["FULL_TEXT_ERRORS"] = nil } + + it "shows the full backtrace" do + message = expected_error.message + expect(message).to include(err.inspect) + expect(message).to include(err.backtrace.join("\n")) + expect(message).not_to include("The rest of the backtrace is hidden") + end + end + + context "when FULL_TEXT_ERRORS is not set" do + before { ENV["FULL_TEXT_ERRORS"] = nil } + + it "shows truncated backtrace with notice" do + message = expected_error.message + expect(message).to include(err.inspect) + expect(message).to include(err.backtrace.first(15).join("\n")) + expect(message).to include("The rest of the backtrace is hidden") + end + end + end end end diff --git a/spec/react_on_rails/utils_spec.rb b/spec/react_on_rails/utils_spec.rb index a9ecbfa4da..b8158a4c66 100644 --- a/spec/react_on_rails/utils_spec.rb +++ b/spec/react_on_rails/utils_spec.rb @@ -310,30 +310,47 @@ module ReactOnRails end describe ".smart_trim" do - it "trims smartly" do - s = "1234567890" - - expect(described_class.smart_trim(s, -1)).to eq("1234567890") - expect(described_class.smart_trim(s, 0)).to eq("1234567890") - expect(described_class.smart_trim(s, 1)).to eq("1#{Utils::TRUNCATION_FILLER}") - expect(described_class.smart_trim(s, 2)).to eq("1#{Utils::TRUNCATION_FILLER}0") - expect(described_class.smart_trim(s, 3)).to eq("1#{Utils::TRUNCATION_FILLER}90") - expect(described_class.smart_trim(s, 4)).to eq("12#{Utils::TRUNCATION_FILLER}90") - expect(described_class.smart_trim(s, 5)).to eq("12#{Utils::TRUNCATION_FILLER}890") - expect(described_class.smart_trim(s, 6)).to eq("123#{Utils::TRUNCATION_FILLER}890") - expect(described_class.smart_trim(s, 7)).to eq("123#{Utils::TRUNCATION_FILLER}7890") - expect(described_class.smart_trim(s, 8)).to eq("1234#{Utils::TRUNCATION_FILLER}7890") - expect(described_class.smart_trim(s, 9)).to eq("1234#{Utils::TRUNCATION_FILLER}67890") - expect(described_class.smart_trim(s, 10)).to eq("1234567890") - expect(described_class.smart_trim(s, 11)).to eq("1234567890") + let(:long_string) { "1234567890" } + + context "when FULL_TEXT_ERRORS is true" do + before { ENV["FULL_TEXT_ERRORS"] = "true" } + after { ENV["FULL_TEXT_ERRORS"] = nil } + + it "returns the full string regardless of length" do + expect(described_class.smart_trim(long_string, 5)).to eq(long_string) + end + + it "handles a hash without trimming" do + hash = { a: long_string } + expect(described_class.smart_trim(hash, 5)).to eq(hash.to_s) + end end - it "trims handles a hash" do - s = { a: "1234567890" } + context "when FULL_TEXT_ERRORS is not set" do + before { ENV["FULL_TEXT_ERRORS"] = nil } + + it "trims smartly" do + expect(described_class.smart_trim(long_string, -1)).to eq("1234567890") + expect(described_class.smart_trim(long_string, 0)).to eq("1234567890") + expect(described_class.smart_trim(long_string, 1)).to eq("1#{Utils::TRUNCATION_FILLER}") + expect(described_class.smart_trim(long_string, 2)).to eq("1#{Utils::TRUNCATION_FILLER}0") + expect(described_class.smart_trim(long_string, 3)).to eq("1#{Utils::TRUNCATION_FILLER}90") + expect(described_class.smart_trim(long_string, 4)).to eq("12#{Utils::TRUNCATION_FILLER}90") + expect(described_class.smart_trim(long_string, 5)).to eq("12#{Utils::TRUNCATION_FILLER}890") + expect(described_class.smart_trim(long_string, 6)).to eq("123#{Utils::TRUNCATION_FILLER}890") + expect(described_class.smart_trim(long_string, 7)).to eq("123#{Utils::TRUNCATION_FILLER}7890") + expect(described_class.smart_trim(long_string, 8)).to eq("1234#{Utils::TRUNCATION_FILLER}7890") + expect(described_class.smart_trim(long_string, 9)).to eq("1234#{Utils::TRUNCATION_FILLER}67890") + expect(described_class.smart_trim(long_string, 10)).to eq("1234567890") + expect(described_class.smart_trim(long_string, 11)).to eq("1234567890") + end - expect(described_class.smart_trim(s, 9)).to eq( - "{:a=#{Utils::TRUNCATION_FILLER}890\"}" - ) + it "trims handles a hash" do + s = { a: "1234567890" } + expect(described_class.smart_trim(s, 9)).to eq( + "{:a=#{Utils::TRUNCATION_FILLER}890\"}" + ) + end end end From cf81d07d926d1a3affca72c9d3737f40b42f71fc Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Wed, 5 Feb 2025 16:24:20 +0400 Subject: [PATCH 3/7] update changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d455aa932..d4e15a9968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac ### [Unreleased] Changes since the last non-beta release. +#### Fixed + - Fix obscure errors by introducing FULL_TEXT_ERRORS [PR 1695](https://github.com/shakacode/react_on_rails/pull/1695) by [Romex91](https://github.com/Romex91). + ### [14.1.1] - 2025-01-15 #### Fixed @@ -387,7 +390,7 @@ Do not use. Unpublished. Caused by an issue with the release script. #### Fixed - Don't apply babel-plugin-transform-runtime inside react-on-rails to work with babel 7. [PR 1136](https://github.com/shakacode/react_on_rails/pull/1136) by [Ryunosuke Sato](https://github.com/tricknotes). - Add support for webpacker 4 prereleases. [PR 1134](https://github.com/shakacode/react_on_rails/pull/1134) by [Judahmeek](https://github.com/Judahmeek)) - +CHANGELOG ### [11.1.2] - 2018-08-18 #### Fixed From 5a500627e85a61a3f19c6b05f8ba181a2a7a506a Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Wed, 5 Feb 2025 19:45:35 +0400 Subject: [PATCH 4/7] revert un unintantional change --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4e15a9968..549ba73925 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -390,7 +390,7 @@ Do not use. Unpublished. Caused by an issue with the release script. #### Fixed - Don't apply babel-plugin-transform-runtime inside react-on-rails to work with babel 7. [PR 1136](https://github.com/shakacode/react_on_rails/pull/1136) by [Ryunosuke Sato](https://github.com/tricknotes). - Add support for webpacker 4 prereleases. [PR 1134](https://github.com/shakacode/react_on_rails/pull/1134) by [Judahmeek](https://github.com/Judahmeek)) -CHANGELOG + ### [11.1.2] - 2018-08-18 #### Fixed From a59fbd1310a8fd69bed5a9927e71b388ac096d41 Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Wed, 5 Feb 2025 19:55:03 +0400 Subject: [PATCH 5/7] extract FULL_TEXT_ERRORS check to Utils.full_text_errors_enabled? --- lib/react_on_rails/prerender_error.rb | 2 +- lib/react_on_rails/utils.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/react_on_rails/prerender_error.rb b/lib/react_on_rails/prerender_error.rb index b7a98ea2ad..2ec5f28d72 100644 --- a/lib/react_on_rails/prerender_error.rb +++ b/lib/react_on_rails/prerender_error.rb @@ -57,7 +57,7 @@ def calc_message(component_name, console_messages, err, js_code, props) MSG - backtrace = if ENV["FULL_TEXT_ERRORS"] == "true" + backtrace = if Utils.full_text_errors_enabled? err.backtrace.join("\n") else "#{err.backtrace.first(15).join("\n")}\n" + diff --git a/lib/react_on_rails/utils.rb b/lib/react_on_rails/utils.rb index e849d15283..c2b0b48766 100644 --- a/lib/react_on_rails/utils.rb +++ b/lib/react_on_rails/utils.rb @@ -185,10 +185,14 @@ def self.react_on_rails_pro_version end end + def self.full_text_errors_enabled? + ENV["FULL_TEXT_ERRORS"] == "true" + end + def self.smart_trim(str, max_length = 1000) # From https://stackoverflow.com/a/831583/1009332 str = str.to_s - return str if ENV["FULL_TEXT_ERRORS"] == "true" + return str if full_text_errors_enabled? return str unless str.present? && max_length >= 1 return str if str.length <= max_length From 3148f4163a08c0422b5173429a2964daa6d1d6fd Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Fri, 7 Feb 2025 14:50:27 +0400 Subject: [PATCH 6/7] Use Rails.backtrace_cleaner --- CHANGELOG.md | 2 +- lib/react_on_rails/prerender_error.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 549ba73925..4737eefe5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac Changes since the last non-beta release. #### Fixed - - Fix obscure errors by introducing FULL_TEXT_ERRORS [PR 1695](https://github.com/shakacode/react_on_rails/pull/1695) by [Romex91](https://github.com/Romex91). +- Fix obscure errors by introducing FULL_TEXT_ERRORS [PR 1695](https://github.com/shakacode/react_on_rails/pull/1695) by [Romex91](https://github.com/Romex91). ### [14.1.1] - 2025-01-15 diff --git a/lib/react_on_rails/prerender_error.rb b/lib/react_on_rails/prerender_error.rb index 2ec5f28d72..096fae6db6 100644 --- a/lib/react_on_rails/prerender_error.rb +++ b/lib/react_on_rails/prerender_error.rb @@ -60,7 +60,7 @@ def calc_message(component_name, console_messages, err, js_code, props) backtrace = if Utils.full_text_errors_enabled? err.backtrace.join("\n") else - "#{err.backtrace.first(15).join("\n")}\n" + + "#{Rails.backtrace_cleaner.clean(err.backtrace).join("\n")}\n" + Rainbow("The rest of the backtrace is hidden. " \ "To see the full backtrace, set FULL_TEXT_ERRORS=true.").red end From b90f1a2978a2f78521c832967330038b9266d27e Mon Sep 17 00:00:00 2001 From: Roman Kuksin Date: Mon, 10 Feb 2025 09:56:45 +0400 Subject: [PATCH 7/7] fix rspec --- spec/react_on_rails/prender_error_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/react_on_rails/prender_error_spec.rb b/spec/react_on_rails/prender_error_spec.rb index 08c77c2203..05503830ba 100644 --- a/spec/react_on_rails/prender_error_spec.rb +++ b/spec/react_on_rails/prender_error_spec.rb @@ -65,7 +65,9 @@ module ReactOnRails it "shows truncated backtrace with notice" do message = expected_error.message expect(message).to include(err.inspect) - expect(message).to include(err.backtrace.first(15).join("\n")) + expect(message).to include( + "spec/react_on_rails/prender_error_spec.rb:20:in `block (2 levels) in '" + ) expect(message).to include("The rest of the backtrace is hidden") end end