Commit f7d81f12 authored by Rene Saarsoo's avatar Rene Saarsoo
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).
parent 29feeab2
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -104,7 +104,12 @@ module JsDuck
    def parallel_parse(filenames)
      @parallel.map(filenames) do |fname|
        Logger.instance.log("Parsing", fname)
        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 @@ module JsDuck
      # Format all doc-objects in parallel
      formatted_classes = @parallel.map(@relations.classes) do |cls|
        Logger.instance.log("Markdown formatting #{cls[:name]}")
        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|
+13 −7
Original line number Diff line number Diff line
@@ -59,6 +59,8 @@ module JsDuck

    def load_guide(guide)
      in_dir = @path + "/guides/" + guide["name"]

      begin
        return Logger.instance.warn(:guide, "Guide #{in_dir} not found") unless File.exists?(in_dir)

        guide_file = in_dir + "/README.md"
@@ -69,6 +71,10 @@ module JsDuck
        @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)
+9 −0
Original line number Diff line number Diff line
@@ -114,6 +114,15 @@ module JsDuck
      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
+4 −12
Original line number Diff line number Diff line
@@ -80,19 +80,11 @@ module JsDuck

    # 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)
      end
    end

    # Creates two-way link between sourcefile and doc-object.