Skip to content
5 changes: 4 additions & 1 deletion 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 Expand Up @@ -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
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 ENV["FULL_TEXT_ERRORS"] == "true"
err.backtrace.join("\n")
else
"#{err.backtrace.first(15).join("\n")}\n" +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this at all? And why first 15 instead of the normal Rails backtrace cleaning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you suggest removing the ENV variable and using Rails.backtrace_cleaner?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave the variable and only use Rails.backtrace_cleaner in the else case.

Copy link
Collaborator

@alexeyr-ci alexeyr-ci Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative is just to always show the full stack trace, but I don't know why we'd ever want first N in particular unless N is 1 or 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
5 changes: 4 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 @@ -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

Expand Down
25 changes: 25 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,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
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