如何在 Rails 中捕获 500 内部服务器错误 [英] How to catch a 500 Internal Server Error in Rails

查看:21
本文介绍了如何在 Rails 中捕获 500 内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在从数据库等中获取东西时已经做过很多次了.

对于我的具体情况,我使用的是第三方 连接到一个硬件......无论如何,在出现错误的情况下,例如无效id显然,我们想要引发异常或救援......但不幸的是我不知道如何提高它,因为当它被击中时,为时已晚(我认为)

这里...

<预><代码>## 获取参数并保存上面的项目...#如果 item.savedevice = RubySpark::Device.new("FAKEUNITID800")device.function("req", "ITEM")redirect_to 控制器:'items',动作:'edit_items'结尾

如果这是一个有效的 ID,那么一切都会正常进行,它会将您带到 /edit 页面!但问题是,如果 ID 无效,它只会...

在 897 毫秒内完成 500 内部服务器错误RubySpark::Device::ApiError - 权限被拒绝:设备 ID 无效:

我查看了以下教程

但老实说,它们只会让我更加困惑.也许我对此有错误的方法.我一直认为首先你提出请求,然后你有一个后备案例,这取决于你得到什么状态(即 200、500、404)......你从那里开始.

解决方案

Rails 返回 500 Internal Server Error 响应,因为引发了一个无法处理的异常.您无法在 Rails 中挽救500 Internal Server Error",因为它不是异常 - 它的框架从未捕获的异常中退出,以避免数据丢失或不可预测的行为.

幸运的是,您不必这样做.你可以拯救 RubySpark 异常:

开始device = RubySpark::Device.new("FAKEUNITID800")device.function("req", "ITEM")救援 RubySpark::Device::ApiError =>电子logger.error(e.message)结尾

您也可以使用 rescue_from 在将整个操作包装在 before 块中的 Rails 控制器中:

class FooController <应用控制器来自 RubySpark::Device::ApiError 的rescue_from,带有::do_something# ...结尾

I have done this tons of times before when fetching things from a database, etc.

For my specific case I am using a 3rd party to connect to a piece of hardware... Anyways, in the case of an error, such as an invalid id obviously, we want to raise a exception or a rescue... but unfortunately I don't know how to raise it because by the time it is hit, it's too late (I think)

Here...

# 
# getting params and saving item above...
# 

if item.save
  device = RubySpark::Device.new("FAKEUNITID800")
  device.function("req", "ITEM")
  redirect_to controller: 'items', action: 'edit_items'
end

If this was a valid ID, everything would work, and it would take you to the /edit page! But the issue is, with an invalid ID, it just does...

Completed 500 Internal Server Error in 897ms

RubySpark::Device::ApiError - Permission Denied: Invalid Device ID:

I checked out the following tutorials

But honestly, they just make me more confused. Maybe I have the wrong approach to this. I always thought that first you make the request, and then you have a fall back case, depending what status (ie. 200, 500, 404) you get... you go from there.

解决方案

Rails returns an 500 Internal Server Error response because an exception was raised that it does not no how to handle. You can't rescue "500 Internal Server Error" in Rails because it is not an exception - its the framework bailing from an uncaught exception to avoid data loss or unpredictable behavior.

Fortunatly you don't have to. You can just rescue the RubySpark exception:

begin
  device = RubySpark::Device.new("FAKEUNITID800")
  device.function("req", "ITEM")
rescue RubySpark::Device::ApiError => e
  logger.error(e.message)
end

You can also use rescue_from in Rails controllers that wraps the entire action in a before block:

class FooController < ApplicationCotnroller
  rescue_from RubySpark::Device::ApiError, with: :do_something
  # ...
end

这篇关于如何在 Rails 中捕获 500 内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