简体   繁体   English

rails rescue_from ScriptError

[英]rails rescue_from ScriptError

According to the family tree of Exception. 根据Exception的家谱。 SyntaxError is child to ScriptError SyntaxErrorScriptErrorScriptError

I wish to handle Syntax and/or ScriptError in my rails application. 我希望在我的rails应用程序中处理语法和/或ScriptError。

Exception
    NoMemoryError
    ScriptError
        LoadError
        NotImplementedError
        SyntaxError
    SignalException
        Interrupt
    StandardError
        ArgumentError
        IOError
            EOFError
        IndexError
            StopIteration
        LocalJumpError
        NameError
            NoMethodError
        RangeError
            FloatDomainError
        RegexpError
        RuntimeError
        SecurityError
        SystemCallError
        SystemStackError
        ThreadError
        TypeError
        ZeroDivisionError
    SystemExit
    fatal

I did: 我做了:

rescue_from ScriptError, :with => :notify_on_landing_page

but didn't worked. 但是没有用。

Error raised on screen : SyntaxError in Bc::Muse::User::ProfileController#show 屏幕上出现错误: SyntaxError in Bc::Muse::User::ProfileController#show

I have created an explicit syntax error, it should gracefully rescue it and do the things I want to. 我创建了一个显式的语法错误,它应该适当地挽救它并做我想做的事情。

Unfortunately, I don't think it works that way. 不幸的是,我认为它不是那样的。

Rescuing from exceptions with rescue_from works only after creating a controller instance during request processing (see source code here and here ). 只有在请求处理期间创建了控制器实例之后,才能使用rescue_from异常(请参见此处此处的源代码)。 Your SyntaxError is probably raised much earlier - during autoloading of the given controller and dependent classes / modules. 您的SyntaxError可能提早得多-在给定控制器和相关类/模块的自动加载期间。 So unless you were trying to rescue from a syntax error of code being loaded during a controller action execution, you are out of luck, I'm afraid. 因此,恐怕除非您试图从执行控制器动作期间加载的代码的语法错误中解脱出来,否则您就不走运了。

Test: when you explicitly load a file with a syntax error in a controller action and the rescue_from will work as expected: 测试:当您在控制器操作中显式load带有语法错误的文件,并且rescue_from将按预期工作时:

class MyController < ApplicationController
  rescue_from(::SyntaxError) { Rails.logger.error "SYNTAX ERROR!" }

  def index
    load "#{Rails.root}/test.rb"
  end
end

If you save a test.rb file in the rails root and add a deliberate syntax error in it, you will see the exception is handled correctly by rescue_from and the error message will be present in the log file. 如果将test.rb文件保存在rails根目录中,并在其中添加了故意的语法错误,您将看到rescue_from正确处理了该异常,并且错误消息将出现在日志文件中。

On the other hand, if you look at the full stack trace of your SyntaxError , you will probably see that it does not even reach the ActionController methods for request processing. 另一方面,如果查看SyntaxError的完整堆栈跟踪,则可能会发现它甚至没有到达ActionController方法进行请求处理。

Thanks! 谢谢!

I added a middleware to achieve this. 我添加了一个中间件来实现这一目标。

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

  def call(env)
    @app.call(env)
  rescue SyntaxError => error

    request = Rack::Request.new(env)
    session = request.env['rack.session']
    params = request.params

    if session.try(:[], :user_object)
      ##Do validation stuff
      ...
      [302, {'Location' => '/'}, []]
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM