# Order Matters For rescue\_from Blocks

In a Rails controller, you can declare any number of [`rescue_from` blocks](https://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html) for capturing and responding to execeptions that are raised by your application.

```ruby
class BooksController < BaseController

  rescue_from ForbiddenAction do |e|
    render json: { error: e.message }.to_json, status: 403
  end

  rescue_from StandardError do |e|
    render json: { error: e.message }.to_json, status: 500
  end

  def index
    # ...

    raise ForbiddenAction, "Which rescue_from is this going to hit?"
  end
end
```

The potential problem with above is the ordering of the two `rescue_from` blocks. Assume that `ForbiddenAction` is a subclass of the `StandardError` class -- this is likely the case for exceptions you declare in your app. The top `rescue_from` will never get hit because everything that subclasses `StandardError` will be trapped by the bottom `rescue_from`.

These `rescue_from` blocks are applied bottom-up. That means you have to consider the class hierarchy when structuring your code. In the above code example, if we flip the two of them around, we will then get what we are expecting.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/order-matters-for-rescue-from-blocks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
