Skip to content

Commit

Permalink
Improve fatal error handling.
Browse files Browse the repository at this point in the history
- Move JS/CSS parsing error handling up to the level where we read the
  file in, so when IO error occours we'll also catch that.

- Handle guide reading/formatting errors.

- Handle errors in formatting class docs (wasn't able to test it, but
  a user reported a stack trace with encoding error happening inside
  there - maybe a difference when running in Windows).
  • Loading branch information
nene committed Jun 26, 2012
1 parent 29feeab commit f7d81f1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
20 changes: 15 additions & 5 deletions lib/jsduck/app.rb
Expand Up @@ -104,7 +104,12 @@ def run
def parallel_parse(filenames)
@parallel.map(filenames) do |fname|
Logger.instance.log("Parsing", fname)
SourceFile.new(JsDuck::IO.read(fname), fname, @opts)
begin
SourceFile.new(JsDuck::IO.read(fname), fname, @opts)
rescue
Logger.instance.fatal("Error while parsing #{fname}", $!)
exit(1)
end
end
end

Expand Down Expand Up @@ -159,10 +164,15 @@ def format_classes
# Format all doc-objects in parallel
formatted_classes = @parallel.map(@relations.classes) do |cls|
Logger.instance.log("Markdown formatting #{cls[:name]}")
{
:doc => class_formatter.format(cls.internal_doc),
:images => doc_formatter.images
}
begin
{
:doc => class_formatter.format(cls.internal_doc),
:images => doc_formatter.images
}
rescue
Logger.instance.fatal("Error while formatting #{cls[:name]}", $!)
exit(1)
end
end
# Then merge the data back to classes sequentially
formatted_classes.each do |cls|
Expand Down
20 changes: 13 additions & 7 deletions lib/jsduck/guides.rb
Expand Up @@ -59,16 +59,22 @@ def to_array

def load_guide(guide)
in_dir = @path + "/guides/" + guide["name"]
return Logger.instance.warn(:guide, "Guide #{in_dir} not found") unless File.exists?(in_dir)

guide_file = in_dir + "/README.md"
return Logger.instance.warn(:guide, "README.md not found in #{in_dir}") unless File.exists?(guide_file)
begin
return Logger.instance.warn(:guide, "Guide #{in_dir} not found") unless File.exists?(in_dir)

@formatter.doc_context = {:filename => guide_file, :linenr => 0}
name = File.basename(in_dir)
@formatter.img_path = "guides/#{name}"
guide_file = in_dir + "/README.md"
return Logger.instance.warn(:guide, "README.md not found in #{in_dir}") unless File.exists?(guide_file)

return add_toc(guide, @formatter.format(JsDuck::IO.read(guide_file)))
@formatter.doc_context = {:filename => guide_file, :linenr => 0}
name = File.basename(in_dir)
@formatter.img_path = "guides/#{name}"

return add_toc(guide, @formatter.format(JsDuck::IO.read(guide_file)))
rescue
Logger.instance.fatal("Error while reading/formatting guide #{in_dir}", $!)
exit(1)
end
end

def write_guide(guide, dir)
Expand Down
9 changes: 9 additions & 0 deletions lib/jsduck/logger.rb
Expand Up @@ -114,6 +114,15 @@ def format(filename=nil, line=nil)
end
out
end

# Prints fatal error message with backtrace.
# The error param should be $! from resque block.
def fatal(msg, error)
puts "#{msg}: #{error}"
puts
puts "Here's a full backtrace:"
puts error.backtrace
end
end

end
16 changes: 4 additions & 12 deletions lib/jsduck/source_file.rb
Expand Up @@ -80,18 +80,10 @@ def id(doc)

# Parses the file depending on filename as JS or CSS
def parse
begin
if @filename =~ /\.s?css$/
CssParser.new(@contents, @options).parse
else
JsParser.new(@contents, @options).parse
end
rescue
puts "Error while parsing #{@filename}: #{$!}"
puts
puts "Here's a full backtrace:"
puts $!.backtrace
exit(1)
if @filename =~ /\.s?css$/
CssParser.new(@contents, @options).parse
else
JsParser.new(@contents, @options).parse
end
end

Expand Down

0 comments on commit f7d81f1

Please sign in to comment.