问题描述
我试图从控制器中的RecordNotFound错误中救出:
def create_user_role
授权用户
角色= params [:user] [:rolify_role] .to_sym
resource_type = params [:user] [:resource_type]
resource_id = params [:user] [:id]
#捕获RecordNotFound不起作用
开始
资源=零
资源= resource_type.constantize.find(resource_id)如果RolifyRoles.available_resources.include?(resource_type)&& ; resource_id.present?
急救ActiveRecord :: RecordNotFound => e
format.html {render:show}
flash [:error] = e.message
end
end
当 resource_type.constantize.find(resource_id)
找不到记录时,它不会被记录捕获
在46毫秒内完成了500个内部服务器错误(ActiveRecord:3.2毫秒)
ActiveRecord :: RecordNotFound-找不到带有'id'= 1的RequestComment:
app / controllers / users_controller.rb:58:在`create_user_role'中
我尝试使用 StandardError
甚至甚至使用 Exception
进行救援,结果是一样。
问题出在哪里?
我还从我的模块
调用了一个方法,该方法引发了一些异常,这些异常也无法处理。 / p>
回溯:
07:23:29 web.1 |在2018-06-13 07:23:29 +0200
07:23:39 web.1开始为10.0.131.29发布POST / users / kru0096 / create_user_role由UsersController#create_user_role处理为HTML
07:23:39 web.1 |参数:{ utf8 =>✓, authenticity_token => 5kN6MuoP4YntbTvL5cTDPuCZypDdO
o1KPcvIu8dJ6otX0aYPwuWg64 / TTTgDe6DRXn6 X 6> = , resource_type => RequestComment, id => 1}, commit =>, id => kru0096}
07:23:39 web.1 |用户负载(0.3毫秒)从`users`中选择`users`.users.`id` = 1 ORDER BY`users``id'ASC LIMIT 1
07:23:39 web.1 |用户负载(0.3毫秒)从`users`中选择`users`.users.`login` =‘kru0096’LIMIT 1
07:23:39 web.1 | RequestComment加载(0.3ms)从`request_comments`中选择`request_comments`。* id = 1 LIMIT 1 LIMIT 1
07:23:39 web.1 |在41毫秒内完成500个内部服务器错误(ActiveRecord:6.5毫秒)
07:23:39 web.1 |
07:23:39 web.1 |
07:23:39 web.1 |
07:23:39 web.1 | ActiveRecord :: RecordNotFound-找不到带有 id = 1的RequestComment:
07:23:39 web.1 | app / controllers / users_controller.rb:92:in`create_user_role'
07:23:39 web.1 |
当异常被捕获时,异常处理开始工作 respond_to
块。
我也定义了自己的异常,但这对结果没有影响。
def create_user_role
授权用户
#参数初始化
的角色= params [:user] [:rolify_role ] .to_sym
resource_type = params [:user] [:resource_type]
resource_id = params [:user] [:id]
response_do | format |
begin
#将资源作为类的实例或设置为nil
resource = RolifyRoles.build_resource(resource_type,resource_id)
如果@ user.add_role角色,资源
format.html {redirect_to @user,注意:角色#{role}已成功添加。 }
format.json {渲染:show,状态::已创建,位置:@user}
否则
format.html {渲染:show}
format.json {渲染json :@ user.errors,状态::unprocessable_entity}
end
#捕获自定义异常
rescue RolifyRolesException => e
flash [:error] = e.message
flash.keep
format.html {render:show,flash:{error:e.message}}}
format.json {render json:e.message,status::unprocessable_entity}
结束
结束
结束
I've tried to rescue from RecordNotFound error in controller like this:
def create_user_role
authorize User
role = params[:user][:rolify_role].to_sym
resource_type = params[:user][:resource_type]
resource_id = params[:user][:id]
# Catch RecordNotFound doesn't work
begin
resource = nil
resource = resource_type.constantize.find(resource_id) if RolifyRoles.available_resources.include?(resource_type) && resource_id.present?
rescue ActiveRecord::RecordNotFound => e
format.html { render :show }
flash[:error] = e.message
end
end
And when the resource_type.constantize.find(resource_id)
cannot find the record, its doesn't caught by the rescue block.
Completed 500 Internal Server Error in 46ms (ActiveRecord: 3.2ms)
ActiveRecord::RecordNotFound - Couldn't find RequestComment with 'id'=1:
app/controllers/users_controller.rb:58:in `create_user_role'
I've tried to rescue with StandardError
or even with Exception
and result was the same.
Where is the problem?
I am also calling a method from my module
which raise some exceptions, which also aren't handled.
Backtrace:
07:23:29 web.1 | Started POST "/users/kru0096/create_user_role" for 10.0.131.29 at 2018-06-13 07:23:29 +0200
07:23:39 web.1 | Processing by UsersController#create_user_role as HTML
07:23:39 web.1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"5kN6MuoP4YntbTvL5cTDPuCZypDdO
o1KPcvIu8dJ6otX0aYPwuWg64/TTTgDe6DRXn6wCs1KvgT9xjkr9g/dyA==", "user"=>{"rolify_role"=>"hoac", "resource_type"=>"RequestComment", "id"=>"1"}, "commit"=>"", "id"=>"kru0096"}
07:23:39 web.1 | User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
07:23:39 web.1 | User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`login` = 'kru0096' LIMIT 1
07:23:39 web.1 | RequestComment Load (0.3ms) SELECT `request_comments`.* FROM `request_comments` WHERE `request_comments`.`id` = 1 LIMIT 1
07:23:39 web.1 | Completed 500 Internal Server Error in 41ms (ActiveRecord: 6.5ms)
07:23:39 web.1 |
07:23:39 web.1 |
07:23:39 web.1 |
07:23:39 web.1 | ActiveRecord::RecordNotFound - Couldn't find RequestComment with 'id'=1:
07:23:39 web.1 | app/controllers/users_controller.rb:92:in `create_user_role'
07:23:39 web.1 |
Exception handling starts working when the exception is caught at the respond_to
block.
I also defined my own exception, but this has no effect on the result.
def create_user_role
authorize User
# Params Inicialization
role = params[:user][:rolify_role].to_sym
resource_type = params[:user][:resource_type]
resource_id = params[:user][:id]
respond_to do |format|
begin
# Make resource as instance of class or set to nil
resource = RolifyRoles.build_resource(resource_type, resource_id)
if @user.add_role role, resource
format.html { redirect_to @user, notice: "Role #{role} was successfully added." }
format.json { render :show, status: :created, location: @user }
else
format.html { render :show }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
# Catch self defined exceptions
rescue RolifyRolesException => e
flash[:error] = e.message
flash.keep
format.html { render :show, flash: { error: e.message } }
format.json { render json: e.message, status: :unprocessable_entity }
end
end
end
这篇关于为什么控制器没有捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!