Project

General

Profile

Actions

Misc #15298

closed

Inconsistent date errors

Added by gollahon (Daniel Gollahon) over 5 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
[ruby-core:89777]

Description

When using Date, if you initialize an invalid date you'll get an ArgumentError:

Date.new(1999, 1, 32)   # => ArgumentError: invalid date

This is as expected.

If you provide nil as any of the three arguments, you do not get this error. You get three unrelated errors (two NoMethodErrors and one TypeError), none of which are specific to date:

Date.new(1999, 1, nil)  # => NoMethodError: undefined method `div' for nil:NilClass
Date.new(1999, nil, 32) # => TypeError: no implicit conversion from nil to integer
Date.new(nil, 1, 32)    # => NoMethodError: undefined method `<' for nil:NilClass

My proposal is that all invalid date initialization cases should raise ArgumentError: invalid date or some uniform date-specific error. This would make debugging more convenient / user friendly and makes rescuing ArgumentError actually catch all Date initialization errors. I find it weird that these internal calls error out and are exposed.

Thanks,
Daniel


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #11935: Date.new returns inconsistent errors when passed invalid argumentsClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0