当i18n语言环境YAML文件中存在语法错误时,会引发Psych::SyntaxError
。在Rails启动期间遇到此异常时(例如,重新启动生产时),Rails崩溃。
答案 0 :(得分:0)
我不确定是否有办法从这个错误中恢复,但是我创建了一个rake任务,确保给定的YAML文件在语法上有效(通过预提交git钩子运行任何已更改的YAML文件):
namespace :yaml do
desc "Check YAML syntax for errors."
task :check_syntax do
require 'YAML'
require 'colorize'
puts "Checking YAML files..."
filenames = (ENV['FILENAMES'].split(',') || []).push(ENV['FILENAME']).uniq.compact
fails = 0
filenames.each do |file|
print "#{file}... "
begin
YAML.load_file(file)
rescue Psych::SyntaxError => e
fails += 1
print "Failed! ".red
print "[#{e.message.sub(/\A.*: /, '')}]"
puts
next
end
print "Success!".green
puts
end
puts
exit fails > 0 ? 1 : 0
end
end