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

Make huma play well with custom error types returned from handlers #504

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

camcalaquian
Copy link

@camcalaquian camcalaquian commented Jul 14, 2024

Allow use of custom handling when custom errors are returned from handlers.

Closes: #482

Summary by CodeRabbit

  • New Features

    • Improved error handling with the new HandleTypedError function for dynamic HTTP error responses.
    • Introduced a NotFoundError type for custom error messages in resource not found scenarios.
  • Bug Fixes

    • Updated Register function to delegate error handling to the HandleTypedError function, ensuring consistent and descriptive error responses.

Copy link

coderabbitai bot commented Jul 14, 2024

Walkthrough

The changes introduce a new function HandleTypedError to dynamically generate HTTP error responses based on different error types, enhancing error management in the huma framework. This affects the Register function to delegate error handling to HandleTypedError. Additionally, the tests now include a NotFoundError type to validate specific error handling for resource not found scenarios.

Changes

File Change Summary
error.go Added HandleTypedError function to handle different error types and provide generic error responses.
huma.go Modified Register function to use HandleTypedError for error handling instead of creating new errors.
huma_test.go Introduced NotFoundError type and updated TestTypedError function to handle this specific error type.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant RegisterFunction
    participant Handler

    Client->>API: Sends Request
    API->>RegisterFunction: Calls Register with handler
    RegisterFunction->>Handler: Executes Handler
    Handler-->>RegisterFunction: Returns Error
    RegisterFunction->>RegisterFunction: Calls HandleTypedError(err)
    RegisterFunction-->>Client: Returns Typed Error Response
Loading

Assessment against linked issues

Objective Addressed Explanation
Make huma play well with custom error types returned from handlers (#482)
Allow error handling with type switches
Ensure original error type isn’t lost, enabling specific error responses
Update test cases to include custom error types

Poem

In the code where errors roam,
A change was made, now they find a home.
Typed and handled with care so true,
HandleTypedError, we thank you.
Errors now speak their name,
In Huma’s world, they find their fame. 🌟


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b8be41a and 768cabe.

Files selected for processing (3)
  • error.go (1 hunks)
  • huma.go (1 hunks)
  • huma_test.go (1 hunks)
Additional comments not posted (5)
huma.go (1)

1344-1344: Verify the implementation of HandleTypedError.

Ensure that the HandleTypedError function is properly implemented to handle different error types appropriately. Refer to the suggested improvements in the error.go file review.

huma_test.go (4)

1943-1945: LGTM!

The NotFoundError type is correctly implemented.


1947-1949: LGTM!

The Error method for NotFoundError is correctly implemented.


1951-1980: LGTM!

The TestTypedError function is correctly implemented and verifies the handling of custom error types.


1981-1981: LGTM!

The TestCustomError function is correctly implemented and verifies custom error handling.

error.go Show resolved Hide resolved
@camcalaquian camcalaquian force-pushed the implement-custom-error-type-returned-from-handler branch from 768cabe to 5ab1dea Compare July 15, 2024 10:18
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 768cabe and 5ab1dea.

Files selected for processing (3)
  • error.go (1 hunks)
  • huma.go (1 hunks)
  • huma_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • error.go
  • huma.go
  • huma_test.go

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

I'm curious how this is meant to be different from the existing custom error support? See https://huma.rocks/features/response-errors/#custom-errors where you can implement a GetStatus() int method. Your handler can check for various internal errors and wrap them as needed with status information, and if you prefer to not duplicate such logic in each handler you can use a utility function to wrap the handlers potentially.

Just trying to understand what this gives us over what is already there, thanks!

@leonklingele
Copy link
Contributor

I find this change quite useful to implement logging. My handlers return a custom error type which includes some additional context which is only meant to be logged and not returned to the client.

Also, say along the way of handling a request my database package returns a ErrNotFound — I can catch this in the new func HandleTypedError (don't like the name to be honest) and return a http.StatusNotFound to the client.

Copy link

codecov bot commented Aug 1, 2024

Codecov Report

Attention: Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 92.85%. Comparing base (b8be41a) to head (5ab1dea).
Report is 45 commits behind head on main.

Files Patch % Lines
error.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #504   +/-   ##
=======================================
  Coverage   92.85%   92.85%           
=======================================
  Files          22       22           
  Lines        3791     3793    +2     
=======================================
+ Hits         3520     3522    +2     
- Misses        227      228    +1     
+ Partials       44       43    -1     

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

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.

Make huma play well with custom error types returned from handlers
3 participants