Fix swallowing original error#49
Fix swallowing original error#49matt-goldman merged 3 commits intomatt-goldman:mainfrom IeuanWalker:main
Conversation
Wrap ActivatorUtilities.CreateInstance call in a try-catch block to handle exceptions. An empty catch block is included to avoid silent failures, while still rethrowing any caught exceptions.
|
Thanks for catching this. Sorry for the delay, I'm not 100% sure about this change as it moves from catching an explicitly defined error to catching a generic error instead. I think the problem here is that this masks an exception further down the chain that gets bubbled up here for some reason. Let me give some thought to potential alternative ways of catching this. Otherwise this might need a flag as the |
|
@matt-goldman no prob, i did do it as a AggregateException orignally, so nothing will be missed. But the logging solution I'm using wasn't handling AggregateException that well and was missing a lot of details. Will have another play when I get back into Monday. |
|
@matt-goldman Sorry been pretty busy with work. Not really sure where to go with this tbh, I only see 3 possible solutions -
private static Page CreatePageUsingViewModel<T>(IServiceProvider serviceProvider, object viewModel) where T : Page
{
try
{
return ActivatorUtilities.CreateInstance<T>(serviceProvider, viewModel);
}
catch (MissingMemberException ex)
{
try
{
return ActivatorUtilities.CreateInstance<T>(Resolver.GetServiceProvider());
}
catch (Exception ex1)
{
throw new AggregateException(ex, ex1);
}
}
}
private static Page CreatePageUsingViewModel<T>(IServiceProvider serviceProvider, object viewModel) where T : Page
{
try
{
return ActivatorUtilities.CreateInstance<T>(serviceProvider, viewModel);
}
catch (MissingMemberException ex)
{
Trace.WriteLine(ex);
return ActivatorUtilities.CreateInstance<T>(Resolver.GetServiceProvider());
}
}My personal preference would be the AggregateException as no information is lost doing it that way. And just to comment on this -
It only throws it if both CreateInstance calls fails, and there is nothing stopping the second call from also throwing MissingMemberException so wont be a change of behaviour, unless I'm missing something. |
|
Hi @IeuanWalker thanks for your patience on this! And my apologise too; I've been swamped and also not come back to this until now. You’re not missing anything, I just didn’t articulate my concern clearly. The I think your latest suggestion (rethrowing an Happy to go with the |
- Updated `DemoProject.csproj` to include `AggregateExceptionPage.xaml`. - Reformatted `MainPage.xaml` and added a button to trigger an aggregate exception. - Introduced `TriggerAggregateException` command in `MainViewModel.cs` for navigation and exception handling. - Refined formatting in `NavigationExtensions.cs` and improved error handling. - Created `AggregateExceptionPage.xaml` and its code-behind to simulate an exception. - Added `AggregateExceptionViewModel.cs` for managing parameters in the new page.
This commit enhances the clarity of the code by adding spaces after `if`, `foreach`, and `for` keywords to align with C# coding conventions. Additionally, spaces were added around the `catch` keyword in the `CreatePageWithViewModel` method for better readability. These changes are purely cosmetic and do not affect the functionality of the code.
|
@matt-goldman ahh cool, just committed the changes :) |
matt-goldman
left a comment
There was a problem hiding this comment.
Lots of formatting changes - need an .editorconfig!
LGTM
Had an issue with release builds, where testers where reporting a specific page crashing, when I looked at the logs it was pointing to the fact that a service was missing from the DI container -


But everything was working fine when debugging. When I turned on trimming for the debug build I was able to replicate the issue, but was still getting the same error pointing to a service missing.
I had to clone PageResolver to be able to see the causing issue, which pointed to an issue with XAML accessing a property of an array using an index.
This PR is to return the original error, rather than the fallback attempt, which then results in the correct error being logged -

