Skip to content

Commit

Permalink
Merge pull request #28 from razeware/WEB-3513
Browse files Browse the repository at this point in the history
WEB-3513: Parsing errors in chapter metadata are now labelled
  • Loading branch information
sammyd authored Jul 28, 2020
2 parents 5d3788a + e627190 commit fadc9dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
21 changes: 19 additions & 2 deletions app/lib/linting/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def lint(options: {})
output
end

def lint_with_ui(options:, show_ui: true) # rubocop:disable Metrics/MethodLength
def lint_with_ui(options:, show_ui: true) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
with_spinner(title: 'Checking {{bold:publish.yaml}} exists', show: show_ui) do
check_publish_file_exists
end
Expand All @@ -31,6 +31,11 @@ def lint_with_ui(options:, show_ui: true) # rubocop:disable Metrics/MethodLength
end
return unless annotations.blank?

with_spinner(title: 'Attempting to parse book', show: show_ui) do
book
end
return unless annotations.blank?

with_spinner(title: 'Validating image references', show: show_ui) do
annotations.concat(Linting::ImageLinter.new(book: book).lint)
end
Expand Down Expand Up @@ -78,11 +83,23 @@ def check_publish_file_exists
false
end

def book
def book # rubocop:disable Metrics/MethodLength
@book ||= begin
parser = Parser::Publish.new(file: file)
parser.parse
end
rescue Parser::Error => e
line_number = (e.message.match(/at line (\d+)/)&.captures&.first&.to_i || 0) + 1
annotations.push(
Annotation.new(
absolute_path: e.file,
annotation_level: 'failure',
start_line: line_number,
end_line: line_number,
message: e.message,
title: 'Unable to parse book.'
)
)
end
end
end
17 changes: 17 additions & 0 deletions app/lib/parser/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Parser
# An error for capturing parsing issues
class Error < StandardError
attr_reader :file

def initialize(file:, error: nil, msg: nil)
message = "There was a problem parsing #{file}"
message += "\n#{error.class.name}\n#{error.message}" if error.present?
message += "\n#{msg}" if msg.present?

super(message)
@file = file
end
end
end
8 changes: 7 additions & 1 deletion app/lib/parser/markdown_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ def extract_metadata
end

def metadata
@metadata ||= Psych.load(extract_metadata).deep_symbolize_keys
@metadata ||= begin
Psych.load(extract_metadata, symbolize_names: true).tap do |metadata|
raise Parser::Error.new(file: path, msg: 'Unable to locate metadata at the top of the markdown') unless metadata.present?
end
end
rescue Psych::SyntaxError => e
raise Parser::Error.new(file: path, error: e)
end

def simple_attributes
Expand Down

0 comments on commit fadc9dc

Please sign in to comment.