Skip to content

Implement IProblemDetailsWriter in Boilerplate (#9764)#9765

Merged
msynk merged 6 commits intobitfoundation:developfrom
yasmoradi:9764
Jan 31, 2025
Merged

Implement IProblemDetailsWriter in Boilerplate (#9764)#9765
msynk merged 6 commits intobitfoundation:developfrom
yasmoradi:9764

Conversation

@yasmoradi
Copy link
Member

@yasmoradi yasmoradi commented Jan 31, 2025

closes #9764

Summary by CodeRabbit

  • New Features

    • Introduced a new AppProblemDetail class for structured error reporting
    • Enhanced error handling with more detailed problem details
  • Bug Fixes

    • Improved error handling mechanism across server and client components
    • Updated exception handling to provide more consistent error responses
  • Refactor

    • Replaced RestErrorInfo with AppProblemDetail for error reporting
    • Renamed ErrorResourcePayload to ModelStateErrors
    • Updated exception handling services and middlewares
  • Chores

    • Updated JSON serialization context for new error types
    • Modified exception handler configurations

@yasmoradi yasmoradi requested a review from msynk January 31, 2025 17:43
@coderabbitai
Copy link

coderabbitai bot commented Jan 31, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes represent a comprehensive refactoring of error handling and problem details management across multiple components of the application. The primary focus is on transitioning from a custom RestErrorInfo structure to a standardized AppProblemDetail approach, which aligns with RFC 7807 problem details specification. This involves modifying exception handlers, service registrations, and serialization contexts to support a more structured and consistent error reporting mechanism.

Changes

File Change Summary
src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/ExceptionDelegatingHandler.cs Updated error handling to use AppProblemDetail instead of RestErrorInfo
src/Server/Boilerplate.Server.Api/Program.Middlewares.cs Simplified exception handler configuration by removing createScopeForErrors parameter
src/Server/Boilerplate.Server.Api/Program.Services.cs Replaced exception handler registration with problem details writer and added problem details services
src/Server/Boilerplate.Server.Api/Services/ServerExceptionHandler.cs Implemented IProblemDetailsWriter with new CanWrite and WriteAsync methods
src/Server/Boilerplate.Server.Web/Program.Middlewares.cs Simplified exception handler configuration
src/Shared/Dtos/AppJsonContext.cs Updated JSON serialization context to support AppProblemDetail and ModelStateErrors
src/Shared/Exceptions/ Added AppProblemDetail, renamed ErrorResourcePayload to ModelStateErrors, updated ResourceValidationException
src/Shared/Exceptions/RestErrorInfo.cs Removed the entire class

Assessment against linked issues

Objective Addressed Explanation
Implement IProblemDetailsWriter

Sequence Diagram

sequenceDiagram
    participant Client
    participant ExceptionHandler
    participant ProblemDetailsWriter
    participant HttpContext

    Client->>ExceptionHandler: Trigger exception
    ExceptionHandler->>ProblemDetailsWriter: Can handle problem details?
    ProblemDetailsWriter-->>ExceptionHandler: Yes
    ExceptionHandler->>ProblemDetailsWriter: Write problem details
    ProblemDetailsWriter->>HttpContext: Set response details
    ProblemDetailsWriter-->>ExceptionHandler: Problem details written
Loading

Poem

🐰 A Rabbit's Ode to Error Handling 🚨

In bytes of code, a problem grows,
Where RestErrorInfo no longer flows
AppProblemDetail takes center stage
With RFC's wisdom on every page
Error handling, now clean and bright! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (6)
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/ServerExceptionHandler.cs (4)

7-7: Consider class naming consistency.

Thanks for adjusting to IProblemDetailsWriter. If we're phasing out the old "ExceptionHandler" nomenclature, it might be more consistent to rename this class to reflect the new IProblemDetailsWriter interface and avoid confusion with the old design.


Line range hint 14-28: Revisit redirect handling for security implications.

When an AuthenticationFailureException is encountered, the code redirects without setting any status code. This is fine for some flows, but consider returning a standard 3xx status if needed, or capturing additional context for security logging.


43-53: Rename variable to align with new naming.

Renaming restExceptionPayload to something like problemDetail or appProblemDetail may better reflect the new AppProblemDetail structure and improve readability.

- var restExceptionPayload = new AppProblemDetail
+ var appProblemDetail = new AppProblemDetail
{
    ...
}

62-62: Check cancellation handling for long writes.

You pass httpContext.RequestAborted as a cancellation token. Ensure any upstream processes also respect this token to prevent partial writes if the request is canceled.

src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/ResourceValidationException.cs (2)

Line range hint 26-42: Encapsulate logic for constructing ModelStateErrors.

The inline creation of ModelStateErrors is clear, but you might consider a helper method to fill its properties. This reduces duplication and keeps the constructor more streamlined.


55-55: Validate default property initialization.

public ModelStateErrors Payload { get; set; } = new ModelStateErrors(); might hide logic if you ever intend to manage an empty vs. non-empty state. This is fine if you always need a default.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 815d058 and f6faed5.

📒 Files selected for processing (10)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/ExceptionDelegatingHandler.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Middlewares.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Services.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/ServerExceptionHandler.cs (3 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Dtos/AppJsonContext.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/AppProblemDetail.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/ModelStateErrors.cs (1 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/ResourceValidationException.cs (2 hunks)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/RestErrorInfo.cs (0 hunks)
💤 Files with no reviewable changes (1)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/RestErrorInfo.cs
✅ Files skipped from review due to trivial changes (1)
  • src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/ModelStateErrors.cs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test
🔇 Additional comments (9)
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Middlewares.cs (1)

59-59: LGTM! Verify integration with IProblemDetailsWriter.

The simplified UseExceptionHandler() call aligns with the PR's objective to implement IProblemDetailsWriter. This change supports the transition to RFC 7807 problem details for standardized error handling.

Run the following script to verify the integration:

✅ Verification successful

✅ Implementation verified: Proper integration with Problem Details middleware

The simplified UseExceptionHandler() is correctly integrated with the custom ServerExceptionHandler that implements IProblemDetailsWriter, providing standardized RFC 7807 problem details responses. The implementation properly handles various exception types and includes all necessary problem details fields.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation and registration of IProblemDetailsWriter.

# Test 1: Check for IProblemDetailsWriter implementation
echo "Checking for IProblemDetailsWriter implementation..."
ast-grep --pattern 'class $_ : IProblemDetailsWriter'

# Test 2: Check for problem details writer registration
echo "Checking for problem details writer registration..."
rg -A 5 'AddProblemDetails|UseProblemDetails'

# Test 3: Check for any remaining RestErrorInfo usage that needs migration
echo "Checking for any remaining RestErrorInfo usage..."
rg 'RestErrorInfo'

Length of output: 5713


Script:

#!/bin/bash
# Description: Check Boilerplate project for problem details implementation and RestErrorInfo usage

echo "Checking Boilerplate project for problem details related code..."
rg -g 'src/Templates/Boilerplate/**' -l 'ProblemDetails|IProblemDetailsWriter'

echo -e "\nChecking for RestErrorInfo in Boilerplate project..."
rg -g 'src/Templates/Boilerplate/**' 'RestErrorInfo'

echo -e "\nChecking Boilerplate project's exception handling setup..."
rg -g 'src/Templates/Boilerplate/**' -A 5 'UseExceptionHandler|AddExceptionHandler'

Length of output: 2321


Script:

#!/bin/bash
# Description: Check the implementation of ServerExceptionHandler

echo "Checking ServerExceptionHandler implementation..."
cat src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/ServerExceptionHandler.cs

echo -e "\nChecking problem details registration..."
cat src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Services.cs

Length of output: 21699

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/ServerExceptionHandler.cs (2)

12-13: Double-check CanWrite condition.

Always returning true indicates this writer will handle every scenario. If multiple writers are registered, consider whether you need logic to selectively handle certain exceptions or statuses.


58-58: Ensure resource validation payload does not leak sensitive data.

Adding the validationException.Payload to the Extensions is beneficial for debugging. However, confirm that no sensitive or private data fields are inadvertently exposed.

src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/ResourceValidationException.cs (2)

43-44: Confirm default error message.

By calling nameof(AppStrings.ResourceValidationException) as the default message, ensure that AppStrings.ResourceValidationException is properly localized or user-friendly.


49-52: Good use of nullable parameter.

Allowing ModelStateErrors? for flexibility is excellent. The fallback to a new instance keeps it robust.

src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Dtos/AppJsonContext.cs (1)

28-29: Nicely aligned with new error model.

Registering AppProblemDetail and ModelStateErrors for source generation ensures standardized JSON handling—this is crucial for consistent error serialization.

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Middlewares.cs (1)

42-42: LGTM!

The simplified exception handler middleware configuration aligns with the transition to using IProblemDetailsWriter for error handling.

src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Exceptions/AppProblemDetail.cs (1)

1-66: Well-structured implementation of RFC 7807 problem details.

The class provides a comprehensive implementation with:

  • Clear XML documentation for all properties
  • Proper JSON serialization configuration
  • Support for extension data
src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.Services.cs (1)

93-94: LGTM!

The registration of ServerExceptionHandler as IProblemDetailsWriter and the addition of ProblemDetails service are correctly configured.

@msynk msynk merged commit 29cfde2 into bitfoundation:develop Jan 31, 2025
2 checks passed
@yasmoradi yasmoradi deleted the 9764 branch February 1, 2025 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Boilerplate project template doesn't implement IProblemDetailsWriter

2 participants