Skip to content

Commit 33deebb

Browse files
dustinsoftwareDaniel15
authored andcommittedJul 1, 2018
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 5cbcd22 commit 33deebb

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
 

‎src/React.Core/JavaScriptEngineFactory.cs

+1-1
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

+22
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ public void ShouldThrowIfReactVersionNotLoaded()
164164
});
165165
}
166166

167+
[Fact]
168+
public void ShouldThrowScriptErrorIfReactFails()
169+
{
170+
var config = new Mock<IReactSiteConfiguration>();
171+
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
172+
config.Setup(x => x.LoadReact).Returns(false);
173+
var fileSystem = new Mock<IFileSystem>();
174+
fileSystem.Setup(x => x.ReadAsString("foo.js")).Returns("FAIL PLZ");
175+
176+
var jsEngine = new Mock<IJsEngine>();
177+
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);
178+
jsEngine.Setup(x => x.Execute("FAIL PLZ")).Throws(new JsRuntimeException("Fail")
179+
{
180+
LineNumber = 42,
181+
ColumnNumber = 911,
182+
});
183+
var factory = CreateFactory(config, fileSystem, () => jsEngine.Object);
184+
185+
var ex = Assert.Throws<ReactScriptLoadException>(() => factory.GetEngineForCurrentThread());
186+
Assert.Equal("Error while loading \"foo.js\": Fail\r\nLine: 42\r\nColumn: 911", ex.Message);
187+
}
188+
167189
[Fact]
168190
public void ShouldCatchErrorsWhileLoadingScripts()
169191
{

0 commit comments

Comments
 (0)
Please sign in to comment.