Skip to content

Commit

Permalink
Pass REXML exception messages onto the caller
Browse files Browse the repository at this point in the history
Fixes #178
  • Loading branch information
mogest committed Jan 8, 2025
1 parent 3cacace commit fb8ae2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
27 changes: 15 additions & 12 deletions lib/prawn/svg/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@ class Prawn::SVG::Document

def initialize(data, bounds, options, font_registry: nil, css_parser: CssParser::Parser.new, attribute_overrides: {})
begin
@root = REXML::Document.new(data).root
rescue REXML::ParseException
@root = nil
end

if @root.nil?
if data.respond_to?(:end_with?) && data.end_with?('.svg')
raise InvalidSVGData,
"The data supplied is not a valid SVG document. It looks like you've supplied a filename instead; use IO.read(filename) to get the data before you pass it to prawn-svg."
else
raise InvalidSVGData, 'The data supplied is not a valid SVG document.'
end
@root = REXML::Document.new(data).root or raise_parse_error(data)
rescue REXML::ParseException => e
raise_parse_error(data, e.message)
end

@warnings = []
Expand Down Expand Up @@ -73,4 +64,16 @@ def load_color_mode
raise ArgumentError, ':color_mode must be set to :rgb (default) or :cmyk'
end
end

def raise_parse_error(data, exception_message = nil)
message = 'The data supplied is not a valid SVG document.'

if data.respond_to?(:end_with?) && data.end_with?('.svg')
message += " It looks like you've supplied a filename instead; use File.read(filename) to get the data from the file before you pass it to prawn-svg."
end

message += "\n#{exception_message}" if exception_message

raise InvalidSVGData, message
end
end
22 changes: 18 additions & 4 deletions spec/prawn/svg/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,35 @@
context 'when unparsable XML is provided' do
let(:svg) { "this isn't SVG data" }

it 'raises an exception' do
it "raises an exception, passing on REXML's error message" do
expect do
Prawn::SVG::Document.new(svg, bounds, options)
end.to raise_error Prawn::SVG::Document::InvalidSVGData, 'The data supplied is not a valid SVG document.'
end.to raise_error Prawn::SVG::Document::InvalidSVGData,
/\AThe data supplied is not a valid SVG document.+Malformed.+#{Regexp.escape(svg)}/m
end
end

context 'when broken XML is provided' do
let(:svg) { '<svg><g><rect></rect></svg>' }

it "raises an exception, passing on REXML's error message" do
expect do
Prawn::SVG::Document.new(svg, bounds, options)
end.to raise_error Prawn::SVG::Document::InvalidSVGData,
/\AThe data supplied is not a valid SVG document.+Missing end tag for 'g'/m
end
end

context 'when the user passes in a filename instead of SVG data' do
let(:svg) { 'some_file.svg' }

it "raises an exception letting them know what they've done" do
message = "The data supplied is not a valid SVG document. It looks like you've supplied a filename " \
'instead; use File.read(filename) to get the data from the file before you pass it to prawn-svg.'

expect do
Prawn::SVG::Document.new(svg, bounds, options)
end.to raise_error Prawn::SVG::Document::InvalidSVGData,
"The data supplied is not a valid SVG document. It looks like you've supplied a filename instead; use IO.read(filename) to get the data before you pass it to prawn-svg."
end.to raise_error Prawn::SVG::Document::InvalidSVGData, /\A#{Regexp.escape(message)}/
end
end
end
Expand Down

0 comments on commit fb8ae2c

Please sign in to comment.