Skip to content

Commit 9e809f0

Browse files
authored
doc(Error): update sample code reduce error log (#5174)
* doc: 忽略取消异常 * doc: 更新 Menu 示例代码 * test: 增加单元测试
1 parent 6af62a5 commit 9e809f0

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/BootstrapBlazor.Server/Components/Samples/Menus.razor

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<DemoBlock Title="@Localizer["MenusTopBarTitle"]"
1414
Introduction="@Localizer["MenusTopBarIntro"]"
1515
Name="TopBar">
16-
<Menu Items="@Items" DisableNavigation="true" OnClick="@OnClickMenu" />
17-
<ConsoleLogger @ref="Logger" />
16+
<Menu Items="@Items" DisableNavigation="true" OnClick="@OnClickMenu"></Menu>
17+
<ConsoleLogger @ref="Logger"></ConsoleLogger>
1818
</DemoBlock>
1919

2020
<DemoBlock Title="@Localizer["MenusBottomBarTitle"]"
@@ -25,21 +25,21 @@
2525
</Tips>
2626
<div style="height: 120px; position: relative;">
2727
<div>@ClickedMenuItemText</div>
28-
<Menu Items="@BottomItems" DisableNavigation="true" OnClick="@OnClickBottomMenu" IsBottom="true" />
28+
<Menu Items="@BottomItems" DisableNavigation="true" OnClick="@OnClickBottomMenu" IsBottom="true"></Menu>
2929
</div>
3030
</DemoBlock>
3131

3232
<DemoBlock Title="@Localizer["MenusIconTopTitle"]"
3333
Introduction="@Localizer["MenusIconTopIntro"]"
3434
Name="IconTop">
35-
<Menu Items="@IconItems" DisableNavigation="true" />
35+
<Menu Items="@IconItems" DisableNavigation="true"></Menu>
3636
</DemoBlock>
3737

3838
<DemoBlock Title="@Localizer["MenusLeftRightLayoutTitle"]"
3939
Introduction="@Localizer["MenusLeftRightLayoutIntro"]"
4040
Name="LeftRightLayout">
41-
<Menu Items="@SideMenuItems" DisableNavigation="true" IsVertical="true" OnClick="@OnClickSideMenu" style="width:220px; border-right: 1px solid #e6e6e6; padding-right: 4px;" />
42-
<ConsoleLogger @ref="SideLogger" />
41+
<Menu Items="@SideMenuItems" DisableNavigation="true" IsVertical="true" OnClick="@OnClickSideMenu" style="width:220px; border-right: 1px solid #e6e6e6; padding-right: 4px;"></Menu>
42+
<ConsoleLogger @ref="SideLogger"></ConsoleLogger>
4343
</DemoBlock>
4444

4545
<DemoBlock Title="@Localizer["MenusIconLeftTitle"]"
@@ -51,7 +51,7 @@
5151
<DemoBlock Title="@Localizer["MenusAccordionTitle"]"
5252
Introduction="@Localizer["MenusAccordionIntro"]"
5353
Name="Accordion">
54-
<Menu Items="@IconSideMenuItems" DisableNavigation="true" IsVertical="true" IsAccordion="true" style="border-right: 1px solid #e6e6e6; width:220px;" />
54+
<Menu Items="@IconSideMenuItems" DisableNavigation="true" IsVertical="true" IsAccordion="true" style="border-right: 1px solid #e6e6e6; width:220px;"></Menu>
5555
</DemoBlock>
5656

5757
<DemoBlock Title="@Localizer["MenusClickShrinkTitle"]"
@@ -91,36 +91,34 @@
9191
<DemoBlock Title="@Localizer["MenusWidgetTitle"]"
9292
Introduction="@Localizer["MenusWidgetIntro"]"
9393
Name="Widget">
94-
<Menu Items="@WidgetIconSideMenuItems" DisableNavigation="true" IsVertical="true" style="border-right: 1px solid #e6e6e6; width:220px;" />
94+
<Menu Items="@WidgetIconSideMenuItems" DisableNavigation="true" IsVertical="true" style="border-right: 1px solid #e6e6e6; width:220px;"></Menu>
9595
</DemoBlock>
9696

9797
<DemoBlock Title="@Localizer["MenusCustomNodeTitle"]"
9898
Introduction="@Localizer["MenusCustomNodeIntro"]"
9999
Name="CustomNode">
100-
<p>@((MarkupString)Localizer["MenusCustomNodeDescription"].Value)</p>
101-
<Menu Items="@CollapsedIconSideMenuItems" DisableNavigation="true" IsVertical="true" style="border-right: 1px solid #e6e6e6; width:220px;" />
100+
<section ignore>@((MarkupString)Localizer["MenusCustomNodeDescription"].Value)</section>
101+
<Menu Items="@CollapsedIconSideMenuItems" DisableNavigation="true" IsVertical="true" style="border-right: 1px solid #e6e6e6; width:220px;"></Menu>
102102
</DemoBlock>
103103

104104
<DemoBlock Title="@Localizer["MenusDynamicTitle"]"
105105
Introduction="@Localizer["MenusDynamicIntro"]"
106106
Name="Dynamic">
107-
<Menu Items="@DynamicSideMenuItems" DisableNavigation="true" IsVertical="true" style="border-right: 1px solid #e6e6e6; width:220px;" class="mb-3" />
108-
<Button Text="@Localizer["MenusDynamicButton1Text"]" OnClick="UpdateMenu" />
109-
<Button Text="@Localizer["MenusDynamicButton2Text"]" OnClick="ResetMenu" />
107+
<Menu Items="@DynamicSideMenuItems" DisableNavigation="true" IsVertical="true" style="border-right: 1px solid #e6e6e6; width:220px;" class="mb-3"></Menu>
108+
<Button Text="@Localizer["MenusDynamicButton1Text"]" OnClick="UpdateMenu"></Button>
109+
<Button Text="@Localizer["MenusDynamicButton2Text"]" OnClick="ResetMenu"></Button>
110110
</DemoBlock>
111111

112112
<DemoBlock Title="@Localizer["MenusPartDisableTitle"]"
113113
Introduction="@Localizer["MenusPartDisableIntro"]"
114114
Name="PartDisable">
115-
<p>@((MarkupString)Localizer["MenusPartDisableDescription1"].Value)</p>
115+
<section ignore>@((MarkupString)Localizer["MenusPartDisableDescription1"].Value)</section>
116116

117-
<Menu Items="@DisabledMenuItems" DisableNavigation="true" OnClick="@OnClick2" />
118-
<p class="mt-3"><b>@Localizer["MenusPartDisableDescription2"]</b></p>
117+
<Menu Items="@DisabledMenuItems" DisableNavigation="true"></Menu>
118+
<section ignore class="mt-3"><b>@Localizer["MenusPartDisableDescription2"]</b></section>
119119
<div style="width:220px; border-right: 1px solid #e6e6e6;">
120-
<Menu Items="@DisabledMenuItems" DisableNavigation="true" IsVertical="true" OnClick="@OnClick2" />
120+
<Menu Items="@DisabledMenuItems" DisableNavigation="true" IsVertical="true"></Menu>
121121
</div>
122-
123-
<ConsoleLogger @ref="Logger" />
124122
</DemoBlock>
125123

126124
<AttributeTable Items="@GetAttributes()" />

src/BootstrapBlazor.Server/Components/Samples/Menus.razor.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ private async Task ResetMenu()
9595
DynamicSideMenuItems = await MenusDataGenerator.GetSideMenuItemsAsync(Localizer);
9696
}
9797

