module ApiErrorHandler
extend ActiveSupport::Concern
included do
rescue_from StandardError, with: :handle_standard_error
rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
rescue_from ActiveRecord::RecordInvalid, with: :handle_validation_error
rescue_from Api::UnauthorizedError, with: :handle_unauthorized
end
private
def handle_standard_error(exception)
Rails.logger.error("#{exception.class}: #{exception.message}\n#{exception.backtrace.join("\n")}")
render json: {
error: 'INTERNAL_SERVER_ERROR',
message: 'An unexpected error occurred',
request_id: request.request_id
}, status: :internal_server_error
end
def handle_not_found(exception)
render json: {
error: 'NOT_FOUND',
message: exception.message,
request_id: request.request_id
}, status: :not_found
end
def handle_validation_error(exception)
render json: {
error: 'VALIDATION_ERROR',
message: 'Validation failed',
details: exception.record.errors.as_json,
request_id: request.request_id
}, status: :unprocessable_entity
end
def handle_unauthorized(exception)
render json: {
error: 'UNAUTHORIZED',
message: exception.message,
request_id: request.request_id
}, status: :unauthorized
end
end
Consistent error handling transforms debugging from guesswork into systematic troubleshooting. I use a rescue handler that catches exceptions globally and transforms them into a standard JSON structure containing an error code, human-readable message, and optional details hash for validation failures. This pattern ensures every API response follows the same contract regardless of which layer raised the exception. I also include a request_id in error responses so clients can reference specific failures when reporting issues. For production systems, I log the full exception with backtrace server-side while returning sanitized messages to clients to avoid leaking implementation details. Custom exception classes like Api::UnauthorizedError map to specific HTTP status codes.
Related snips
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
payload = {
sub: user.id,
iss: 'https://auth.example.com',
aud: 'codesnips-api',
exp: 15.minutes.from_now.to_i,
iat: Time.now.to_i,
JWT issuance and verification without common footguns
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
Share this code
Here's the card — post it anywhere.