Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add specific errors. #27

Merged
merged 1 commit into from

2 participants

@miloshadzic

Some errors bubble up to surrounding code when phone is used. Hopefully this patch makes them easier to handle(it did for me).

Proposed exception structure in the patch:

RuntimeError
|-- PhoneError
   |--NumberError
   |--AreaCodeError
   |--CountryCodeError

Please review.

@carr carr merged commit 388c50f into carr:master
@miloshadzic miloshadzic deleted the miloshadzic:exceptions branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 16, 2012
  1. @miloshadzic

    Add specific errors.

    miloshadzic authored
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 15 deletions.
  1. +6 −0 lib/errors.rb
  2. +8 −6 lib/phone.rb
  3. +9 −9 test/phone_test.rb
View
6 lib/errors.rb
@@ -0,0 +1,6 @@
+module Phoner
+ class PhoneError < RuntimeError;end
+ class BlankNumberError < PhoneError;end
+ class AreaCodeError < PhoneError;end
+ class CountryCodeError < PhoneError;end
+end
View
14 lib/phone.rb
@@ -11,6 +11,7 @@
#
require File.join(File.dirname(__FILE__), 'support') unless defined? ActiveSupport
require File.join(File.dirname(__FILE__), 'country')
+require File.join(File.dirname(__FILE__), 'errors')
module Phoner
class Phone
@@ -48,9 +49,9 @@ def initialize(*hash_or_args)
self.country_code = hash_or_args[ keys[:country_code] ] || self.default_country_code
self.extension = hash_or_args[ keys[:extension] ]
- raise "Must enter number" if self.number.blank?
- raise "Must enter area code or set default area code" if self.area_code.blank?
- raise "Must enter country code or set default country code" if self.country_code.blank?
+ raise NumberError, "Must enter number" if self.number.blank?
+ raise AreaCodeError, "Must enter area code or set default area code" if self.area_code.blank?
+ raise CountryCodeError, "Must enter country code or set default country code" if self.country_code.blank?
end
# create a new phone number by parsing a string
@@ -78,7 +79,8 @@ def self.parse(string, options={})
def self.valid?(string)
begin
parse(string).present?
- rescue RuntimeError # if we encountered exceptions (missing country code, missing area code etc)
+ # if we encountered exceptions (missing country code, missing area code etc)
+ rescue PhoneError
return false
end
end
@@ -98,9 +100,9 @@ def self.split_to_parts(string, options = {})
if country.nil?
if options[:country_code].nil?
- raise "Must enter country code or set default country code"
+ raise CountryCodeError, "Must enter country code or set default country code"
else
- raise "Could not find country with country code #{options[:country_code]}"
+ raise CountryCodeError, "Could not find country with country code #{options[:country_code]}"
end
end
View
18 test/phone_test.rb
@@ -4,17 +4,17 @@ class PhoneTest < Test::Unit::TestCase
def test_number_without_country_code_initialize
Phoner::Phone.default_country_code = nil
-
- assert_raise RuntimeError do
+
+ assert_raise Phoner::CountryCodeError do
pn = Phoner::Phone.new '5125486', '91'
end
end
def test_number_without_country_and_area_code_initialize
Phoner::Phone.default_country_code = nil
- Phoner::Phone.default_area_code = nil
-
- assert_raise RuntimeError do
+ Phoner::Phone.default_area_code = nil
+
+ assert_raise Phoner::AreaCodeError do
pn = Phoner::Phone.new '451588'
end
end
@@ -54,16 +54,16 @@ def test_parse_empty
def test_parse_short_without_special_characters_without_country
Phoner::Phone.default_country_code = nil
-
- assert_raise RuntimeError do
+
+ assert_raise Phoner::CountryCodeError do
pn = Phoner::Phone.parse "0915125486"
end
end
def test_parse_short_with_special_characters_without_country
Phoner::Phone.default_country_code = nil
-
- assert_raise RuntimeError do
+
+ assert_raise Phoner::CountryCodeError do
pn = Phoner::Phone.parse "091/512-5486"
end
end
Something went wrong with that request. Please try again.