From 02f1eade46dbe1d14bb2f9b7215c4460aadd327d Mon Sep 17 00:00:00 2001 From: Gabriel Belgamo <19699724+belgamo@users.noreply.github.com> Date: Fri, 17 Jul 2020 10:52:38 -0300 Subject: [PATCH] Improve the message of protected tokens error (#1641) (closes #1230) Co-authored-by: Gabriel --- CHANGELOG.md | 1 + src/_lib/protectedTokens/index.js | 10 +++++----- src/format/index.js | 12 ++++++------ src/format/test.js | 8 ++++---- src/parse/index.js | 12 ++++++------ src/parse/test.js | 8 ++++---- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e87198c15..fa61e49a5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,7 @@ Kudos to [@oakhan3](https://github.com/oakhan3), [@Mukhammadali](https://github. - [Added `weeks` to `Duration`](https://github.com/date-fns/date-fns/pull/1592). - [Added `weeks` support to `add` and `sub`](https://github.com/date-fns/date-fns/pull/1592). +- [Added details message in `throwProtectedError`](https://github.com/date-fns/date-fns/pull/1592). ## [2.9.0] - 2020-01-08 diff --git a/src/_lib/protectedTokens/index.js b/src/_lib/protectedTokens/index.js index 4987ff9bfd..c62313f1b8 100644 --- a/src/_lib/protectedTokens/index.js +++ b/src/_lib/protectedTokens/index.js @@ -9,22 +9,22 @@ export function isProtectedWeekYearToken(token) { return protectedWeekYearTokens.indexOf(token) !== -1 } -export function throwProtectedError(token) { +export function throwProtectedError(token, format, input) { if (token === 'YYYY') { throw new RangeError( - 'Use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr' + `Use \`yyyy\` instead of \`YYYY\` (in \`${format}\`) for formatting years to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'YY') { throw new RangeError( - 'Use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr' + `Use \`yy\` instead of \`YY\` (in \`${format}\`) for formatting years to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'D') { throw new RangeError( - 'Use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr' + `Use \`d\` instead of \`D\` (in \`${format}\`) for formatting days of the month to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'DD') { throw new RangeError( - 'Use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr' + `Use \`dd\` instead of \`DD\` (in \`${format}\`) for formatting days of the month to the input \`${input}\`; see: https://git.io/fxCyr` ) } } diff --git a/src/format/index.js b/src/format/index.js index 21289ce5e7..bac081d48c 100644 --- a/src/format/index.js +++ b/src/format/index.js @@ -319,10 +319,10 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/ * @throws {RangeError} `options.locale` must contain `formatLong` property * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7 - * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr - * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr + * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr * @throws {RangeError} format string contains an unescaped latin alphabet character * * @example @@ -438,13 +438,13 @@ export default function format(dirtyDate, dirtyFormatStr, dirtyOptions) { !options.useAdditionalWeekYearTokens && isProtectedWeekYearToken(substring) ) { - throwProtectedError(substring) + throwProtectedError(substring, dirtyFormatStr, dirtyDate) } if ( !options.useAdditionalDayOfYearTokens && isProtectedDayOfYearToken(substring) ) { - throwProtectedError(substring) + throwProtectedError(substring, dirtyFormatStr, dirtyDate) } return formatter(utcDate, substring, locale.localize, formatterOptions) } diff --git a/src/format/test.js b/src/format/test.js index d5655712d1..2c85c50f64 100644 --- a/src/format/test.js +++ b/src/format/test.js @@ -743,7 +743,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `d` instead of `D` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /(Use `d` instead of `D` \(in `yyyy-MM-D`\) for formatting days of the month to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -759,7 +759,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `dd` instead of `DD` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /(Use `dd` instead of `DD` \(in `yyyy-MM-DD`\) for formatting days of the month to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -775,7 +775,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yy` instead of `YY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /(Use `yy` instead of `YY` \(in `YY-MM-dd`\) for formatting years to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -791,7 +791,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yyyy` instead of `YYYY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /(Use `yyyy` instead of `YYYY` \(in `YYYY-MM-dd`\) for formatting years to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) diff --git a/src/parse/index.js b/src/parse/index.js index 9460e3f769..86d6aee636 100644 --- a/src/parse/index.js +++ b/src/parse/index.js @@ -336,10 +336,10 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/ * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7 * @throws {RangeError} `options.locale` must contain `match` property - * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr - * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr + * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr * @throws {RangeError} format string contains an unescaped latin alphabet character * * @example @@ -451,13 +451,13 @@ export default function parse( !options.useAdditionalWeekYearTokens && isProtectedWeekYearToken(token) ) { - throwProtectedError(token) + throwProtectedError(token, formatString, dirtyDateString) } if ( !options.useAdditionalDayOfYearTokens && isProtectedDayOfYearToken(token) ) { - throwProtectedError(token) + throwProtectedError(token, formatString, dirtyDateString) } var firstCharacter = token[0] diff --git a/src/parse/test.js b/src/parse/test.js index fdcdb7dab8..b78488f53a 100644 --- a/src/parse/test.js +++ b/src/parse/test.js @@ -2464,7 +2464,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `d` instead of `D` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /Use `d` instead of `D` \(in `yyyy D`\) for formatting days of the month to the input `2016 5`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2480,7 +2480,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `dd` instead of `DD` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /Use `dd` instead of `DD` \(in `yyyy DD`\) for formatting days of the month to the input `2016 05`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2496,7 +2496,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yy` instead of `YY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /Use `yy` instead of `YY` \(in `YY w`\) for formatting years to the input `16 1`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2512,7 +2512,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yyyy` instead of `YYYY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /Use `yyyy` instead of `YYYY` \(in `YYYY w`\) for formatting years to the input `2016 1`; see: https:\/\/git.io\/fxCyr/ ) })