Error Handling
Important: These docs are for the outdated Jets 5 versions and below. For the latest Jets docs: docs.rubyonjets.com
Jets provides some error handling capabilities in controllers that can rescue errors that occur during the callbacks or action. This is done with the rescue_from
method.
Example rescue_from
class PostsController < ApplicationController
rescue_from ActiveRecord::RecordNotFound do |exception|
render json: { message: "We could not find your post." }, status: 404
end
# ...
end
Example using with
association
class PostsController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :missing_post
# ...
private
def missing_post
render json: { message: "We could not find your post." }, status: 404
end
end