Skip to content

Commit 923525d

Browse files
authored
Fix obscure errors by introducing FULL_TEXT_ERRORS (#1695)
* Fix obscure errors by introducing FULL_TEXT_ERRORS * add rspec tests * update changelog * revert un unintantional change * extract FULL_TEXT_ERRORS check to Utils.full_text_errors_enabled? * Use Rails.backtrace_cleaner * fix rspec
1 parent 90033e7 commit 923525d

File tree

5 files changed

+86
-24
lines changed

5 files changed

+86
-24
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
1818
### [Unreleased]
1919
Changes since the last non-beta release.
2020

21+
#### Fixed
22+
- 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).
23+
2124
### [14.1.1] - 2025-01-15
2225

2326
#### Fixed

lib/react_on_rails/prerender_error.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "rainbow"
4+
35
# rubocop:disable: Layout/IndentHeredoc
46
module ReactOnRails
57
class PrerenderError < ::ReactOnRails::Error
@@ -51,11 +53,17 @@ def calc_message(component_name, console_messages, err, js_code, props)
5153
message << <<~MSG
5254
Encountered error:
5355
54-
#{err}
56+
#{err.inspect}
5557
5658
MSG
5759

58-
backtrace = err.backtrace.first(15).join("\n")
60+
backtrace = if Utils.full_text_errors_enabled?
61+
err.backtrace.join("\n")
62+
else
63+
"#{Rails.backtrace_cleaner.clean(err.backtrace).join("\n")}\n" +
64+
Rainbow("The rest of the backtrace is hidden. " \
65+
"To see the full backtrace, set FULL_TEXT_ERRORS=true.").red
66+
end
5967
else
6068
backtrace = nil
6169
end

lib/react_on_rails/utils.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
module ReactOnRails
1010
module Utils
11-
TRUNCATION_FILLER = "\n... TRUNCATED ...\n"
11+
TRUNCATION_FILLER = "\n... TRUNCATED #{
12+
Rainbow('To see the full output, set FULL_TEXT_ERRORS=true.').red
13+
} ...\n".freeze
1214

1315
# https://forum.shakacode.com/t/yak-of-the-week-ruby-2-4-pathname-empty-changed-to-look-at-file-size/901
1416
# return object if truthy, else return nil
@@ -183,9 +185,14 @@ def self.react_on_rails_pro_version
183185
end
184186
end
185187

188+
def self.full_text_errors_enabled?
189+
ENV["FULL_TEXT_ERRORS"] == "true"
190+
end
191+
186192
def self.smart_trim(str, max_length = 1000)
187193
# From https://stackoverflow.com/a/831583/1009332
188194
str = str.to_s
195+
return str if full_text_errors_enabled?
189196
return str unless str.present? && max_length >= 1
190197
return str if str.length <= max_length
191198

spec/react_on_rails/prender_error_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,32 @@ module ReactOnRails
4545
expect(expected_error.raven_context).to eq(expected_error_info)
4646
end
4747
end
48+
49+
describe "error message formatting" do
50+
context "when FULL_TEXT_ERRORS is true" do
51+
before { ENV["FULL_TEXT_ERRORS"] = "true" }
52+
after { ENV["FULL_TEXT_ERRORS"] = nil }
53+
54+
it "shows the full backtrace" do
55+
message = expected_error.message
56+
expect(message).to include(err.inspect)
57+
expect(message).to include(err.backtrace.join("\n"))
58+
expect(message).not_to include("The rest of the backtrace is hidden")
59+
end
60+
end
61+
62+
context "when FULL_TEXT_ERRORS is not set" do
63+
before { ENV["FULL_TEXT_ERRORS"] = nil }
64+
65+
it "shows truncated backtrace with notice" do
66+
message = expected_error.message
67+
expect(message).to include(err.inspect)
68+
expect(message).to include(
69+
"spec/react_on_rails/prender_error_spec.rb:20:in `block (2 levels) in <module:ReactOnRails>'"
70+
)
71+
expect(message).to include("The rest of the backtrace is hidden")
72+
end
73+
end
74+
end
4875
end
4976
end

spec/react_on_rails/utils_spec.rb

+38-21
Original file line numberDiff line numberDiff line change
@@ -310,30 +310,47 @@ module ReactOnRails
310310
end
311311

