Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation Exception: Introduce Clear Message & Helper Class #56644

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

spdebeer
Copy link

@spdebeer spdebeer commented Jul 5, 2024

Navigation Exception: Introduce Clear Message & Helper Class

Description:
This pull request is for adding a useful message to the NavigationException when thrown as well as a helper class that can be called instead of creating absolute URIs and throwing a NavigationException multiple times accross the codebase.

Changes:

  • Added an overload to the NavigationException constructor that accepts a custom message
  • Introduced a helper file to make throwing NavigationException easier
  • Updated the existing NavigationException references

Fixes #51787

NB:
I am unsure about the location of the helper file, the shared location suggested (in the previous inactive PR #54862) seems inaccessible to me, any help is appreciated.
There is a reference to NavigationException here that I am unsure if it needs to be updated as well

@spdebeer spdebeer requested a review from a team as a code owner July 5, 2024 10:12
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jul 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 5, 2024
@spdebeer
Copy link
Author

spdebeer commented Jul 5, 2024

@spdebeer please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree company="Retro Rabbit"

Comment on lines 11 to 28
public static class NavigationRedirectHelper
{
private const string RedirectExceptionMessage =
"A navigation was initiated during static rendering. " +
"This exception is not an error, but instead signals to the framework that a redirect should occur. " +
"It should not be caught by application code.";

/// <summary>
/// Creates an absolute URI and throws a new <see cref="NavigationException"/>.
/// </summary>

[DoesNotReturn]
public static void ThrowNavigationExceptionForRedirect(NavigationManager navigationManager, string uri)
{
var absoluteUriString = navigationManager.ToAbsoluteUri(uri).AbsoluteUri;
throw new NavigationException(absoluteUriString, RedirectExceptionMessage);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a helper class to replace what otherwise is two lines of code that get called in two locations. The cost of adding new APIs is not worth the "savings" that we might get. Specially since this exception is entirely thrown by the framework and no one else.

Comment on lines 19 to 26
/// <summary>
/// Initializes a new <see cref="NavigationException"/> instance with a custom message.
/// </summary>
public NavigationException(string uri, string message) : base(message)
{
Location = uri;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the extra constructor; we can simply pass the message to the base class since it's a static message. There's no need for the message to be customizable.

@javiercn javiercn added the pr: pending author input For automation. Specifically separate from Needs: Author Feedback label Jul 5, 2024
@spdebeer spdebeer marked this pull request as draft July 8, 2024 07:39
… & NavigationRedirectHelper references & pass static message to NavigationException main constructor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pr: pending author input For automation. Specifically separate from Needs: Author Feedback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NavigationException doesn't have a useful message.
2 participants