Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added smart_trim method for better logging #1118

Merged
merged 1 commit into from
Jul 23, 2018
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
9 changes: 6 additions & 3 deletions lib/react_on_rails/prerender_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ def calc_message(component_name, console_messages, err, js_code, props)
end
# rubocop:disable Layout/IndentHeredoc
message << <<-MSG
when prerendering #{component_name} with props (truncated): #{props.to_s[0, MAX_ERROR_SNIPPET_TO_LOG]}
js_code was:
#{js_code[0, MAX_ERROR_SNIPPET_TO_LOG]}
when prerendering #{component_name} with props: #{Utils.smart_trim(props, MAX_ERROR_SNIPPET_TO_LOG)}

code:

#{Utils.smart_trim(js_code, MAX_ERROR_SNIPPET_TO_LOG)}

MSG
# rubocop:enable Layout/IndentHeredoc

Expand Down
17 changes: 17 additions & 0 deletions lib/react_on_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

module ReactOnRails
module Utils
TRUNCATION_FILLER = "\n... TRUNCATED ...\n"

# 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
def self.truthy_presence(obj)
Expand Down Expand Up @@ -152,5 +154,20 @@ def self.gem_available?(name)
def self.react_on_rails_pro?
@react_on_rails_pro ||= gem_available?("react_on_rails_pro")
end

def self.smart_trim(str, max_length = 1000)
# From https://stackoverflow.com/a/831583/1009332
str = str.to_s
return str unless str.present? && max_length >= 1
return str if str.length <= max_length

return str[0, 1] + TRUNCATION_FILLER if max_length == 1

midpoint = (str.length / 2.0).ceil;
to_remove = str.length - max_length;
lstrip = (to_remove / 2.0).ceil;
rstrip = to_remove - lstrip;
str[0..(midpoint - lstrip - 1)] + TRUNCATION_FILLER + str[(midpoint + rstrip)..-1]
end
end
end
57 changes: 42 additions & 15 deletions spec/react_on_rails/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ module ReactOnRails
end
before do
allow(ReactOnRails).to receive_message_chain(:configuration, :generated_assets_dir)
.and_return("")
.and_return("")
allow(Webpacker).to receive_message_chain("dev_server.running?")
.and_return(false)
.and_return(false)
allow(Webpacker).to receive_message_chain("config.public_output_path")
.and_return(webpacker_public_output_path)
.and_return(webpacker_public_output_path)
allow(ReactOnRails::WebpackerUtils).to receive(:using_webpacker?).and_return(true)
end

Expand All @@ -39,8 +39,8 @@ module ReactOnRails
# [2] (pry) ReactOnRails::WebpackerUtils: 0> Webpacker.manifest.lookup("app-bundle.js")
# "/webpack/development/app-bundle-c1d2b6ab73dffa7d9c0e.js"
allow(Webpacker).to receive_message_chain("manifest.lookup!")
.with("webpack-bundle.js")
.and_return("/webpack/dev/webpack-bundle-0123456789abcdef.js")
.with("webpack-bundle.js")
.and_return("/webpack/dev/webpack-bundle-0123456789abcdef.js")
end

it { expect(subject).to eq("#{webpacker_public_output_path}/webpack-bundle-0123456789abcdef.js") }
Expand All @@ -58,14 +58,14 @@ module ReactOnRails
context "Without Webpacker enabled" do
before do
allow(ReactOnRails).to receive_message_chain(:configuration, :generated_assets_dir)
.and_return("public/webpack/dev")
.and_return("public/webpack/dev")
allow(ReactOnRails::WebpackerUtils).to receive(:using_webpacker?).and_return(false)
end

it {
expect(subject).to eq(File.expand_path(
File.join(Rails.root,
"public/webpack/dev/webpack-bundle.js")
File.join(Rails.root,
"public/webpack/dev/webpack-bundle.js")
))
}
end
Expand All @@ -76,17 +76,17 @@ module ReactOnRails
allow(Rails).to receive(:root).and_return(Pathname.new("."))
allow(ReactOnRails::WebpackerUtils).to receive(:using_webpacker?).and_return(true)
allow(Webpacker).to receive_message_chain("config.public_output_path")
.and_return(Pathname.new("public/webpack/development"))
.and_return(Pathname.new("public/webpack/development"))
end

context "With Webpacker enabled and server file not in manifest", :webpacker do
it "returns the unhashed server path" do
server_bundle_name = "server-bundle.js"
allow(ReactOnRails).to receive_message_chain("configuration.server_bundle_js_file")
.and_return(server_bundle_name)
.and_return(server_bundle_name)
allow(Webpacker).to receive_message_chain("manifest.lookup!")
.with(server_bundle_name)
.and_raise(Webpacker::Manifest::MissingEntryError)
.with(server_bundle_name)
.and_raise(Webpacker::Manifest::MissingEntryError)

path = Utils.server_bundle_js_file_path

Expand All @@ -97,10 +97,10 @@ module ReactOnRails
context "With Webpacker enabled and server file in the manifest", :webpacker do
it "returns the correct path hashed server path" do
allow(ReactOnRails).to receive_message_chain("configuration.server_bundle_js_file")
.and_return("webpack-bundle.js")
.and_return("webpack-bundle.js")
allow(Webpacker).to receive_message_chain("manifest.lookup!")
.with("webpack-bundle.js")
.and_return("webpack/development/webpack-bundle-123456.js")
.with("webpack-bundle.js")
.and_return("webpack/development/webpack-bundle-123456.js")

path = Utils.server_bundle_js_file_path

Expand Down Expand Up @@ -247,6 +247,33 @@ module ReactOnRails
end
end
end

describe ".smart_trim" do
it "trims smartly" do
s = '1234567890'

expect(Utils.smart_trim(s, -1)).to eq('1234567890')
expect(Utils.smart_trim(s, 0)).to eq('1234567890')
expect(Utils.smart_trim(s, 1)).to eq("1#{Utils::TRUNCATION_FILLER}")
expect(Utils.smart_trim(s, 2)).to eq("1#{Utils::TRUNCATION_FILLER}0")
expect(Utils.smart_trim(s, 3)).to eq("1#{Utils::TRUNCATION_FILLER}90")
expect(Utils.smart_trim(s, 4)).to eq("12#{Utils::TRUNCATION_FILLER}90")
expect(Utils.smart_trim(s, 5)).to eq("12#{Utils::TRUNCATION_FILLER}890")
expect(Utils.smart_trim(s, 6)).to eq("123#{Utils::TRUNCATION_FILLER}890")
expect(Utils.smart_trim(s, 7)).to eq("123#{Utils::TRUNCATION_FILLER}7890")
expect(Utils.smart_trim(s, 8)).to eq("1234#{Utils::TRUNCATION_FILLER}7890")
expect(Utils.smart_trim(s, 9)).to eq("1234#{Utils::TRUNCATION_FILLER}67890")
expect(Utils.smart_trim(s, 10)).to eq('1234567890')
expect(Utils.smart_trim(s, 11)).to eq('1234567890')
end

it "trims handles a hash" do
s = { a: '1234567890' }

expect(Utils.smart_trim(s, 9)).to eq(
"{:a=#{Utils::TRUNCATION_FILLER}890\"}")
end
end
end
end
# rubocop:enable Metrics/ModuleLength, Metrics/BlockLength