Skip to content

Commit 92c6da3

Browse files
justin808claude
andcommitted
docs: Enhance error handling documentation and test coverage
Address code review suggestions: - Expand module documentation to explain graceful degradation philosophy in detail, including specific failure scenarios (network, permissions, package_json gem states) - Add comprehensive tests for graceful degradation: * Verify setup_js_dependencies completes without raising when all package operations fail * Verify completion when install fails but add succeeds * Ensure warnings are generated but no errors that crash generators - Improve add_package method documentation to clarify that exact: true flag ensures version pinning aligns with gem version, preventing mismatches between Ruby gem and NPM package These changes improve code documentation and test coverage without changing functionality. All 38 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent de05f09 commit 92c6da3

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

lib/generators/react_on_rails/js_dependency_manager.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ module Generators
2929
# installs what's actually needed from package.json. This prevents edge cases
3030
# where package.json was modified but dependencies weren't installed.
3131
#
32-
# == Error Handling
33-
# All dependency addition methods use a tolerant error handling approach:
34-
# - Return false on failure instead of raising exceptions
35-
# - Catch StandardError and add warnings to GeneratorMessages
36-
# - Provide clear manual installation instructions in warnings
37-
# This provides better UX - the generator completes successfully even if
38-
# dependency installation fails (e.g., network issues), and users can
39-
# manually install dependencies afterward.
32+
# == Error Handling Philosophy
33+
# All dependency addition methods use a graceful degradation approach:
34+
# - Methods return false on failure instead of raising exceptions
35+
# - StandardError is caught at the lowest level (add_package) and higher levels (add_*_dependencies)
36+
# - Failures trigger user-facing warnings via GeneratorMessages
37+
# - Warnings provide clear manual installation instructions
38+
#
39+
# This ensures the generator ALWAYS completes successfully, even when:
40+
# - Network connectivity issues prevent package downloads
41+
# - Package manager (npm/yarn/pnpm) has permission errors
42+
# - package_json gem encounters unexpected states
43+
#
44+
# Users can manually run package installation commands after generator completion.
45+
# This is preferable to generator crashes that leave Rails apps in incomplete states.
4046
#
4147
# == Usage
4248
# Include this module in generator classes and call setup_js_dependencies
@@ -256,6 +262,9 @@ def add_dev_dependencies
256262
# with version-specific handling (react-on-rails@VERSION).
257263
# For batch operations, use add_packages instead.
258264
#
265+
# The exact: true flag ensures version pinning aligns with the gem version,
266+
# preventing version mismatches between the Ruby gem and NPM package.
267+
#
259268
# @param package [String] Package specifier (e.g., "react-on-rails@16.0.0")
260269
# @param dev [Boolean] Whether to add as dev dependency
261270
# @return [Boolean] true if successful, false otherwise

spec/react_on_rails/generators/js_dependency_manager_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,31 @@ def errors
357357
expect(warning.to_s).to include("manually")
358358
end
359359
end
360+
361+
describe "graceful degradation" do
362+
it "setup_js_dependencies completes successfully even when all package operations fail" do
363+
# Simulate complete package installation failure
364+
instance.add_npm_dependencies_result = false
365+
instance.package_json = nil
366+
367+
# This should not raise any exceptions
368+
expect { instance.send(:setup_js_dependencies) }.not_to raise_error
369+
370+
# Should have generated warnings for failures
371+
expect(warnings.size).to be > 0
372+
# But no errors that would crash the generator
373+
expect(errors.size).to eq(0)
374+
end
375+
376+
it "setup_js_dependencies completes when install fails but add succeeds" do
377+
instance.add_npm_dependencies_result = true
378+
allow(mock_manager).to receive(:install).and_raise(StandardError, "Network timeout")
379+
380+
# Should not raise despite install failure
381+
expect { instance.send(:setup_js_dependencies) }.not_to raise_error
382+
383+
# Should have warning about install failure
384+
expect(warnings.any? { |w| w.to_s.include?("installation failed") }).to be(true)
385+
end
386+
end
360387
end

0 commit comments

Comments
 (0)