5

In Grape if you use the error! method it will throw an error and never call the Grape::Endpoint "after" callback.

I'd like the app to call an after hook when error! has been called.

I've added this piece of middleware to make this happen.

class AfterFailure <  Grape::Middleware::Base

  def call!(env)
    @env = env
    before

    error = catch (:error) do 
      @app_response = @app.call(@env)
      return after || @app_response
    end

    after_failure(error) || throw(:error, error)
  end

  def after_failure(error)
    puts "After Failure"

    nil
  end

end

Is there a better way to accomplish this?

Thanks

jadent
  • 3,674
  • 5
  • 27
  • 32
  • the above code will work and it's certainly not a bad implementation. I was curious if there is a better way to do this. I would figure Grape would have a better event lifecycle but it doesn't – jadent Aug 19 '14 at 12:34

1 Answers1

0

just insert your middleware before Grape::Middleware::Error

class YourApi < Grape::API
  insert_before Grape::Middleware, AfterFailure

  # your api code...
end

then you can get the error response from @app_response