Handling Exceptions

David Thielen 3,211 Reputation points
2023-06-30T16:37:08.4633333+00:00

Hi all;

Is this a good way to handle exceptions globally?

In any razor.cs page I have:

			catch (Exception ex)
			{
				ex.Data.Add("Category", typeof(CampaignPage));
				throw;
			}

And then in MainLayout.razor I have:

@inherits LayoutComponentBase
@inject ILoggerFactory LoggerFactory
@inject ILogger<MainLayout> Logger

<ErrorBoundary>
	<ChildContent>
		<div style="height: 100%; overflow-y: hidden;">
			<div style="margin: 0; height: 100%; overflow-y: hidden;">
				<div style="display: flex; flex-direction: column; height: 100%; overflow-y: hidden;">
					<div style="width: 100%;">
						<header>
							<Banner ShowBanner="@SessionData.ShowWelcomeBanner"
									OnBannerClosed="WelcomeBannerClosed"
									BannerContent="Please complete your registration. <a href='/Admin/Welcome'>Click here for more information.</a>" />
						</header>
					</div>
					<div style="width: 100%;">
						<NavMenu />
					</div>
					<div style="flex-grow: 1; width: 100%; overflow: auto;">
						<div style="height: 100%; overflow: auto;">
							<main>
								<div class="my-main-scrollbar">
									<PopupMessageBox>
										@Body
									</PopupMessageBox>
								</div>
							</main>
						</div>
					</div>
					<div style="width: 100%;">
						<footer class="main-footer">
							<span>&copy; David Thielen 2023 - ALL RIGHTS RESERVED</span>
						</footer>
					</div>
				</div>
			</div>
		</div>
	</ChildContent>
	<ErrorContent Context="error">
		@{
			var guid = Guid.NewGuid().ToString();
			var useFallback = true;
			if (error.Data.Contains("Category"))
			{
				var data = error.Data["Category"];
				if (data is Type category)
				{
					var logger = LoggerFactory.CreateLogger(category);
					logger.LogError(error, "Error: {guid}", guid);
					useFallback = false;
				}
			}
			if (useFallback)
			{
				Logger.LogError(error, "Error: {guid}", guid);
			}
		}

		<h1>Unexpected Error (sorry)</h1>

		<p>
			The program has encountered an unexpected error (yes there are expected errors). This error has been
			logged and we will look at it.
		</p>

		<p>We suggest you <a href="/identity/account/LogOut">logout</a> and then log back in if you wish to try again.</p>

		<p>If you want to contact us, please include the following error id: @guid when you do.</p>

	</ErrorContent>
</ErrorBoundary>

Is this a good way to globally handle exceptions?

I assign the Category to Exception.Data so that the category is for where it was thrown for when I filter the log. Otherwise every exception would be in the category of MainLayout.

thoughts - ??? - dave

Developer technologies | .NET | Blazor
0 comments No comments
{count} votes

Accepted answer
  1. Xinran Shen - MSFT 2,091 Reputation points
    2023-07-03T02:38:05.1566667+00:00

    Hi @David Thielen,

    From your code, You add custom Category info to Exception.Data in each razor component, Then filter exceptions via this custom Category info in MainLayout.razor. In my opinion, It is very reasonable, Because of this custom Category info, you can easily filter the exceptions and you can also group razor components by different custom Category info. In all, It is a very good approach to handle exceptions globally.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Xinran Shen

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.