-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Grape's error!
method for handler subclasses
#50
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
Conversation
Co-authored-by: GrantBirki <[email protected]>
Co-authored-by: GrantBirki <[email protected]>
Co-authored-by: GrantBirki <[email protected]>
error!
methoderror!
method for handler subclasses
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.
Pull Request Overview
This PR adds Grape‐style error!
support to handler subclasses by introducing a custom exception, wiring it into the handler base class, and catching it in the API layer to return structured error responses. It also includes corresponding unit and acceptance test updates.
- Introduced
Hooks::Plugins::Handlers::Error
exception withbody
andstatus
payload - Added
error!
method toHandlers::Base
that raises the new exception - Updated
Hooks::App::API
to rescueHandlers::Error
and return JSON responses - Expanded unit tests for the exception and error! method; fixed acceptance tests for the new behavior
Reviewed Changes
Copilot reviewed 10 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
spec/unit/lib/hooks/handlers/error_spec.rb | Added tests verifying Error initialization behavior |
spec/unit/lib/hooks/handlers/base_spec.rb | Added tests for Base#error! raising Error |
spec/acceptance/plugins/handlers/boomtown_with_error.rb | Updated comment to reflect new error! usage |
spec/acceptance/acceptance_tests.rb | Rewrote acceptance test to expect custom handler error |
lib/hooks/plugins/handlers/error.rb | Implemented custom Error exception |
lib/hooks/plugins/handlers/base.rb | Added error! method to handler base class |
lib/hooks/app/api.rb | Caught Handlers::Error in API and returned JSON errors |
.bundle/config | CI environment paths mistakenly committed |
Comments suppressed due to low confidence (1)
.bundle/config:3
- [nitpick] This CI/workflow–specific change to
.bundle/config
was committed; environment-specific paths should not be in version control. Consider reverting these lines or adding.bundle/config
to.gitignore
.
BUNDLE_PATH: "/home/runner/work/hooks/hooks/vendor/bundle"
…handler and add corresponding acceptance test
@copilot everything looks really good and works great on my machine running Ruby
Please resolve these errors in the unit tests. When finished, make sure to run |
Co-authored-by: GrantBirki <[email protected]>
Fixed the test compatibility issue with different Ruby versions. The test was expecting Ruby 3.4.4's hash formatting but older versions use different syntax. Updated the test expectation to use a regex pattern that works across all Ruby versions. All tests now pass. (2760d30) |
This PR implements support for Grape's
error!
method in handler subclasses, allowing them to immediately terminate request processing and return custom error responses instead of raising genericStandardError
exceptions.Problem
Previously, handler subclasses could only raise standard exceptions which would be caught and converted to generic 500 errors with
"server_error"
messages. This made it impossible for handlers to return specific error codes and custom error responses. For example:Solution
The implementation uses a custom exception class to bridge the gap between handler classes and the Grape API context:
Hooks::Plugins::Handlers::Error
to carry error details from handlers back to the APIerror!(body, status = 500)
method toHooks::Plugins::Handlers::Base
with the same interface as Grape's nativeerror!
StandardError
handlingUsage
Handlers can now call
error!
with the same interface as Grape:Changes Made
New files:
lib/hooks/plugins/handlers/error.rb
- Custom exception classspec/unit/lib/hooks/handlers/error_spec.rb
- Unit tests for exception classModified files:
lib/hooks/plugins/handlers/base.rb
- Addederror!
methodlib/hooks/app/api.rb
- Added rescue block for handler errorsspec/unit/lib/hooks/handlers/base_spec.rb
- Unit tests forerror!
methodspec/acceptance/acceptance_tests.rb
- Uncommented and fixed failing acceptance testspec/acceptance/plugins/handlers/boomtown_with_error.rb
- Updated TODO commentTesting
Fixes #49.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.