Skip to content

feat(IIpLocatorProvider): add LastError property#7708

Merged
ArgoZhang merged 12 commits intomainfrom
doc-local
Feb 23, 2026
Merged

feat(IIpLocatorProvider): add LastError property#7708
ArgoZhang merged 12 commits intomainfrom
doc-local

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Feb 23, 2026

Link issues

fixes #7707

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Improve IP geolocation error handling and update localization-related documentation and samples.

Bug Fixes:

  • Ensure Baidu IP locator only returns a location when the response status is successful and data is present.

Enhancements:

  • Expose a LastError property on IP locator providers and clear it at the start of each lookup to surface provider failures to callers.
  • Simplify the Baidu IP locator response model by removing the ToString override and using a non-null, default-initialized data collection.
  • Update the IP locator sample to display provider error messages when lookups fail.
  • Simplify localization documentation by removing outdated .NET 5.0 configuration examples.

Copilot AI review requested due to automatic review settings February 23, 2026 02:11
@bb-auto bb-auto bot added the documentation Improvements or additions to documentation label Feb 23, 2026
@bb-auto bb-auto bot added this to the v10.3.0 milestone Feb 23, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Feb 23, 2026

Reviewer's Guide

Refines the IP locator providers to expose detailed error information and adjust Baidu IP lookup parsing, updates the localization sample/docs to remove obsolete .NET 5 content, and aligns localization strings accordingly.

Sequence diagram for IP location lookup with error handling

sequenceDiagram
    actor User
    participant LocatorsComponent
    participant IpLocatorFactory
    participant IIpLocatorProvider
    participant BaiduIpLocatorProvider
    participant BaiduApi

    User->>LocatorsComponent: Click Locate button
    LocatorsComponent->>IpLocatorFactory: Create(ProviderName)
    IpLocatorFactory-->>LocatorsComponent: IIpLocatorProvider provider
    LocatorsComponent->>IIpLocatorProvider: Locate(Ip)
    activate IIpLocatorProvider
    IIpLocatorProvider->>BaiduIpLocatorProvider: Locate(Ip)
    activate BaiduIpLocatorProvider
    BaiduIpLocatorProvider->>BaiduApi: HTTP GET IP location
    BaiduApi-->>BaiduIpLocatorProvider: LocationResult json
    BaiduIpLocatorProvider->>BaiduIpLocatorProvider: Parse Status and Data
    alt Status is 0 and Data has Location
        BaiduIpLocatorProvider-->>IIpLocatorProvider: location string
    else Error or exception
        BaiduIpLocatorProvider->>BaiduIpLocatorProvider: set LastError
        BaiduIpLocatorProvider-->>IIpLocatorProvider: null
    end
    deactivate BaiduIpLocatorProvider
    IIpLocatorProvider-->>LocatorsComponent: location or null
    deactivate IIpLocatorProvider

    LocatorsComponent->>LocatorsComponent: check provider.LastError
    alt LastError not empty
        LocatorsComponent->>LocatorsComponent: Location = LastError
    else LastError empty
        LocatorsComponent->>LocatorsComponent: Location = result
    end
    LocatorsComponent-->>User: Show Location text
Loading

Updated class diagram for IP locator providers

classDiagram
    direction LR

    class IIpLocatorProvider {
        <<interface>>
        +string Key
        +string LastError
        +Task~string?~ Locate(string? ip)
    }

    class DefaultIpLocatorProvider {
        -IOptions~BootstrapBlazorOptions~ Options
        -List~string~ _localhostList
        +string Key
        +string LastError
        +Task~string?~ Locate(string? ip)
        #DefaultIpLocatorProvider(IOptions~BootstrapBlazorOptions~ options)
    }

    class BaiduIpLocatorProvider {
        -IHttpClientFactory HttpClientFactory
        -IOptions~BootstrapBlazorOptions~ Options
        +string Key
        +string LastError
        +Task~string?~ Locate(string? ip)
        #Task~string?~ Fetch(string url, HttpClient client, CancellationToken token)
    }

    class LocationResult {
        +string Status
        +List~LocationData~ Data
    }

    class LocationData {
        +string Location
    }

    class LocatorsComponent {
        -IIpLocatorFactory IpLocatorFactory
        -string ProviderName
        -string Ip
        -string Location
        -Task OnClick()
    }

    IIpLocatorProvider <|.. DefaultIpLocatorProvider
    IIpLocatorProvider <|.. BaiduIpLocatorProvider

    LocationResult "1" o-- "*" LocationData

    LocatorsComponent --> IIpLocatorProvider : uses via IpLocatorFactory
Loading

File-Level Changes

Change Details Files
Handle Baidu IP lookup responses directly in the provider and surface errors instead of relying on LocationResult.ToString().
  • Change Fetch to parse LocationResult directly, returning the first Location when Status is "0".
  • Wrap Fetch in try/catch and assign LastError on exceptions instead of throwing.
  • Make LocationResult.Data a non-null list with a default empty value and remove the ToString override that previously did the parsing.