98-
private Task OnClick2(MenuItem item)
99-
{
100-
DisabledLogger.Log($"菜单点击项: {item.Text}");
101-
return Task.CompletedTask;
102-
}
103-
10498
private AttributeItem[] GetAttributes() =>
10599
[
106100
new()

src/BootstrapBlazor/Services/IPLocator/BaiduIpLocatorProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class BaiduIpLocatorProvider(IHttpClientFactory httpClientFactory, IOptio
2929
using var token = new CancellationTokenSource(3000);
3030
ret = await Fetch(url, _client, token.Token);
3131
}
32+
catch (TaskCanceledException) { }
3233
catch (Exception ex)
3334
{
3435
logger.LogError(ex, "Url: {url}", url);

test/UnitTest/Services/IpLocatorTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ public async Task Fetch_Error()
108108
var result = await provider.Locate("223.91.188.112");
109109
Assert.Null(result);
110110

111+
var cancelPprovider = new MockProviderFetchCancelError(factory, option, logger);
112+
result = await cancelPprovider.Locate("223.91.188.112");
113+
Assert.Null(result);
114+
111115
var configuration = Context.Services.GetRequiredService<IConfiguration>();
112116
var juHeLogger = Context.Services.GetRequiredService<ILogger<JuHeIpLocatorProvider>>();
113117
var juHeProvider = new MockProviderJuHeFetchError(factory, configuration, option, juHeLogger);
@@ -163,6 +167,11 @@ class MockProviderFetchError(IHttpClientFactory httpClientFactory, IOptions<Boot
163167
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token) => throw new InvalidOperationException();
164168
}
165169

170+
class MockProviderFetchCancelError(IHttpClientFactory httpClientFactory, IOptions<BootstrapBlazorOptions> option, ILogger<MockProviderFetchError> logger) : BaiduIpLocatorProvider(httpClientFactory, option, logger)
171+
{
172+
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token) => throw new TaskCanceledException();
173+
}
174+
166175
class MockProviderJuHeFetchError(IHttpClientFactory httpClientFactory, IConfiguration configuration, IOptions<BootstrapBlazorOptions> option, ILogger<JuHeIpLocatorProvider> logger) : JuHeIpLocatorProvider(httpClientFactory, configuration, option, logger)
167176
{
168177
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token) => throw new InvalidOperationException();

0 commit comments

Comments
 (0)