Mateusz Urbański
Mateusz Urbański

Reputation: 7862

No such middleware to insert before: ActionDispatch::Http::Parameters (RuntimeError)

In my Rails 5.2 app I have the following middleware inside app/middleware folder:

class CatchJsonParseErrors
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call(env)
    rescue ActionDispatch::Http::Parameters::ParseError => error
      raise error unless env['HTTP_ACCEPT']&.match(/application\/json/)

      error_output = "There was a problem in the JSON you submitted: #{error}"
      return [
        400, { "Content-Type" => "application/json" },
        [{ status: 400, error: error_output }.to_json]
      ]
    end
  end
end

In my application.rb file I have the following line:

config.middleware.insert_before(ActionDispatch::Http::Parameters, "CatchJsonParseErrors")

When I try to start my app It returns following error:

`assert_index': No such middleware to insert before: ActionDispatch::Http::Parameters (RuntimeError)

How can I fix that?

Upvotes: 2

Views: 1195

Answers (1)

Tensho
Tensho

Reputation: 893

I really don't see ActionDispatch::Http::Parameters middleware at official Rails Guide or GitHub rails repo. There is a module ActionDispatch::Http::Parameters and it's just included to ActionDispatch::Request class. I guess as far as Rails (ActionDispatch) request object is ready you are free to verify/adjust it's params, and seems like this place is right after ActionDispatch::RequestId middleware.

Upvotes: 1

Related Questions