rails-error-handling
Installation
SKILL.md
Rails Error Handling
Validation Errors
- Use
errors.add(:field, "message")to attach custom messages in validations - Use
errors.full_messagesfor user display;errors.detailsfor API/machine-readable output - Prefer
ActiveModel::Validatororvalidate :method_nameover inline logic - Always write clear, user-friendly validation messages
Exceptions & Rescue
- Never rescue generic
StandardErrorunless you re-raise it - Use
rescue_frominApplicationControllerfor domain-specific errors (e.g.,ActiveRecord::RecordNotFound) - Raise custom exceptions as subclasses of
StandardError; include context and error codes - Custom error hierarchy:
ApplicationError < StandardError, thenAuthenticationError,AuthorizationError,ValidationError,ExternalServiceError - Avoid silent failures — always log unexpected exceptions with context
- Set the correct HTTP status code whenever you render an error response