Open Bug 1946615 Opened 9 months ago Updated 9 months ago

Improve error message in Temporal.Duration.prototype.add/subtract for calendar durations

Categories

(Core :: JavaScript: Standard Library, enhancement, P1)

enhancement

Tracking

()

Tracking Status
firefox137 --- affected

People

(Reporter: ptomato, Unassigned)

References

(Blocks 1 open bug)

Details

Ref: https://github.com/mdn/content/pull/37978#discussion_r1945848982

Temporal.Duration.prototype.add/subtract don't support durations with calendar units. Currently the error message is:

const d1 = Temporal.Duration.from({ days: 1 });
const d2 = Temporal.Duration.from({ months: 1 });
d1.add(d2); // RangeError: can't compare durations when "relativeTo" is undefined

The message refers to a relativeTo option that no longer exists, so is confusing. I suggest a message something like "for calendar duration arithmetic, use date arithmetic relative to a starting point."

The error message in question is https://searchfox.org/mozilla-central/search?q=JSMSG_TEMPORAL_DURATION_UNCOMPARABLE

At https://searchfox.org/mozilla-central/rev/b0b003e992b199fd8e13999bd5d06d06c84a3fd2/js/src/builtin/temporal/Duration.cpp#1387, this corresponds to https://tc39.es/proposal-temporal/#sec-temporal.duration.compare, step 13.

At https://searchfox.org/mozilla-central/rev/b0b003e992b199fd8e13999bd5d06d06c84a3fd2/js/src/builtin/temporal/Duration.cpp#2786, this corresponds to https://tc39.es/proposal-temporal/#sec-temporal-adddurations, step 6.

At https://searchfox.org/mozilla-central/rev/b0b003e992b199fd8e13999bd5d06d06c84a3fd2/js/src/builtin/temporal/Duration.cpp#3681, this corresponds to https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.round, step 28.

At https://searchfox.org/mozilla-central/rev/b0b003e992b199fd8e13999bd5d06d06c84a3fd2/js/src/builtin/temporal/Duration.cpp#3874, this corresponds to https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.total, steps 13 a and b.

The common thread is the https://tc39.es/proposal-temporal/#sec-temporal-iscalendarunit operation. I wonder if we could say something like: "Unable to {0} because {1} would require a calendar calculation", where we would substitute the name of the operation and problem into the error message, e.g. "add duration", "largest unit".

We have the ability to link error messages in devtools to MDN pages to give the extra help, e.g. "use date arithmetic relative to a starting point", although I don't think any of this is configured for Temporal. I'll check and file another bug if necessary.

Severity: -- → N/A
Priority: -- → P1
You need to log in before you can comment on or make changes to this bug.