add error on bad section JSON

Julien Girard committed Apr 30, 2018
commit 919bb041d407dd8b8776c49735e8a6600cc93a27
Showing 5 changed files with 59 additions and 12 deletions
locomotive/steam/adapters/filesystem/sanitizers/section.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +10 -6
@@ @@ -15,13 +15,17 @@ module Locomotive::Steam
def parse_json(entity)
content = File.read(entity.template_path)
+ match = content.match(JSON_FRONTMATTER_REGEXP)
+ raise raise_parsing_error(entity, content) if match.nil?
+ json, template = match[:json], match[:template]
+ entity.definition = JSON.parse(json)
+ entity.template = template
+ rescue JSON::ParserError
+ raise_parsing_error(entity, content)
+ end
- if match = content.match(JSON_FRONTMATTER_REGEXP)
- json, template = match[:json], match[:template]
-
- entity.definition = JSON.parse(json)
- entity.template = template
- end
+ def raise_parsing_error(entity, content)
+ raise Locomotive::Steam::ParsingRenderingError.new('Your section requires a valid JSON header', entity.template_path, content, 0, nil)
end
end
end
spec/fixtures/errors/section_bad_json_content.liquid +9 -0
@@ @@ -0,0 +1,9 @@
+ ---
+ {
+ name: "Section"
+ text: "Missing coma in previous line"
+ }
+ ---
+ <div>
+ Plop
+ </div>
\ No newline at end of file
spec/fixtures/errors/section_bad_json_header.liquid +8 -0
@@ @@ -0,0 +1,8 @@
+ {
+ name: "Section",
+ text: "Missing start of JSON symbol"
+ }
+ ---
+ <div>
+ Plop
+ </div>
\ No newline at end of file
spec/integration/liquid/tags/sections_spec.rb +0 -0
spec/unit/adapters/filesystem/sanitizers/section_spec.rb +32 -6
@@ @@ -2,6 +2,7 @@ require 'spec_helper'
require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/sanitizer.rb'
require_relative '../../../../../lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb'
+ require_relative '../../../../../lib/locomotive/steam/errors.rb'
describe Locomotive::Steam::Adapters::Filesystem::Sanitizers::Section do
@@ @@ -17,9 +18,12 @@ describe Locomotive::Steam::Adapters::Filesystem::Sanitizers::Section do
describe '#apply_to_entity' do
subject { sanitizer.apply_to_entity(entity) }
- it 'sanitize entity' do
- expect(entity).to receive(:definition=).with(hash_including({ 'name' => 'header' }))
- expect(entity).to receive(:template=).with((<<-LIQUID
+
+ describe 'with correct json' do
+
+ it 'sanitize entity' do
+ expect(entity).to receive(:definition=).with(hash_including({ 'name' => 'header' }))
+ expect(entity).to receive(:template=).with((<<-LIQUID
<h1> {{ section.settings.brand }} </h1>
<ul>
{% for block in section.blocks %}
@@ @@ -31,9 +35,31 @@ describe Locomotive::Steam::Adapters::Filesystem::Sanitizers::Section do
{% endfor %}
</ul>
LIQUID
- ).gsub /^$\n/, '')
- expect(entity).to receive(:[]=).with(:site_id, 1)
- subject
+ ).gsub /^$\n/, '')
+ expect(entity).to receive(:[]=).with(:site_id, 1)
+ subject
+ end
+ end
+ describe 'errors' do
+ before(:each) do
+ allow(entity).to receive(:[]=)
+ end
+
+ describe 'in json header' do
+ let(:template_path) { 'spec/fixtures/errors/section_bad_json_header.liquid' }
+
+ it 'should throw an error' do
+ expect { subject }.to raise_error(Locomotive::Steam::ParsingRenderingError)
+ end
+ end
+
+ describe 'json content' do
+ let(:template_path) { 'spec/fixtures/errors/section_bad_json_content.liquid' }
+
+ it 'should throw an error' do
+ expect { subject }.to raise_error(Locomotive::Steam::ParsingRenderingError)
+ end
+ end
end
end
end