Skip to content
Browse files

Improve fatal error handling.

- 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...
1 parent 29feeab commit f7d81f12fd63aedf5e30cf792ce1fca2c4e8a967 @nene nene committed
Showing with 41 additions and 24 deletions.
  1. +15 −5 lib/jsduck/app.rb
  2. +13 −7 lib/jsduck/guides.rb
  3. +9 −0 lib/jsduck/logger.rb
  4. +4 −12 lib/jsduck/source_file.rb
View
20 lib/jsduck/app.rb
@@ -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
@@ -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|
View
20 lib/jsduck/guides.rb
@@ -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)
View
9 lib/jsduck/logger.rb
@@ -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
View
16 lib/jsduck/source_file.rb
@@ -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

0 comments on commit f7d81f1

Please sign in to comment.
Something went wrong with that request. Please try again.