Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Raise specific errors with invalid UUID cases
It would be more helpful for developers integrating functionality at a
higher level if errors raised for invalid UUIDs are namespaced. This
adds a new Cql::Uuid::InvalidUuidError that improves two cases:

 - The fix for jruby issue jruby/jruby#1608
   is no longer necessary because we are now raising an explicit error
   describing that the UUID is invalid - so the reliance on raising an
   ArgumentError in order to maintain consistency for Integer is no
   longer necessary.

 - The error messages are now more specific, and therefore more helpful
   for when a developer is doing an integration. On the same note, the
   errors being namespaced specifically under Cql::Uuid allows for a
   rescue for specific cases, instead of the higher-level ArgumentError
   that could occur in many other places, for many other reasons.
  • Loading branch information
Russell Cloak committed Aug 5, 2014
1 parent 3d1ab79 commit f3bd764
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
25 changes: 9 additions & 16 deletions lib/cql/uuid.rb
Expand Up @@ -9,11 +9,13 @@ module Cql
# If you want to generate UUIDs see {Cql::TimeUuid::Generator}.
#
class Uuid
InvalidUuidError = Class.new(CqlError)

# Creates a new UUID either from a string (expected to be on the standard
# 8-4-4-4-12 form, or just 32 characters without hyphens), or from a
# 128 bit number.
#
# @raise [ArgumentError] if the string does not conform to the expected format
# @raise [Cql::Uuid::InvalidUuidError] if the string does not conform to the expected format
#
def initialize(n)
case n
Expand Down Expand Up @@ -61,22 +63,13 @@ def eql?(other)
RAW_FORMAT = '%032x'.force_encoding(Encoding::ASCII).freeze
HYPHEN = '-'.force_encoding(Encoding::ASCII).freeze
EMPTY_STRING = ''.freeze
HEX_RE = /^[A-Fa-f0-9]+$/

if RUBY_ENGINE == 'jruby'
HEX_RE = /^[A-Fa-f0-9]+$/
# See https://github.com/jruby/jruby/issues/1608
def from_s(str)
str = str.gsub(HYPHEN, EMPTY_STRING)
raise ArgumentError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32
raise ArgumentError, "invalid value for Integer(): \"#{str}\"" unless str =~ HEX_RE
Integer(str, 16)
end
else
def from_s(str)
str = str.gsub(HYPHEN, EMPTY_STRING)
raise ArgumentError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32
Integer(str, 16)
end
def from_s(str)
str = str.gsub(HYPHEN, EMPTY_STRING)
raise InvalidUuidError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32
raise InvalidUuidError, "Expected only hexidecimal digits but got \"#{str}\"" unless str =~ HEX_RE
Integer(str, 16)
end
end
end
6 changes: 3 additions & 3 deletions spec/cql/uuid_spec.rb
Expand Up @@ -15,15 +15,15 @@ module Cql
end

it 'raises an error if the string is shorter than 32 chars' do
expect { Uuid.new('a4a7090024e111df8924001ff359171') }.to raise_error(ArgumentError)
expect { Uuid.new('a4a7090024e111df8924001ff359171') }.to raise_error(Cql::Uuid::InvalidUuidError)
end

it 'raises an error if the string is longer than 32 chars' do
expect { Uuid.new('a4a7090024e111df8924001ff35917111') }.to raise_error(ArgumentError)
expect { Uuid.new('a4a7090024e111df8924001ff35917111') }.to raise_error(Cql::Uuid::InvalidUuidError)
end

it 'raises an error if the string is not a hexadecimal number' do
expect { Uuid.new('a4a7090024e111df8924001ff359171x') }.to raise_error(ArgumentError)
expect { Uuid.new('a4a7090024e111df8924001ff359171x') }.to raise_error(Cql::Uuid::InvalidUuidError)
end

it 'can be created from a number' do
Expand Down

0 comments on commit f3bd764

Please sign in to comment.