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
end
Problems 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_from blocks 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}]"
end
Users can then handle these in ApplicationController:
rescue_from Bunko::PostTypeNotFoundError, with: :render_404
rescue_from Bunko::CollectionNotFoundError, with: :render_404
Or 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-53
lib/bunko/controllers/collection.rb:65-67
lib/bunko/controllers/collection.rb:86-88
lib/bunko/controllers/collection.rb:100-102
lib/bunko/controllers/collection.rb:107-109
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:Problems this causes
rescue_from)Pros of refactoring
rescue_fromCons of refactoring
rescue_fromblocks if they want custom handlingRecommended approach
Create custom exception classes and raise them instead of rendering:
Users can then handle these in ApplicationController:
Or 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