Skip to content

Commit

Permalink
ErrorBoundary added for page exception
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-atharva committed May 1, 2023
1 parent 7c272ff commit 10fabfe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
34 changes: 26 additions & 8 deletions src/Presentation/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,28 @@
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />*@
@if (SearchText == null && CategoryUrl == null)
{
<FeatureProducts />
}
else
{
<ProductList />
}
<ErrorBoundary @ref="errorBoundary">
<ChildContent>
@if (SearchText == null && CategoryUrl == null)
{
<FeatureProducts />
}
else
{
<ProductList />
}
</ChildContent>
<ErrorContent>
<p class="error-message">Uh oh! Something went wrong</p>
<button class="btn btn-warning" @onclick="ResetError">Reset</button>
</ErrorContent>
</ErrorBoundary>



@code {
private ErrorBoundary? errorBoundary;

[Parameter]
public string CategoryUrl { get; set; } = null;

Expand All @@ -32,6 +43,8 @@ else

protected override async Task OnParametersSetAsync()
{
errorBoundary?.Recover();

if (SearchText != null)
{
await ProductService.SearchProducts(SearchText, Page);
Expand All @@ -42,4 +55,9 @@ else
}

}

private void ResetError()
{
errorBoundary?.Recover();
}
}
3 changes: 2 additions & 1 deletion src/Presentation/Client/Shared/FeatureProducts.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ else
<div class="container">
@foreach (var product in ProductService.Products)
{
@*throw new InvalidOperationException("Product ErrorBoundary Test");*@
@if (product.Featured)
{
<div class="featured-product">
Expand Down Expand Up @@ -41,7 +42,7 @@ else

@code {
protected override void OnInitialized()
{
{
ProductService.ProductsChanged += StateHasChanged;
}

Expand Down
File renamed without changes.

0 comments on commit 10fabfe

Please sign in to comment.