Error Handling

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

Pro tip: Use the <- and -> arrow keys to move back and forward.

Edit this page

See a typo or an error? You can improve this page. This website is available on GitHub, and contributions are encouraged and welcomed. We love pull requests from you!