Skip to content

Commit 0c4ce2e

Browse files
Fix unhelpful error message if React can't be loaded
If React fails to load from a user-provided bundle, the script load exception would be hidden from the user, because the exception is stored to a field and then rethrown later. EnsureReactLoaded was getting called before the real exception was thrown, which threw a new (unhelpful) exception.
1 parent 79ca155 commit 0c4ce2e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/React.Core/JavaScriptEngineFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected virtual void InitialiseEngine(IJsEngine engine)
135135
}
136136

137137
LoadUserScripts(engine);
138-
if (!_config.LoadReact)
138+
if (!_config.LoadReact && _scriptLoadException == null)
139139
{
140140
// We expect to user to have loaded their own version of React in the scripts that
141141
// were loaded above, let's ensure that's the case.

tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ public void ShouldThrowIfReactVersionNotLoaded()
155155
});
156156
}
157157

158+
[Fact]
159+
public void ShouldThrowScriptErrorIfReactFails()
160+
{
161+
var config = new Mock<IReactSiteConfiguration>();
162+
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
163+
config.Setup(x => x.LoadReact).Returns(false);
164+
var fileSystem = new Mock<IFileSystem>();
165+
fileSystem.Setup(x => x.ReadAsString("foo.js")).Returns("FAIL PLZ");
166+
167+
var jsEngine = new Mock<IJsEngine>();
168+
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);
169+
jsEngine.Setup(x => x.Execute("FAIL PLZ")).Throws(new JsRuntimeException("Fail")
170+
{
171+
LineNumber = 42,
172+
ColumnNumber = 911,
173+
});
174+
var factory = CreateFactory(config, fileSystem, () => jsEngine.Object);
175+
176+
var ex = Assert.Throws<ReactScriptLoadException>(() => factory.GetEngineForCurrentThread());
177+
Assert.Equal("Error while loading \"foo.js\": Fail\r\nLine: 42\r\nColumn: 911", ex.Message);
178+
}
179+
158180
[Fact]
159181
public void ShouldCatchErrorsWhileLoadingScripts()
160182
{

0 commit comments

Comments
 (0)