在rails中自定义消息或重定向400错误请求(因为params上的未显示%)

时间:2022-06-23 21:59:31

I got 400 Bad Request if i request

如果我要求,我收到400 Bad Request

http://localhost:3000/cont/act?params=%

On the log it only show

在日志上它只显示

=> Booting Thin
=> Rails 3.2.14 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[Zonebie] Setting timezone: ZONEBIE_TZ="Tokelau Is."
dalton
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
!! Invalid request

How can i make custom message or redirect for this kind of error ?

如何为此类错误制作自定义消息或重定向?

P.S

P.S

I've try using few different middleware solution on google.

我尝试在谷歌上使用几个不同的中间件解决方案。

Like adding

喜欢添加

class InvalidUriCatch
  puts "Hello"

  def initialize(app)
    @app = app
  end

  def call(env)
    puts "hello"

    query = Rack::Utils.parse_nested_query(env['QUERY_STRING'].to_s) rescue :bad_query
    if query == :bad_query
      [302, {'Location' => 'google.com'}, "<a href=\"google.com\">a</a>"]
    else
      @app.call(env)
    end
  end
end

on my libs and put

在我的库和放

config.middleware.insert_before ActionDispatch::Static, "InvalidUriCatch"

on application.rb

在application.rb上

But still, doesn't work

但是,仍然不起作用

it didn't even output hello on rails s log when i request it.

当我请求它时,它甚至没有在rails s log上输出hello。

1 个解决方案

#1


0  

My code doesn't work because i'm using Thin on my development (My application using passenger on production. ).

我的代码不起作用,因为我在开发中使用Thin(我的应用程序使用乘客进行生产)。

That's why on production it show error with backtrace while on local it doesn't.

这就是为什么在生产中它显示错误与回溯,而在本地它没有。

#1


0  

My code doesn't work because i'm using Thin on my development (My application using passenger on production. ).

我的代码不起作用,因为我在开发中使用Thin(我的应用程序使用乘客进行生产)。

That's why on production it show error with backtrace while on local it doesn't.

这就是为什么在生产中它显示错误与回溯,而在本地它没有。