Reputation: 492
I am testing Datetime (ruby) with null value and am getting the error "TypeError: no implicit conversion of nil into String". I wonder why my code is not working accordingly if it already has exception implemented. Any explanation about this or how to get rid of it?
def get_parsed_datetime(raw_payload)
parse_datetime(raw_payload[:datetime])
rescue ArgumentError
raise Base::UnprocessableContentError, I18n.t('invalid_datetime_error')
end
def parse_datetime(datetime_value)
{
:date => Date.parse(datetime_value).strftime('%d/%m/%Y'),
:hour => Time.parse(datetime_value).hour
}
end
Upvotes: 1
Views: 334
Reputation: 18784
Both Date.parse(nil)
and Time.parse(nil)
will raise TypeError
.
get_parsed_datetime
only rescues ArgumentError
, and not TypeError
.
A version that also rescues for TypeError
would look like this:
def get_parsed_datetime(raw_payload)
parse_datetime(raw_payload[:datetime])
rescue ArgumentError, TypeError
raise Base::UnprocessableContentError, I18n.t('invalid_datetime_error')
end
or, a version that rescues all types of errors could rescue StandardError
.
Upvotes: 4