class Api::BaseController < ActionController::API
rescue_from ActiveRecord::RecordNotFound do |e|
render_problem(status: 404, type: 'not_found', title: 'Not Found', detail: e.message)
end
rescue_from ActionController::ParameterMissing do |e|
render_problem(status: 422, type: 'invalid_request', title: 'Invalid Request', detail: e.message)
end
private
def render_problem(status:, type:, title:, detail: nil, extra: {})
render status: status, json: {
type: type,
title: title,
status: status,
detail: detail,
request_id: request.request_id
}.merge(extra)
end
end
APIs are easier to operate when errors are structured and consistent. Wrap errors into a problem-details style response with a stable type and request_id so support can quickly trace issues.