gpt4 book ai didi

ruby-on-rails-3 - 异常通知程序 - 如何显示自己的错误页面?

转载 作者:行者123 更新时间:2023-12-04 06:57:51 25 4
gpt4 key购买 nike

我使用 exception_notification gem 来处理应用中的错误。我的 ApplicationController 看起来像这样:

unless Rails.application.config.consider_all_requests_local
rescue_from Exception,
:with => :render_error
rescue_from ActiveRecord::RecordNotFound,
:with => :render_not_found
rescue_from ActionController::RoutingError,
:with => :render_not_found
rescue_from ActionController::UnknownController,
:with => :render_not_found
rescue_from ActionController::UnknownAction,
:with => :render_not_found
end

def render_not_found(exception)
ExceptionNotifier::Notifier
.exception_notification(request.env, exception)
.deliver
render :template => "/errors/404.html.erb",
:layout => 'errors.html.erb'
return
end

def render_error(exception)
ExceptionNotifier::Notifier
.exception_notification(request.env, exception)
.deliver
render :template => "/errors/500.html.erb",
:layout => 'errors.html.erb'
return
end

在文件末尾的 /config/enviroments/productions.rg 中:

config.middleware.use ExceptionNotifier,
:email_prefix => "[MY APP| Error Report] ",
:sender_address => %{"MY APP" <err@my-app.com>},
:exception_recipients => 'my_email@gmail.com'
end

当我在应用程序上收到错误时 - 例如。 Article.find(not-existing-ID),我将从 /public/500.html 而不是从指定的文件中获取标准错误页面 (ERROR 500)在应用程序 Controller 中......这怎么可能?过去几个小时我试图找出问题所在,但我仍然不知道问题所在。

最佳答案

这对我有用——来自 application_controller.rb:

unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: :render_500
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ActionController::UnknownAction, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end

  private
def render_404(exception)
ExceptionNotifier::Notifier.exception_notification(request.env, exception,
:data => {:User => current_user.full_name, :Email => current_user.email, :UserID => current_user.id}).deliver
@not_found_path = exception.message
respond_to do |format|
format.html { render template: 'pages/404', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404}
end
end
def render_500(exception)
ExceptionNotifier::Notifier.exception_notification(request.env, exception,
:data => {:User => current_user.full_name, :Email => current_user.email, :UserID => current_user.id}).deliver
@error = exception
respond_to do |format|
format.html { render template: 'pages/500', layout: 'layouts/application', status: 500 }
format.all { render nothing: true, status: 500}
end
end

注意:我在/app/views/pages 中有我的自定义错误页面。调用了 500.html.haml 和 404.html.haml

我的 routes.rb 中也有这个(请注意在“页面而不是正斜杠”之后的 散列):

  unless Rails.application.config.consider_all_requests_local
match '*not_found', to: 'pages#404'
end

附言。请在此处查看更新的说明:http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

关于ruby-on-rails-3 - 异常通知程序 - 如何显示自己的错误页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com