-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor error handling in Collection concern to use exceptions #47
Copy link
Copy link
Open
Labels
refactorStuff we should improveStuff we should improve
Description
Description
The Collection concern currently renders errors directly using render plain: which prevents controllers from customizing error responses and doesn't integrate with application-wide error handling. This violates separation of concerns and makes it impossible to provide JSON API responses or custom error pages.
Current code
lib/bunko/controllers/collection.rb:51-53, 65-67:
unless @post_type
render plain: "PostType '#{@collection_name}' not found in database. Run: rails bunko:setup[#{@collection_name}]", status: :not_found
return
endProblems this causes
- Controllers can't customize error responses (JSON, HTML layouts, etc.)
- No way to hook into application-wide error handling (
rescue_from) - Forces plain text errors - breaks API consistency
- Violates separation of concerns (view logic in concern)
- Can't use custom error pages or error tracking services
- Difficult to test error scenarios
Pros of refactoring
- Controllers can customize error handling via
rescue_from - Supports multiple response formats (JSON, HTML, XML)
- Integrates with error tracking (Sentry, Honeybadger, etc.)
- Follows Rails conventions for error handling
- More testable
- Users can provide custom error pages
Cons of refactoring
- Requires users to add
rescue_fromblocks if they want custom handling - Default Rails error pages might be less helpful than current messages
- Need to create custom exception classes
Recommended approach
Create custom exception classes and raise them instead of rendering:
# lib/bunko/errors.rb
module Bunko
class Error < StandardError; end
class PostTypeNotFoundError < Error; end
class CollectionNotFoundError < Error; end
end
# In collection.rb
unless @post_type
raise Bunko::PostTypeNotFoundError,
"PostType '#{@collection_name}' not found. Run: rails bunko:setup[#{@collection_name}]"
endUsers can then handle these in ApplicationController:
rescue_from Bunko::PostTypeNotFoundError, with: :render_404
rescue_from Bunko::CollectionNotFoundError, with: :render_404Or let Rails handle them with default 500 pages. Could also provide a generator for adding common rescue_from handlers.
References
lib/bunko/controllers/collection.rb:51-53lib/bunko/controllers/collection.rb:65-67lib/bunko/controllers/collection.rb:86-88lib/bunko/controllers/collection.rb:100-102lib/bunko/controllers/collection.rb:107-109
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
refactorStuff we should improveStuff we should improve