Kip*_*Kip 12 ruby ruby-on-rails exception params rescue
Rails 4添加了一个异常ActionDispatch :: ParamsParser :: ParseError异常,但由于它在中间件堆栈中,它似乎无法在普通控制器环境中获救.在json API应用程序中,我想用标准错误格式进行响应.
这个要点显示了插入中间件以拦截和响应的策略.遵循这种模式我有:
application.rb中:
module Traphos
class Application < Rails::Application
....
config.middleware.insert_before ActionDispatch::ParamsParser, "JSONParseError"
end
end
Run Code Online (Sandbox Code Playgroud)
中间件是:
class JSONParseError
def initialize(app)
@app = app
end
def call(env)
begin
@app.call(env)
rescue ActionDispatch::ParamsParser::ParseError => e
[422, {}, ['Parse Error']]
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果我在没有中间件的情况下运行测试,我会得到(spec):
Failures:
1) Photo update attributes with non-parseable json
Failure/Error: patch update_url, {:description => description}, "CONTENT_TYPE" => content_type, "HTTP_ACCEPT" => accepts, "HTTP_AUTHORIZATION" => @auth
ActionDispatch::ParamsParser::ParseError:
399: unexpected token at 'description=Test+New+Description]'
Run Code Online (Sandbox Code Playgroud)
这正是我所期望的(ParseError,我无法rescue_from).
现在只需在上面的中间件中添加更改:
2) Photo update attributes with non-parseable json
Failure/Error: response.status.should eql(422)
expected: 422
got: 200
Run Code Online (Sandbox Code Playgroud)
并且日志显示正在执行标准控制器操作并返回正常响应(尽管因为它没有接收任何参数而没有更新任何内容).
我的问题:
如何从ParseError中解救并返回自定义响应.感觉就像我在正确的轨道,但不是那里.
我无法弄清楚为什么,当异常被提出并获救时,控制器动作仍然继续进行.
非常感谢, - Kip
事实证明,在中间件堆栈中,可以使用例外应用程序配置ActionDispatch :: ShowExceptions.
module Traphos
class Application < Rails::Application
# For the exceptions app
require "#{config.root}/lib/exceptions/public_exceptions"
config.exceptions_app = Traphos::PublicExceptions.new(Rails.public_path)
end
end
Run Code Online (Sandbox Code Playgroud)
基于Rails,我现在使用的是:
module Traphos
class PublicExceptions
attr_accessor :public_path
def initialize(public_path)
@public_path = public_path
end
def call(env)
exception = env["action_dispatch.exception"]
status = code_from_exception(env["PATH_INFO"][1..-1], exception)
request = ActionDispatch::Request.new(env)
content_type = request.formats.first
body = {:status => { :code => status, :exception => exception.class.name, :message => exception.message }}
render(status, content_type, body)
end
private
def render(status, content_type, body)
format = content_type && "to_#{content_type.to_sym}"
if format && body.respond_to?(format)
render_format(status, content_type, body.public_send(format))
else
render_html(status)
end
end
def render_format(status, content_type, body)
[status, {'Content-Type' => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
'Content-Length' => body.bytesize.to_s}, [body]]
end
def render_html(status)
found = false
path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
path = "#{public_path}/#{status}.html" unless path && (found = File.exist?(path))
if found || File.exist?(path)
render_format(status, 'text/html', File.read(path))
else
[404, { "X-Cascade" => "pass" }, []]
end
end
def code_from_exception(status, exception)
case exception
when ActionDispatch::ParamsParser::ParseError
"422"
else
status
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
要在测试环境中使用它,需要设置配置变量(否则您将在开发和测试中获得标准异常处理).所以测试我有(编辑到只有关键部分):
describe Photo, :type => :api do
context 'update' do
it 'attributes with non-parseable json' do
Rails.application.config.consider_all_requests_local = false
Rails.application.config.action_dispatch.show_exceptions = true
patch update_url, {:description => description}
response.status.should eql(422)
result = JSON.parse(response.body)
result['status']['exception'].should match(/ParseError/)
Rails.application.config.consider_all_requests_local = true
Rails.application.config.action_dispatch.show_exceptions = false
end
end
end
Run Code Online (Sandbox Code Playgroud)
它以我需要的公共API方式执行,并且适用于我可能选择自定义的任何其他异常.