312312
describe ".smart_trim" do
313-
it "trims smartly" do
314-
s = "1234567890"
315-
316-
expect(described_class.smart_trim(s, -1)).to eq("1234567890")
317-
expect(described_class.smart_trim(s, 0)).to eq("1234567890")
318-
expect(described_class.smart_trim(s, 1)).to eq("1#{Utils::TRUNCATION_FILLER}")
319-
expect(described_class.smart_trim(s, 2)).to eq("1#{Utils::TRUNCATION_FILLER}0")
320-
expect(described_class.smart_trim(s, 3)).to eq("1#{Utils::TRUNCATION_FILLER}90")
321-
expect(described_class.smart_trim(s, 4)).to eq("12#{Utils::TRUNCATION_FILLER}90")
322-
expect(described_class.smart_trim(s, 5)).to eq("12#{Utils::TRUNCATION_FILLER}890")
323-
expect(described_class.smart_trim(s, 6)).to eq("123#{Utils::TRUNCATION_FILLER}890")
324-
expect(described_class.smart_trim(s, 7)).to eq("123#{Utils::TRUNCATION_FILLER}7890")
325-
expect(described_class.smart_trim(s, 8)).to eq("1234#{Utils::TRUNCATION_FILLER}7890")
326-
expect(described_class.smart_trim(s, 9)).to eq("1234#{Utils::TRUNCATION_FILLER}67890")
327-
expect(described_class.smart_trim(s, 10)).to eq("1234567890")
328-
expect(described_class.smart_trim(s, 11)).to eq("1234567890")
313+
let(:long_string) { "1234567890" }
314+
315+
context "when FULL_TEXT_ERRORS is true" do
316+
before { ENV["FULL_TEXT_ERRORS"] = "true" }
317+
after { ENV["FULL_TEXT_ERRORS"] = nil }
318+
319+
it "returns the full string regardless of length" do
320+
expect(described_class.smart_trim(long_string, 5)).to eq(long_string)
321+
end
322+
323+
it "handles a hash without trimming" do
324+
hash = { a: long_string }
325+
expect(described_class.smart_trim(hash, 5)).to eq(hash.to_s)
326+
end
329327
end
330328

331-
it "trims handles a hash" do
332-
s = { a: "1234567890" }
329+
context "when FULL_TEXT_ERRORS is not set" do
330+
before { ENV["FULL_TEXT_ERRORS"] = nil }
331+
332+
it "trims smartly" do
333+
expect(described_class.smart_trim(long_string, -1)).to eq("1234567890")
334+
expect(described_class.smart_trim(long_string, 0)).to eq("1234567890")
335+
expect(described_class.smart_trim(long_string, 1)).to eq("1#{Utils::TRUNCATION_FILLER}")
336+
expect(described_class.smart_trim(long_string, 2)).to eq("1#{Utils::TRUNCATION_FILLER}0")
337+
expect(described_class.smart_trim(long_string, 3)).to eq("1#{Utils::TRUNCATION_FILLER}90")
338+
expect(described_class.smart_trim(long_string, 4)).to eq("12#{Utils::TRUNCATION_FILLER}90")
339+
expect(described_class.smart_trim(long_string, 5)).to eq("12#{Utils::TRUNCATION_FILLER}890")
340+
expect(described_class.smart_trim(long_string, 6)).to eq("123#{Utils::TRUNCATION_FILLER}890")
341+
expect(described_class.smart_trim(long_string, 7)).to eq("123#{Utils::TRUNCATION_FILLER}7890")
342+
expect(described_class.smart_trim(long_string, 8)).to eq("1234#{Utils::TRUNCATION_FILLER}7890")
343+
expect(described_class.smart_trim(long_string, 9)).to eq("1234#{Utils::TRUNCATION_FILLER}67890")
344+
expect(described_class.smart_trim(long_string, 10)).to eq("1234567890")
345+
expect(described_class.smart_trim(long_string, 11)).to eq("1234567890")
346+
end
333347

334-
expect(described_class.smart_trim(s, 9)).to eq(
335-
"{:a=#{Utils::TRUNCATION_FILLER}890\"}"
336-
)
348+
it "trims handles a hash" do
349+
s = { a: "1234567890" }
350+
expect(described_class.smart_trim(s, 9)).to eq(
351+
"{:a=#{Utils::TRUNCATION_FILLER}890\"}"
352+
)
353+
end
337354
end
338355
end
339356

0 commit comments

Comments
 (0)