lib/figly.rb in figly-1.0.2 vs lib/figly.rb in figly-1.0.3
- old
+ new
@@ -1,31 +1,34 @@
require "figly/version"
require "figly/settings"
module Figly
class ParserError < StandardError; end
- class UnsupportedError < StandardError; end
+ class UnsupportedFormatError < StandardError; end
+ class ConfigNotFoundError < StandardError; end
def self.load_file(path)
- raise "File does not exist: #{path}" unless File.exists?(path)
+ raise ConfigNotFoundError, "File does not exist: #{path}" unless File.exists?(path)
ext = File.extname(path)
data = case ext
when '.toml'
begin
require 'toml'
# HACK: TOML captures Parslet errors and puts them so they get swallowed
# here we redirect stdout to an IO buffer that we can read from and test
# that the value doesn't match an error
old_stdout = $stdout
$stdout = StringIO.new('','w')
- d = TOML.load_file(path)
- cap = $stdout.string
- raise ParserError, cap if cap =~ /^Failed to match/
- $stdout = old_stdout
- d
+ TOML.load_file(path).tap do |d|
+ cap = $stdout.string
+ raise ParserError, cap if cap =~ /^Failed to match/
+ end
rescue Exception => e
raise ParserError, e.message
+ ensure
+ # Make sure to reset the old stdout even if an exception is thrown
+ $stdout = old_stdout
end
when '.yml'
begin
require 'yaml'
YAML.load_file(path)
@@ -38,10 +41,10 @@
JSON.parse(File.read(path))
rescue Exception => e
raise ParserError, e.message
end
else
- raise UnsupportedError, "Unsupported file extension (#{ext})"
+ raise UnsupportedFormatError, "Unsupported file extension (#{ext})"
end
# Here we merge config files if there are multiple load calls
if defined?(@@data) && !@@data.nil?
_deep_merge(@@data, data)