Skip to content

Commit 6c70ee8

Browse files
author
Robert Mosolgo
authored
Merge pull request #704 from reactjs/error-handling
feat(ujs) show errors when component can't be loaded
2 parents 7d0ac32 + 5ae4ebb commit 6c70ee8

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

lib/assets/javascripts/react_ujs.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,14 @@ module.exports = function(reqctx) {
185185
try {
186186
// `require` will raise an error if this className isn't found:
187187
component = fromCtx(className)
188-
} catch (err) {
188+
} catch (firstErr) {
189189
// fallback to global:
190-
component = fromGlobal(className)
190+
try {
191+
component = fromGlobal(className)
192+
} catch (secondErr) {
193+
console.error(firstErr)
194+
console.error(secondErr)
195+
}
191196
}
192197
return component
193198
}

react_ujs/dist/react_ujs.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,14 @@ module.exports = function(reqctx) {
185185
try {
186186
// `require` will raise an error if this className isn't found:
187187
component = fromCtx(className)
188-
} catch (err) {
188+
} catch (firstErr) {
189189
// fallback to global:
190-
component = fromGlobal(className)
190+
try {
191+
component = fromGlobal(className)
192+
} catch (secondErr) {
193+
console.error(firstErr)
194+
console.error(secondErr)
195+
}
191196
}
192197
return component
193198
}

react_ujs/src/getConstructor/fromRequireContextWithGlobalFallback.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ module.exports = function(reqctx) {
1111
try {
1212
// `require` will raise an error if this className isn't found:
1313
component = fromCtx(className)
14-
} catch (err) {
14+
} catch (firstErr) {
1515
// fallback to global:
16-
component = fromGlobal(className)
16+
try {
17+
component = fromGlobal(className)
18+
} catch (secondErr) {
19+
console.error(firstErr)
20+
console.error(secondErr)
21+
}
1722
}
1823
return component
1924
}

test/react/server_rendering/bundle_renderer_test.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ class BundleRendererTest < ActiveSupport::TestCase
6868
@renderer.render("NonExistentComponent", {}, nil)
6969
end
7070

71-
assert_match(/ReferenceError/, err.to_s)
71+
if WebpackerHelpers.available?
72+
# require() failed:
73+
assert_match(/Invariant Violation: Element type is invalid: expected a string/, err.to_s)
74+
else
75+
# eval() failed:
76+
assert_match(/ReferenceError/, err.to_s)
77+
end
7278
assert_match(/NonExistentComponent/, err.to_s, "it names the component")
7379

7480
assert_match(/\n/, err.to_s, "it includes the multi-line backtrace")

0 commit comments

Comments
 (0)