[Bug] exception not catched into the modal component
See original GitHub issueDescribe the bug
I use a ilogger global exception handler and I call services from my component which is in my modal. when a service throws an exception I can’t catch it from my global handler.
it works fine all the application except for the exceptions of the services called by my component in the modal
Is there a way to catch the exception that thrown inside the modal? Is it a different thread? A cascading problem?
** Code **
My Ilogger
public interface IUnhandledExceptionSender
{
event EventHandler<Exception> UnhandledExceptionThrown;
}
public class UnhandledExceptionSender : ILogger, IUnhandledExceptionSender
{
public event EventHandler<Exception> UnhandledExceptionThrown;
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
Exception exception, Func<TState, Exception, string> formatter)
{
if (exception != null)
{
UnhandledExceptionThrown?.Invoke(this, exception);
}
}
}
public class UnhandledExceptionProvider : ILoggerProvider
{
UnhandledExceptionSender _unhandledExceptionSender;
private ITechnicalLogger _technicalLogger;
private IToastService _toastService;
public UnhandledExceptionProvider(UnhandledExceptionSender unhandledExceptionSender, ITechnicalLogger technicalLogger, IToastService toastService)
{
_unhandledExceptionSender = unhandledExceptionSender;
_technicalLogger = technicalLogger;
_toastService = toastService;
}
public ILogger CreateLogger(string categoryName)
{
return new UnhandledExceptionLogger(categoryName, _unhandledExceptionSender, _technicalLogger, _toastService);
}
public void Dispose()
{
}
public class UnhandledExceptionLogger : ILogger
{
private readonly string _categoryName;
private readonly UnhandledExceptionSender _unhandeledExceptionSender;
private ITechnicalLogger _technicalLogger;
private IToastService _toastService;
public UnhandledExceptionLogger(string categoryName, UnhandledExceptionSender unhandledExceptionSender, ITechnicalLogger technicalLogger, IToastService toastService)
{
_unhandeledExceptionSender = unhandledExceptionSender;
_categoryName = categoryName;
_technicalLogger = technicalLogger;
_toastService = toastService;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (exception != null)
{
//All exceptions catched here except from my modal component
_technicalLogger.Error("Error occurred", exception);
}
}
public IDisposable BeginScope<TState>(TState state)
{
return new NoopDisposable();
}
private class NoopDisposable : IDisposable
{
public void Dispose()
{
}
}
}
Program init
var unhandledExceptionSender = new UnhandledExceptionSender();
var unhandledExceptionProvider = new UnhandledExceptionProvider(unhandledExceptionSender, technicalLogger, null);
builder.Logging.AddProvider(unhandledExceptionProvider);
builder.Services.AddSingleton<IUnhandledExceptionSender>(unhandledExceptionSender);
App.razor
<CascadingBlazoredModal>
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingBlazoredModal>
Method to open the modal
public async Task Edit(int id)
{
var parameters = new ModalParameters();
parameters.Add(nameof(AgencyEdit.AgencyId), id);
var editModal = Modal.Show<AgencyEdit>("Edit", parameters, new ModalOptions
{
Animation = ModalAnimation.FadeIn(1)
});
var result = await editModal.Result;
if (!result.Cancelled)
{
await RefreshList();
}
}
My Edit component into the modal
public partial class AgencyEdit : ComponentBase
{
[Inject]
private IAgencyService _agencyService { get; set; }
[Inject]
private IToastService _toastService { get; set; }
[CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; }
[Parameter]
public int AgencyId { get; set; }
public AgencyViewModel Agency { get; set; }
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
Agency = new AgencyViewModel();
}
protected override async Task OnParametersSetAsync()
{
if (AgencyId > 0)
{
Agency = await _agencyService.GetAgency(AgencyId);
}
}
public async void HandleValidSubmit()
{
// This method throws an exception !
await _agencyService.AddAgency(Agency))
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
show modal Bootstrap on catch exception
modal -backdrop').appendTo('#aspnetForm'); is code to correct styles so the modal is up in front and not weirdly placed behind a gray see through ......
Read more >Is a "catch all" error handler in _app.js possible and is it ...
I am thinking about creating an error modal component in _app.js that handles ... Ofcourse it's possible and no it's not a terrible...
Read more >Creating a modal error box after Exception thrown from ...
I handle this possibility by encapsulating the data processing in a 'try-catch' block. If an error occurs such as a database time out,...
Read more >Triggering an Error Modal in React
I decided to implement a popup modal which would display error ... So I made sure to map that error message to props...
Read more >How can I show an exception that occurs in application ...
I just hit the button for opening the modal, and I can't see the modal due to the exception in OnGetAsync method. Actually,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
ok thank your help, i will migrate to 5.0.0-preview2, it seems to be resolve my issue
It seems like this has been solved, so I will close this issue. If it isn’t fixed yet, let us know!