Skip to content

Commit

Permalink
#170 Add parser error file to default tmp folder if anything goes wro…
Browse files Browse the repository at this point in the history
…ng processing a config file
  • Loading branch information
ryanmelt committed Jul 17, 2015
1 parent a828892 commit 28e830c
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/cosmos/config/config_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,13 @@ def parse_file(filename,
remove_quotes = true,
&block)
@filename = filename
# Create a temp file where we can write the ERB parsed output
file = Tempfile.new("parsed_#{File.basename(filename)}")
file.write(ERB.new(File.read(@filename)).result(binding))
file.rewind
begin
# Create a temp file where we can write the ERB parsed output
file = Tempfile.new("parsed_#{File.basename(filename)}")
unparsed_data = File.read(@filename)
file.write(ERB.new(unparsed_data).result(binding))
file.rewind

size = file.stat.size.to_f

# Callbacks for beginning of parsing
Expand All @@ -189,6 +191,28 @@ def parse_file(filename,
size,
PARSING_REGEX,
&block)
rescue Exception => e
# If we had an error parsing write out the parsed results for debugging
default_tmp_folder = File.join(Cosmos::USERPATH, 'outputs', 'tmp')
if File.exist?(default_tmp_folder)
begin
File.open(File.join(default_tmp_folder, "parser_error_#{File.basename(filename)}"), 'w') do |save_file|
save_file.puts e.formatted
save_file.puts "\nParsed Data (will only be present if parse ran successfully):"
save_file.puts
if defined? file
file.rewind
save_file.puts file.read
end
save_file.puts "\nUnparsed Data:"
save_file.puts
save_file.puts unparsed_data if defined? unparsed_data
end
rescue
# Oh well - we tried
end
end
raise e
ensure
file.close unless file.closed?
end
Expand Down

0 comments on commit 28e830c

Please sign in to comment.