src/BootstrapBlazor/Services/IPLocator/BaiduIpLocatorProvider.cs
Expose and manage the last IP lookup error through the IP locator abstraction and default implementation.
  • Extend IIpLocatorProvider with a LastError read-only property for error reporting.
  • Implement LastError with protected setter in DefaultIpLocatorProvider and reset it at the start of each Locate call.
src/BootstrapBlazor/Services/IPLocator/IIpLocatorProvider.cs
src/BootstrapBlazor/Services/IPLocator/DefaultIPLocatorProvider.cs
Update the IP locator sample component to display error details when a lookup fails.
  • After calling Locate, check provider.LastError and display the error message instead of the location when it is non-empty.
src/BootstrapBlazor.Server/Components/Samples/Locators.razor.cs
Simplify localization documentation by removing obsolete .NET 5 configuration samples and syncing localization resources.
  • Remove the .NET 5.0 Tab/TabItem and associated Startup code from the Localization documentation page, keeping only current configuration guidance.
  • Adjust localization resource files to match the updated documentation and samples.
src/BootstrapBlazor.Server/Components/Pages/Localization.razor
src/BootstrapBlazor.Server/Locales/en-US.json
src/BootstrapBlazor.Server/Locales/zh-CN.json

Assessment against linked issues

Issue Objective Addressed Explanation
#7707 Update the localization documentation component/page (Localization.razor) to reflect the current, supported configuration and remove outdated content.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In BaiduIpLocatorProvider.Fetch, you're only setting LastError on exceptions; consider also populating LastError when the API returns a non-success Status so callers can distinguish between "no data" and an upstream error.
  • Swallowing exceptions in BaiduIpLocatorProvider.Fetch without any logging may make diagnosing issues difficult; consider adding some form of logging or telemetry hook in the catch block in addition to setting LastError.
  • In Locators.razor.cs, Location is overwritten by LastError when an error occurs; it may be clearer to keep location and error separate (e.g., a dedicated error field) so the UI can distinguish between a valid location and an error message.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `BaiduIpLocatorProvider.Fetch`, you're only setting `LastError` on exceptions; consider also populating `LastError` when the API returns a non-success `Status` so callers can distinguish between "no data" and an upstream error.
- Swallowing exceptions in `BaiduIpLocatorProvider.Fetch` without any logging may make diagnosing issues difficult; consider adding some form of logging or telemetry hook in the catch block in addition to setting `LastError`.
- In `Locators.razor.cs`, `Location` is overwritten by `LastError` when an error occurs; it may be clearer to keep location and error separate (e.g., a dedicated error field) so the UI can distinguish between a valid location and an error message.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor.Server/Components/Samples/Locators.razor.cs:72-77` </location>
<code_context>
         {
             var provider = IpLocatorFactory.Create(ProviderName);
             Location = await provider.Locate(Ip);
+
+            if (!string.IsNullOrEmpty(provider.LastError))
+            {
+                Location = provider.LastError;
+            }
         }
</code_context>

<issue_to_address>
**🚨 issue (security):** Showing raw LastError messages directly in the UI may expose internal details.

Because `provider.LastError` comes from `ex.Message`, assigning it to `Location` risks exposing internal implementation or infrastructure details to end users. Instead, keep `Location` for location data and surface a generic, user-friendly error message (optionally localized), while logging the detailed error server-side.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ArgoZhang ArgoZhang changed the title doc(Localization): update localization documentation feat(IIpLocatorProvider): add LastError property Feb 23, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9cfd4ff) to head (895e742).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #7708   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          749       749           
  Lines        33190     33207   +17     
  Branches      4603      4604    +1     
=========================================
+ Hits         33190     33207   +17     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the server-side localization documentation/sample content and extends the IP locator provider API to surface error details from the last lookup.

Changes:

  • Adds LastError to IIpLocatorProvider and implements it in the default/base locator provider.
  • Refines BaiduIpLocatorProvider response parsing and exception handling to set LastError.
  • Updates the server localization docs/resources and the locator sample to display provider errors.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/BootstrapBlazor/Services/IPLocator/IIpLocatorProvider.cs Adds LastError to the IP locator provider interface.
src/BootstrapBlazor/Services/IPLocator/DefaultIPLocatorProvider.cs Implements LastError and resets it at the start of Locate.
src/BootstrapBlazor/Services/IPLocator/BaiduIpLocatorProvider.cs Parses Baidu API response explicitly and sets LastError on exceptions; adjusts result model.
src/BootstrapBlazor.Server/Locales/zh-CN.json Removes unused localization keys tied to removed doc content.
src/BootstrapBlazor.Server/Locales/en-US.json Removes unused localization keys tied to removed doc content.
src/BootstrapBlazor.Server/Components/Samples/Locators.razor.cs Shows provider error details in the sample UI.
src/BootstrapBlazor.Server/Components/Pages/Localization.razor Simplifies localization documentation by removing the NET5 tab/sample.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(IIpLocatorProvider): add LastError property

2 participants