Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bd1badb

Browse files
committedNov 16, 2017
Add tests and update sample
1 parent 53ae074 commit bd1badb

File tree

5 files changed

+86
-23
lines changed

5 files changed

+86
-23
lines changed
 

Diff for: ‎src/React.Core/IReactSiteConfiguration.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) 2014-Present, Facebook, Inc.
33
* All rights reserved.
44
*
@@ -178,12 +178,20 @@ public interface IReactSiteConfiguration
178178
/// <summary>
179179
/// Disables server-side rendering. This is useful when debugging your scripts.
180180
/// </summary>
181-
IReactSiteConfiguration DisableServerSideRendering();
182-
181+
IReactSiteConfiguration DisableServerSideRendering();
182+
183183
/// <summary>
184-
/// Handle an exception caught during server-render of a component.
184+
/// An exception handler which will be called if a render exception is thrown.
185185
/// If unset, unhandled exceptions will be thrown for all component renders.
186186
/// </summary>
187-
Action<Exception> HandleRenderException { get; set; }
187+
Action<Exception> ExceptionHandler { get; set; }
188+
189+
/// <summary>
190+
/// Sets an exception handler which will be called if a render exception is thrown.
191+
/// If unset, unhandled exceptions will be thrown for all component renders.
192+
/// </summary>
193+
/// <param name="handler"></param>
194+
/// <returns></returns>
195+
IReactSiteConfiguration SetExceptionHandler(Action<Exception> handler);
188196
}
189197
}

Diff for: ‎src/React.Core/ReactComponent.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) 2014-Present, Facebook, Inc.
33
* All rights reserved.
44
*
@@ -132,15 +132,15 @@ public virtual string RenderHtml(bool renderContainerOnly = false, bool renderSe
132132
}
133133
catch (JsRuntimeException ex)
134134
{
135-
if (_configuration.HandleRenderException == null) {
135+
if (_configuration.ExceptionHandler == null) {
136136
throw new ReactServerRenderingException(string.Format(
137137
"Error while rendering \"{0}\" to \"{2}\": {1}",
138138
ComponentName,
139139
ex.Message,
140140
ContainerId
141141
));
142142
}
143-
_configuration.HandleRenderException(ex);
143+
_configuration.ExceptionHandler(ex);
144144
}
145145
}
146146

Diff for: ‎src/React.Core/ReactSiteConfiguration.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) 2014-Present, Facebook, Inc.
33
* All rights reserved.
44
*
@@ -306,6 +306,17 @@ public IReactSiteConfiguration DisableServerSideRendering()
306306
/// Handle an exception caught during server-render of a component.
307307
/// If unset, unhandled exceptions will be thrown for all component renders.
308308
/// </summary>
309-
public Action<Exception> HandleRenderException { get; set; }
309+
public Action<Exception> ExceptionHandler { get; set; }
310+
311+
/// <summary>
312+
///
313+
/// </summary>
314+
/// <param name="handler"></param>
315+
/// <returns></returns>
316+
public IReactSiteConfiguration SetExceptionHandler(Action<Exception> handler)
317+
{
318+
ExceptionHandler = handler;
319+
return this;
320+
}
310321
}
311322
}

Diff for: ‎src/React.Sample.CoreMvc/Startup.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ namespace React.Sample.CoreMvc
2020
{
2121
public class Startup
2222
{
23-
public Startup(IHostingEnvironment env)
24-
{
25-
// Setup configuration sources.
23+
public Startup(IHostingEnvironment env, ILogger<Startup> logger)
24+
{
25+
// Setup configuration sources.
2626
var builder = new ConfigurationBuilder().AddEnvironmentVariables();
27-
27+
Logger = logger;
2828
Configuration = builder.Build();
2929
}
3030

31-
public IConfiguration Configuration { get; set; }
31+
public IConfiguration Configuration { get; set; }
32+
public ILogger<Startup> Logger { get; set; }
3233

3334
// This method gets called by the runtime.
3435
public IServiceProvider ConfigureServices(IServiceCollection services)
@@ -69,8 +70,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6970
{
7071
config
7172
.SetReuseJavaScriptEngines(true)
72-
.AddScript("~/js/Sample.jsx")
73-
.SetUseDebugReact(true);
73+
.AddScript("~/js/Sample.jsx")
74+
.SetExceptionHandler(ex =>
75+
{
76+
Logger.LogError("React component exception thrown!" + ex.ToString());
77+
})
78+
.SetUseDebugReact(true);
7479
});
7580

7681
// Add static files to the request pipeline.

Diff for: ‎tests/React.Tests/Core/ReactComponentTest.cs

+45-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
/*
1+
/*
22
* Copyright (c) 2014-Present, Facebook, Inc.
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
9-
8+
*/
9+
10+
using System;
11+
using JavaScriptEngineSwitcher.Core;
1012
using Moq;
11-
using Xunit;
1213
using React.Exceptions;
13-
14+
using Xunit;
15+
1416
namespace React.Tests.Core
1517
{
1618
public class ReactComponentTest
@@ -190,7 +192,44 @@ public void GeneratesContainerIdIfNotProvided()
190192

191193
var component = new ReactComponent(environment.Object, config.Object, "Foo", null);
192194
Assert.StartsWith("react_", component.ContainerId);
193-
}
195+
}
196+
197+
[Fact]
198+
public void ExceptionThrownIsHandled()
199+
{
200+
var environment = new Mock<IReactEnvironment>();
201+
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
202+
environment.Setup(x => x.Execute<string>(@"ReactDOMServer.renderToString(React.createElement(Foo, {""hello"":""World""}))"))
203+
.Throws(new JsRuntimeException("'undefined' is not an object"));
204+
205+
var config = new Mock<IReactSiteConfiguration>();
206+
config.Setup(x => x.UseServerSideRendering).Returns(true);
194207

208+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
209+
{
210+
Props = new { hello = "World" }
211+
};
212+
213+
// Default behavior
214+
bool exceptionCaught = false;
215+
try
216+
{
217+
component.RenderHtml();
218+
}
219+
catch (ReactServerRenderingException)
220+
{
221+
exceptionCaught = true;
222+
}
223+
224+
Assert.True(exceptionCaught);
225+
226+
// Custom exception handler set
227+
Exception caughtException = null;
228+
config.Setup(x => x.ExceptionHandler).Returns(ex => caughtException = ex);
229+
230+
var result = component.RenderHtml();
231+
Assert.Equal(@"<div id=""container""></div>", result);
232+
Assert.NotNull(caughtException);
233+
}
195234
}
196235
}

0 commit comments

Comments
 (0)
Please sign in to comment.