-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
base: main
Are you sure you want to change the base?
Make huma play well with custom error types returned from handlers #504
Conversation
WalkthroughThe changes introduce a new function Changes
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
Assessment against linked issues
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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
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 ofHandleTypedError
.Ensure that the
HandleTypedError
function is properly implemented to handle different error types appropriately. Refer to the suggested improvements in theerror.go
file review.huma_test.go (4)
1943-1945
: LGTM!The
NotFoundError
type is correctly implemented.
1947-1949
: LGTM!The
Error
method forNotFoundError
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.
768cabe
to
5ab1dea
Compare
There was a problem hiding this 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
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
There was a problem hiding this 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!
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 |
Codecov ReportAttention: Patch coverage is
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. |
Allow use of custom handling when custom errors are returned from handlers.
Closes: #482
Summary by CodeRabbit
New Features
HandleTypedError
function for dynamic HTTP error responses.NotFoundError
type for custom error messages in resource not found scenarios.Bug Fixes
Register
function to delegate error handling to theHandleTypedError
function, ensuring consistent and descriptive error responses.