Skip to content

Commit

Permalink
feat(bindata): optimise errors
Browse files Browse the repository at this point in the history
reduce string interpolation and assignments
  • Loading branch information
stakach committed Sep 14, 2020
1 parent 1932c40 commit 3755fb7
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/bindata.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ abstract class BinData
# Support inheritance
super(io)

type_name = {{@type.stringify}}
part_name = ""

begin
Expand Down Expand Up @@ -173,7 +172,7 @@ abstract class BinData

{% if part[:verify] %}
if !({{part[:verify]}}).call
raise VerificationException.new "Failed to verify reading #{{{part[:type]}}} at {{@type}}.{{part[:name]}}"
raise VerificationException.new "Failed to verify reading {{part[:type].id}} at {{@type}}.{{part[:name]}}"
end
{% end %}
{% end %}
Expand All @@ -193,15 +192,15 @@ abstract class BinData
{% end %}
{% if REMAINING[0][:verify] %}
if !({{REMAINING[0][:verify]}}).call
raise VerificationException.new "Failed to verify reading #{{{REMAINING[0][:type]}}} at {{@type}}.{{REMAINING[0][:name]}}"
raise VerificationException.new "Failed to verify reading {{REMAINING[0][:type].id}} at {{@type}}.{{REMAINING[0][:name]}}"
end
{% end %}
{% end %}

rescue ex : VerificationException | ParseError
raise ex
rescue error
raise ParseError.new "failed to parse #{type_name}.#{part_name}", error
raise ParseError.new "failed to parse {{@type.id}}.#{part_name}", error
end

io
Expand All @@ -211,7 +210,6 @@ abstract class BinData
# Support inheritance
super(io)

type_name = {{@type.stringify}}
part_name = ""

begin
Expand Down Expand Up @@ -294,7 +292,7 @@ abstract class BinData

{% if part[:verify] %}
if !({{part[:verify]}}).call
raise VerificationException.new "Failed to verify writing #{{{part[:type]}}} at {{@type}}.{{part[:name]}}"
raise VerificationException.new "Failed to verify writing {{part[:type].id}} at {{@type}}.{{part[:name]}}"
end
{% end %}
{% end %}
Expand All @@ -312,15 +310,15 @@ abstract class BinData
{% end %}
{% if REMAINING[0][:verify] %}
if !({{REMAINING[0][:verify]}}).call
raise VerificationException.new "Failed to verify writing #{{{REMAINING[0][:type]}}} at {{@type}}.{{REMAINING[0][:name]}}"
raise VerificationException.new "Failed to verify writing {{REMAINING[0][:type].id}} at {{@type}}.{{REMAINING[0][:name]}}"
end
{% end %}
{% end %}

rescue ex : VerificationException | WriteError
raise ex
rescue error
raise WriteError.new "failed to write #{type_name}.#{part_name}", error
raise WriteError.new "failed to write {{@type.id}}.#{part_name}", error
end

io
Expand Down

0 comments on commit 3755fb7

Please sign in to comment.