Skip to content

Commit c10c0d4

Browse files
Copilotilonatommy
andcommitted
Change warning to throwing exception for NotFound/NotFoundPage validation
Co-authored-by: ilonatommy <[email protected]>
1 parent aa54562 commit c10c0d4

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/Components/Components/src/Routing/Router.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public async Task SetParametersAsync(ParameterView parameters)
147147
#pragma warning disable CS0618 // Type or member is obsolete
148148
if (NotFound != null)
149149
{
150-
Log.BothNotFoundParametersSet(_logger);
150+
throw new InvalidOperationException("Both NotFound and NotFoundPage parameters are set on Router component. NotFoundPage is preferred and NotFound will be deprecated. Consider using only NotFoundPage.");
151151
}
152152
#pragma warning restore CS0618 // Type or member is obsolete
153153
if (!typeof(IComponent).IsAssignableFrom(NotFoundPage))
@@ -451,8 +451,5 @@ private static partial class Log
451451
[LoggerMessage(4, LogLevel.Debug, $"Displaying {nameof(NotFound)} on request", EventName = "DisplayingNotFoundOnRequest")]
452452
internal static partial void DisplayingNotFound(ILogger logger);
453453
#pragma warning restore CS0618 // Type or member is obsolete
454-
455-
[LoggerMessage(5, LogLevel.Warning, "Both NotFound and NotFoundPage parameters are set on Router component. NotFoundPage is preferred and NotFound will be deprecated. Consider using only NotFoundPage.", EventName = "BothNotFoundParametersSet")]
456-
internal static partial void BothNotFoundParametersSet(ILogger logger);
457454
}
458455
}

src/Components/Components/test/Routing/RouterTest.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,11 @@ await _renderer.Dispatcher.InvokeAsync(() =>
268268
}
269269

270270
[Fact]
271-
public async Task LogsWarningWhenBothNotFoundAndNotFoundPageAreSet()
271+
public async Task ThrowsExceptionWhenBothNotFoundAndNotFoundPageAreSet()
272272
{
273273
// Arrange
274-
var logger = new TestLogger<Router>();
275274
var services = new ServiceCollection();
276-
services.AddSingleton<ILoggerFactory>(new TestLoggerFactory(logger));
275+
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
277276
services.AddSingleton<NavigationManager>(_navigationManager);
278277
services.AddSingleton<INavigationInterception, TestNavigationInterception>();
279278
services.AddSingleton<IScrollToLocationHash, TestScrollToLocationHash>();
@@ -293,15 +292,13 @@ public async Task LogsWarningWhenBothNotFoundAndNotFoundPageAreSet()
293292
{ nameof(Router.NotFoundPage), typeof(NotFoundTestComponent) }
294293
};
295294

296-
// Act
297-
await renderer.Dispatcher.InvokeAsync(() =>
298-
router.SetParametersAsync(ParameterView.FromDictionary(parameters)));
295+
// Act & Assert
296+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
297+
await renderer.Dispatcher.InvokeAsync(() =>
298+
router.SetParametersAsync(ParameterView.FromDictionary(parameters))));
299299

300-
// Assert
301-
var warningLogs = logger.LogEntries.Where(entry => entry.LogLevel == LogLevel.Warning).ToList();
302-
Assert.Single(warningLogs);
303-
Assert.Contains("Both NotFound and NotFoundPage parameters are set on Router component", warningLogs[0].Message);
304-
Assert.Contains("NotFoundPage is preferred and NotFound will be deprecated", warningLogs[0].Message);
300+
Assert.Contains("Both NotFound and NotFoundPage parameters are set on Router component", exception.Message);
301+
Assert.Contains("NotFoundPage is preferred and NotFound will be deprecated", exception.Message);
305302
}
306303

307304
internal class TestNavigationManager : NavigationManager

0 commit comments

Comments
 (0)