Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/react_on_rails/prerender_error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "rainbow"

# rubocop:disable: Layout/IndentHeredoc
module ReactOnRails
class PrerenderError < ::ReactOnRails::Error
Expand Down Expand Up @@ -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 Utils.full_text_errors_enabled?
err.backtrace.join("\n")
else
"#{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
else
backtrace = nil
end
Expand Down
9 changes: 8 additions & 1 deletion lib/react_on_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,9 +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 full_text_errors_enabled?
return str unless str.present? && max_length >= 1
return str if str.length <= max_length

Expand Down
27 changes: 27 additions & 0 deletions spec/react_on_rails/prender_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,32 @@ 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(
"spec/react_on_rails/prender_error_spec.rb:20:in `block (2 levels) in <module:ReactOnRails>'"
)
expect(message).to include("The rest of the backtrace is hidden")
end
end
end
end
end
59 changes: 38 additions & 21 deletions spec/react_on_rails/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading