gpt4 book ai didi

ruby-on-rails - Rails 4.0 的自定义错误处理

转载 作者:行者123 更新时间:2023-12-03 14:32:55 26 4
gpt4 key购买 nike

我正在使用 Ruby 2.0 和 Rails 4.0 构建一个 Ruby on Rails api。我的应用程序几乎完全是一个 JSON API,因此如果发生错误 (500, 404),我想捕获该错误并返回格式良好的 JSON 错误消息。

我试过 this并且:

rescue_from ActionController::RoutingError, :with => :error_render_method

def error_render_method
puts "HANDLING ERROR"
render :json => { :errors => "Method not found." }, :status => :not_found
true
end

在我的应用程序 Controller 中。

这些都不起作用(根本没有捕获异常)。我的谷歌搜索显示这在 3.1 和 3.2 之间发生了很大变化,我找不到关于如何在 Rails 4.0 中执行此操作的任何好的文档。

有人知道吗?

编辑
这是我转到 404 页面时的堆栈跟踪:
Started GET "/testing" for 127.0.0.1 at 2013-08-21 09:50:42 -0400

ActionController::RoutingError (No route matches [GET] "/testing"):
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.9ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (0.9ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.1ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (38.3ms)

我不认为我希望它走这么远,应该有什么东西可以捕获它并返回适当的 json 错误响应。

最佳答案

该请求甚至没有到达您的应用程序。

您需要定义一个包罗万象的路由,以便 Rails 将请求发送到您的应用程序,而不是显示错误(开发中)或呈现 public/404.html 页面(生产中)

修改您的 routes.rb 文件以包含以下内容

match "*path", to: "errors#catch_404", via: :all

在你的 Controller 中
class ErrorsController < ApplicationController

def catch_404
raise ActionController::RoutingError.new(params[:path])
end
end

还有您的 rescue_from那么应该捕获错误。

关于ruby-on-rails - Rails 4.0 的自定义错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18359088/

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