Skip to content

Commit

Permalink
Add a rescue statement when reading YAML
Browse files Browse the repository at this point in the history
- FIXED: Error handling when YAML can't be processed
  • Loading branch information
ttscoff committed Dec 5, 2023
1 parent 620219f commit 2f5dfb5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/mdless/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,17 @@ def color_meta(text)
in_yaml = true
input.sub!(/(?i-m)^---[ \t]*\n(?<content>(?:[\s\S]*?))\n[-.]{3}[ \t]*\n/m) do
m = Regexp.last_match
MDLess.log.info('Processing YAML Header')
MDLess.meta = YAML.load(m['content']).map { |k, v| "#{k.downcase}" => v }
MDLess.log.info('Processing YAML header')
begin
MDLess.meta = YAML.load(m['content']).map { |k, v| "#{k.downcase}" => v }
rescue Psych::DisallowedClass => e
@log.error('Error reading YAML header')
@log.error(e)
MDLess.meta = {}
rescue StandardError => e
@log.error("StandardError: #{e}")
end

lines = m[0].split(/\n/)
longest = lines.longest_element.length
longest = longest < MDLess.cols ? longest + 1 : MDLess.cols
Expand Down

0 comments on commit 2f5dfb5

Please sign in to comment.