Ruby-on-rails 如何在 Rails 中引发异常,使其表现得像其他 Rails 异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1918373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:02:49  来源:igfitidea点击:

How do I raise an exception in Rails so it behaves like other Rails exceptions?

ruby-on-railsexceptionexception-handling

提问by Chirag Patel

I would like to raise an exception so that it does the same thing a normal Rails exception does. Specially, show the exception and stack trace in development mode and show "We're sorry, but something went wrong" page in production mode.

我想引发一个异常,以便它执行与正常 Rails 异常相同的操作。特别是,在开发模式下显示异常和堆栈跟踪,并在生产模式下显示“我们很抱歉,但出了点问题”页面。

I tried the following:

我尝试了以下方法:

raise "safety_care group missing!" if group.nil?

But it simply writes "ERROR signing up, group missing!"to the development.log file

但它只是写入"ERROR signing up, group missing!"development.log 文件

回答by levinalex

You don't have to do anything special, it should just be working.

你不必做任何特别的事情,它应该只是工作。

When I have a fresh rails app with this controller:

当我有一个带有这个控制器的新 Rails 应用程序时:

class FooController < ApplicationController
  def index
    raise "error"
  end
end

and go to http://127.0.0.1:3000/foo/

然后去 http://127.0.0.1:3000/foo/

I am seeing the exception with a stack trace.

我看到堆栈跟踪异常。

You might not see the whole stacktrace in the console log because Rails (since 2.3) filters lines from the stack trace that come from the framework itself.

您可能不会在控制台日志中看到整个堆栈跟踪,因为 Rails(自 2.3 起)过滤了来自框架本身的堆栈跟踪中的行。

See config/initializers/backtrace_silencers.rbin your Rails project

config/initializers/backtrace_silencers.rb在您的 Rails 项目中查看

回答by halfdan

You can do it like this:

你可以这样做:

class UsersController < ApplicationController
  ## Exception Handling
  class NotActivated < StandardError
  end

  rescue_from NotActivated, :with => :not_activated

  def not_activated(exception)
    flash[:notice] = "This user is not activated."
    Event.new_event "Exception: #{exception.message}", current_user, request.remote_ip
    redirect_to "/"
  end

  def show
      // Do something that fails..
      raise NotActivated unless @user.is_activated?
  end
end

What you're doing here is creating a class "NotActivated" that will serve as Exception. Using raise, you can throw "NotActivated" as an Exception. rescue_from is the way of catching an Exception with a specified method (not_activated in this case). Quite a long example, but it should show you how it works.

你在这里做的是创建一个类“NotActivated”,它将作为异常。使用 raise,您可以将“NotActivated”作为异常抛出。rescue_from 是使用指定方法(在本例中为 not_activated)捕获异常的方法。一个很长的例子,但它应该向你展示它是如何工作的。

Best wishes,
Fabian

最好的祝福,
法比安

回答by Sambhav Sharma

If you need an easier way to do it, and don't want much fuss, a simple execution could be:

如果您需要一种更简单的方法来做到这一点,并且不想大惊小怪,一个简单的执行可能是:

raise Exception.new('something bad happened!')

This will raise an exception, say ewith e.message = something bad happened!

这将引发异常,说ee.message = something bad happened!

and then you can rescue it as you are rescuing all other exceptions in general.

然后你可以拯救它,因为你正在拯救所有其他的例外。