Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor: make gem errors inherit from base classes #37

Merged
merged 4 commits into from
Oct 23, 2023
Merged
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
3 changes: 1 addition & 2 deletions lib/itax_code.rb
Original file line number Diff line number Diff line change
@@ -6,10 +6,9 @@
require "itax_code/utils"
require "itax_code/encoder"
require "itax_code/parser"
require "itax_code/error"

module ItaxCode
Error = Class.new(StandardError)

class << self
# Encodes the user tax code.
#
4 changes: 1 addition & 3 deletions lib/itax_code/encoder.rb
Original file line number Diff line number Diff line change
@@ -14,8 +14,6 @@ module ItaxCode
#
# @return [String] The encoded tax code
class Encoder
MissingDataError = Class.new(StandardError)

# @param [Hash] data The user attributes
# @param [Utils] utils
#
@@ -99,7 +97,7 @@ def validate_data_presence!
def parse_birthdate!
Date.parse(birthdate)
rescue StandardError
raise ArgumentError, "#{birthdate} is not a valid date"
raise InvalidBirthdateError, "#{birthdate} is not a valid date"
end
end
end
20 changes: 20 additions & 0 deletions lib/itax_code/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module ItaxCode
Error = Class.new(StandardError)

class Encoder
Error = Class.new(Error)

InvalidBirthdateError = Class.new(Error)
MissingDataError = Class.new(Error)
end

class Parser
Error = Class.new(Error)

InvalidControlInternalNumberError = Class.new(Error)
InvalidTaxCodeError = Class.new(Error)
NoTaxCodeError = Class.new(Error)
end
end
5 changes: 0 additions & 5 deletions lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
@@ -11,11 +11,6 @@ module ItaxCode
#
# @return [Hash] The parsed tax code
class Parser
Error = Class.new(StandardError)
NoTaxCodeError = Class.new(Error)
InvalidControlInternalNumberError = Class.new(Error)
InvalidTaxCodeError = Class.new(Error)

LENGTH = 16

# @param [String] tax_code
2 changes: 1 addition & 1 deletion test/itax_code/encoder_test.rb
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ class EncoderTest < ActiveSupport::TestCase
end

test "#encode raises ArgumentError on malformed birthdate" do
assert_raises ArgumentError do
assert_raises Encoder::InvalidBirthdateError do
Encoder.new(
surname: "Rossi",
name: "Mario",