0

I am trying to use an exception handler in my controller but it doesn't seem to be doing anything:

class API::PaymentsController < ApplicationController
    before_filter :set_client

    def index
        @foo = @client.foo
    end

    ...

    def set_client

      rescue ActiveRecord::RecordNotFound do |e|
        render json: e.message, status: :not_found
      end

      @client = Client.find(params[:client_id])
    end
end

and when I go to localhost:3000/clients/[invalid_id]/payments, I still see the error:

ActiveRecord::RecordNotFound (Couldn't find Client with 'id'=1):
app/controllers/payments_controller.rb:58:in `set_client'

when I'd expect the RecordNotFound handler to handle this exception. What am I missing?

1 Answer 1

1

The rescue block comes after the failure.

def set_client
  @client = Client.find(params[:client_id])
rescue ActiveRecord::RecordNotFound => err
  render json: { message: err.message}, status: :not_found
end
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much. I also added an unnecessary end after the rescue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.