Skip to content

Commit

Permalink
More descriptive error for unparseable DateTime
Browse files Browse the repository at this point in the history
Alters the error raised when a DateTime cannot be parsed to include the
unparseable value in order to help track down the source of the problem.
  • Loading branch information
gshutler committed Nov 1, 2014
1 parent c9a0db5 commit 8e37bcb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/icalendar/values/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ class DateTime < Value
def initialize(value, params = {})
if value.is_a? String
params['tzid'] = 'UTC' if value.end_with? 'Z'
super ::DateTime.strptime(value, FORMAT), params

begin
parsed_date = ::DateTime.strptime(value, FORMAT)
rescue ArgumentError => e
raise ArgumentError.new("Failed to parse \"#{value}\" - #{e.message}")
end

super parsed_date, params
elsif value.respond_to? :to_datetime
super value.to_datetime, params
else
Expand Down
10 changes: 9 additions & 1 deletion spec/values/date_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,13 @@
expect(subject.to_ical described_class).to eq ":#{value}"
end
end

context 'unparseable time' do
let(:value) { 'unparseable_time' }

it 'raises an error including the unparseable time' do
expect { subject }.to raise_error(ArgumentError, %r{Failed to parse \"#{value}\"})
end
end
end
end
end

0 comments on commit 8e37bcb

Please sign in to comment.