Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEB-3513: Parsing errors in chapter metadata are now labelled #28

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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