在Rails中通过Actionmailer发送电子邮件时,它会记录如下内容:
Sent mail to example@example.com (72ms)
Rendered mailer/_header.html.erb (0.0ms)
...
Run Code Online (Sandbox Code Playgroud)
我想过滤日志ala参数过滤的电子邮件
Sent mail to [FILTERED] (72ms)
Rendered mailer/_header.html.erb (0.0ms)
...
Run Code Online (Sandbox Code Playgroud)
有干净的方法吗?或者,不记录整个第一行就没问题.
begin
ActiveRecord::Base.transaction do
// ...
sanitized_sql = "INSERT INTO pinfo ..."
ActiveRecord::Base.connection.execute(sanitized_sql)
end
rescue
// how can I get the error?
end
Run Code Online (Sandbox Code Playgroud)
在webrick控制台中,错误(1967-07-16?00:00:00)显示为:
EXECUTE(0.0ms)ODBC :: Error:22008(241)[unixODBC] [FreeTDS] [SQL Server]语法错误将日期时间从字符串转换为。:INSERT INTO pinfo(birthdate)VALUES('1967-07-16?00: 00:00')EXECUTE(0.8ms)IF @@ TRANCOUNT> 0回滚交易
如何ODBC::Error: 22008 (241) ...从ActiveRecord::Base.connection.executein中引发上述错误消息()rescue?
我在我的一个模型中有以下方法来保存用户记录:
def save_user(params)
begin
save_user_details(params)
rescue ActiveRecord::RecordInvalid => ex
{ success: false, errors: ex.messages }
rescue Exception => ex
Rails.logger.info(ex.message)
Rails.logger.info(ex.backtrace.join(‘\n’)
{ success: false, errors: ’Some error occurred.}
end
end
Run Code Online (Sandbox Code Playgroud)
我们可以看到rescue块很重,这种块在其他行为中也很常见.所以我考虑重构这个并将rescue块移动到一个单独的函数.我想实现这样的事情:
def save_user(params)
begin
save_user_details(params) # my method to save the details
handle_the_exception # not sure how to implement this
end
def handle_the_exception
# How to handle here ?
end
Run Code Online (Sandbox Code Playgroud)
如上所述对实施的任何想法都将是一个很大的帮助.
我正在学习 rails 并且对一些基础知识感到困惑。这是我的 API 方法:
def itunes_app_create
begin
app = Spaceship::Tunes::Application.create!(name: params[:itunes_app_name],
primary_language: "English",
version: params[:itunes_app_version],
sku: params[:itunes_app_sku],
bundle_id: params[:itunes_app_bundle_id],
team_id: params[:itunes_team_id])
render json: app.to_json, status: 200
rescue
render json: app.errors.full_messages.to_json, status: 200
end
end
Run Code Online (Sandbox Code Playgroud)
我的 app.errors.full_messages.to_json线路失败了,因为好吧,我只是从我看到的东西中弥补了这一点。如何返回导致方法失败的消息?
不确定它是否重要,app它不是我模型的一部分。我只需要从我的客户端应用程序调用它,然后发回结果。
作为一个附带问题,在这种情况下,我应该返回什么状态并显示错误?
我们如何捕获或/和处理ruby中所有未处理的异常?
例如,这样做的动机可能是将不同的文件记录到不同的文件或发送和发送电子邮件到系统管理.
在Java中我们会这样做
Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler ex);
Run Code Online (Sandbox Code Playgroud)
在NodeJS中
process.on('uncaughtException', function(error) {
/*code*/
});
Run Code Online (Sandbox Code Playgroud)
在PHP中
register_shutdown_function('errorHandler');
function errorHandler() {
$error = error_get_last();
/*code*/
}
Run Code Online (Sandbox Code Playgroud)
我们怎么能用红宝石做到这一点?
我想在我的应用程序中出现异常时发送电子邮件并呈现常规500页.我找不到如何执行500页渲染:
class ApplicationController < ActionController::Base
rescue_from StandardError do
send_email_of_error
# what goes here?
end
...
end
Run Code Online (Sandbox Code Playgroud) 我想检查字符串是否有效YAML.我想在我的Ruby代码中使用gem或库执行此操作.我只有这个开始/救援条款,但它没有得到适当的救援:
def valid_yaml_string?(config_text)
require 'open-uri'
file = open("https://github.com/TheNotary/the_notarys_linux_mint_postinstall_configuration")
hard_failing_bad_yaml = file.read
config_text = hard_failing_bad_yaml
begin
YAML.load config_text
return true
rescue
return false
end
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,我遇到了可怕的错误:
irb(main):089:0> valid_yaml_string?("b")
Psych::SyntaxError: (<unknown>): mapping values are not allowed in this context at line 6 column 19
from /home/kentos/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/psych.rb:203:in `parse'
from /home/kentos/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/psych.rb:203:in `parse_stream'
from /home/kentos/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/psych.rb:151:in `parse'
from /home/kentos/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/psych.rb:127:in `load'
from (irb):83:in `valid_yaml_string?'
from (irb):89
from /home/kentos/.rvm/rubies/ruby-1.9.3-p374/bin/irb:12:in `<main>'
Run Code Online (Sandbox Code Playgroud) 使用||运算符和rescueRuby 之间有什么区别吗?
说:
b = A.value || "5"
b = A.value rescue 5
Run Code Online (Sandbox Code Playgroud)
对象A没有value方法的地方.
有时在处理API响应时,我最终会写一些类似于:
what_i_need = response["key"]["another key"]["another key 2"]
Run Code Online (Sandbox Code Playgroud)
问题是,如果"another key"丢失,它会抛出一个错误.我不喜欢那样.如果我是一个很多快乐what_i_need翻了nil,如果沿着过程的东西坏了.
是否有比以下更优雅的解决方案:
what_i_need = nil
begin
what_i_need = response["key"]["another key"]["another key 2"]
rescue Exception => e
end
Run Code Online (Sandbox Code Playgroud)
我还考虑过猴子修补NilClass你尝试访问nil["something"]它会返回nil,但我不确定这是否是最好的方式来解决它,如果它甚至可能.
ruby ×6
exception ×2
actionmailer ×1
activerecord ×1
block ×1
fastlane ×1
hash ×1
logging ×1
yaml ×1