From 88f325e378b6e8532b8ae9107624f6b95eac087a Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Wed, 3 Jul 2024 02:54:31 +0530 Subject: [PATCH 1/7] [chore]: refactor exception strings to be more verbose --- .../.internal/utils/getIntlInstanceWithOptions.ts | 5 ++++- .../src/modules/currency/convertToMajorUnit.ts | 7 +++++-- .../src/modules/currency/convertToMinorUnit.ts | 7 +++++-- .../src/modules/currency/formatNumber.ts | 8 +++++--- .../src/modules/currency/formatNumberByParts.ts | 8 +++++--- .../src/modules/currency/getCurrencySymbol.ts | 5 ++++- .../src/modules/dateTime/formatDateTime.ts | 6 ++++-- .../src/modules/dateTime/getRelativeTime.ts | 6 ++++-- .../src/modules/dateTime/getWeekdays.ts | 6 ++++-- .../src/modules/dateTime/parseDateTime.ts | 6 ++++-- packages/i18nify-js/src/modules/dateTime/utils.ts | 15 +++++++++++---- .../i18nify-js/src/modules/geo/getAllCountries.ts | 4 +++- packages/i18nify-js/src/modules/geo/getCities.ts | 14 +++++++++++--- .../src/modules/geo/getFlagOfCountry.ts | 4 +++- packages/i18nify-js/src/modules/geo/getStates.ts | 10 ++++++++-- .../i18nify-js/src/modules/geo/getZipcodes.ts | 12 +++++++++--- .../src/modules/phoneNumber/formatPhoneNumber.ts | 5 ++++- .../phoneNumber/getDialCodeByCountryCode.ts | 5 ++++- .../modules/phoneNumber/getMaskedPhoneNumber.ts | 8 ++++++-- .../src/modules/phoneNumber/parsePhoneNumber.ts | 5 ++++- 20 files changed, 107 insertions(+), 39 deletions(-) diff --git a/packages/i18nify-js/src/modules/.internal/utils/getIntlInstanceWithOptions.ts b/packages/i18nify-js/src/modules/.internal/utils/getIntlInstanceWithOptions.ts index 068a41ee..b4f647a2 100644 --- a/packages/i18nify-js/src/modules/.internal/utils/getIntlInstanceWithOptions.ts +++ b/packages/i18nify-js/src/modules/.internal/utils/getIntlInstanceWithOptions.ts @@ -20,7 +20,10 @@ export const getIntlInstanceWithOptions = ( intlOptions.currency = (options.currency || intlOptions.currency) as string; } - if (!locale) throw new Error('Pass valid locale !'); + if (!locale) + throw new Error( + `The provided locale value is invalid. The received value was: ${locale}. Please ensure you pass a correct locale string for proper formatting.`, + ); return new Intl.NumberFormat( locale || undefined, diff --git a/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts index cfda4504..d014c6fb 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts @@ -22,8 +22,11 @@ const convertToMajorUnit = ( ): number => { const currencyInfo = CURRENCY_INFO[options.currency]; - if (!currencyInfo) - throw new Error(`Unsupported currency ${String(options.currency)}`); + if (!options.currency || !currencyInfo) { + throw new Error( + `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code that is included in the supported list.`, + ); + } const minorUnitMultiplier = Math.pow(10, Number(currencyInfo.minor_unit)) || 100; diff --git a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts index 85262ee8..57418279 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts @@ -22,8 +22,11 @@ const convertToMinorUnit = ( ): number => { const currencyInfo = CURRENCY_INFO[options.currency]; - if (!currencyInfo) - throw new Error(`Unsupported currency ${String(options.currency)}`); + if (!options.currency || !currencyInfo) { + throw new Error( + `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code that is included in the supported list.`, + ); + } const minorUnitMultiplier = Math.pow(10, Number(currencyInfo.minor_unit)) || 100; diff --git a/packages/i18nify-js/src/modules/currency/formatNumber.ts b/packages/i18nify-js/src/modules/currency/formatNumber.ts index 8fb38c3e..9d9f7460 100644 --- a/packages/i18nify-js/src/modules/currency/formatNumber.ts +++ b/packages/i18nify-js/src/modules/currency/formatNumber.ts @@ -15,7 +15,7 @@ const formatNumber = ( // Validate the amount parameter to ensure it is a number if (!Number(amount) && Number(amount) !== 0) throw new Error( - `Parameter 'amount' is not a number. typeof amount: ${typeof amount}`, + `Parameter 'amount' is not a valid number. The received value was: ${amount} of type ${typeof amount}. Please ensure you pass a valid number.`, ); try { @@ -34,9 +34,11 @@ const formatNumber = ( return parts.map((p) => p.value).join(''); } catch (err) { if (err instanceof Error) { - throw new Error(err.message); + throw new Error( + `An error occurred while formatting the number: ${err.message}`, + ); } else { - throw new Error(`An unknown error occurred = ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } }; diff --git a/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts b/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts index 02e7f6ab..71599b98 100644 --- a/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts +++ b/packages/i18nify-js/src/modules/currency/formatNumberByParts.ts @@ -20,7 +20,7 @@ const formatNumberByParts = ( // Validate the amount parameter to ensure it is a number if (!Number(amount) && Number(amount) !== 0) throw new Error( - `Parameter 'amount' is not a number. typeof amount: ${typeof amount}`, + `Parameter 'amount' is not a valid number. The received value was: ${amount} of type ${typeof amount}. Please ensure you pass a valid number.`, ); try { @@ -58,9 +58,11 @@ const formatNumberByParts = ( }; } catch (err) { if (err instanceof Error) { - throw new Error(err.message); + throw new Error( + `An error occurred while formatting the number: ${err.message}`, + ); } else { - throw new Error(`An unknown error occurred = ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } }; diff --git a/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts b/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts index 4d8e9256..2651e921 100644 --- a/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts +++ b/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts @@ -6,7 +6,10 @@ const getCurrencySymbol = (currencyCode: CurrencyCodeType): string => { const currencyInformation = CURRENCY_INFO; if (currencyCode in currencyInformation) return currencyInformation[currencyCode]?.symbol; - else throw new Error(`Invalid currencyCode: ${String(currencyCode)}`); + else + throw new Error( + `The provided currency code is invalid. The received value was: ${String(currencyCode)}. Please ensure you pass a valid currency code that is included in the supported list.`, + ); }; export default withErrorBoundary(getCurrencySymbol); diff --git a/packages/i18nify-js/src/modules/dateTime/formatDateTime.ts b/packages/i18nify-js/src/modules/dateTime/formatDateTime.ts index c1f75a73..93c049c3 100644 --- a/packages/i18nify-js/src/modules/dateTime/formatDateTime.ts +++ b/packages/i18nify-js/src/modules/dateTime/formatDateTime.ts @@ -69,9 +69,11 @@ const formatDateTime = ( formatter = new DateFormatter(extractedLocale, finalIntlOptions); } catch (err) { if (err instanceof Error) { - throw new Error(err.message); + throw new Error( + `An error occurred while creating the DateFormatter instance: ${err.message}. Please ensure the provided options are valid and try again.`, + ); } else { - throw new Error(`An unknown error occurred = ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } diff --git a/packages/i18nify-js/src/modules/dateTime/getRelativeTime.ts b/packages/i18nify-js/src/modules/dateTime/getRelativeTime.ts index 872bc8e2..440d4e8e 100644 --- a/packages/i18nify-js/src/modules/dateTime/getRelativeTime.ts +++ b/packages/i18nify-js/src/modules/dateTime/getRelativeTime.ts @@ -72,9 +72,11 @@ const getRelativeTime = ( relativeTime = rtf.format(Math.round(value), unit); } catch (err) { if (err instanceof Error) { - throw new Error(err.message); + throw new Error( + `An error occurred while creating the RelativeTimeFormat instance: ${err.message}. Please ensure the provided options are valid and try again.`, + ); } else { - throw new Error(`An unknown error occurred = ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } diff --git a/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts b/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts index 950ec38a..54d69cf0 100644 --- a/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts +++ b/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts @@ -33,9 +33,11 @@ const getWeekdays = (options: { ); } catch (err) { if (err instanceof Error) { - throw new Error(err.message); + throw new Error( + `An error occurred while creating the DateFormatter instance or formatting the weekdays: ${err.message}. Please ensure the provided options are valid and try again.`, + ); } else { - throw new Error(`An unknown error occurred = ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } }; diff --git a/packages/i18nify-js/src/modules/dateTime/parseDateTime.ts b/packages/i18nify-js/src/modules/dateTime/parseDateTime.ts index 908445f7..6d8c57b6 100644 --- a/packages/i18nify-js/src/modules/dateTime/parseDateTime.ts +++ b/packages/i18nify-js/src/modules/dateTime/parseDateTime.ts @@ -57,9 +57,11 @@ const parseDateTime = ( } catch (err) { // Handle any errors that occur during parsing if (err instanceof Error) { - throw err; + throw new Error( + `An error occurred while parsing the date: ${err.message}. Please ensure the provided date and options are valid and try again.`, + ); } else { - throw new Error(`An unknown error occurred: ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } }; diff --git a/packages/i18nify-js/src/modules/dateTime/utils.ts b/packages/i18nify-js/src/modules/dateTime/utils.ts index c67040c6..bed6b86e 100644 --- a/packages/i18nify-js/src/modules/dateTime/utils.ts +++ b/packages/i18nify-js/src/modules/dateTime/utils.ts @@ -34,19 +34,26 @@ export const stringToDate = (dateString: string): Date => { ); if (dateObj.getTime()) return dateObj; - else throw new Error('Invalid Date!'); + else + throw new Error( + `Invalid Date! The constructed date from the provided string "${dateString}" is invalid. Please ensure the date string is correct.`, + ); } catch (err) { if (err instanceof Error) { - throw new Error(err.message); + throw new Error( + `An error occurred while constructing the date: ${err.message}. Please ensure the date string "${dateString}" is in a supported format.`, + ); } else { - throw new Error(`An unknown error occurred = ${err}`); + throw new Error(`An unknown error occurred. Error details: ${err}`); } } } } // If no format matches, throw an error. - throw new Error('Date format not recognized'); + throw new Error( + `Date format not recognized. The provided date string "${dateString}" does not match any supported formats. Please use a recognized date format.`, + ); }; export const convertToStandardDate = (date: DateInput): Date => { diff --git a/packages/i18nify-js/src/modules/geo/getAllCountries.ts b/packages/i18nify-js/src/modules/geo/getAllCountries.ts index 8edddf74..805e3099 100644 --- a/packages/i18nify-js/src/modules/geo/getAllCountries.ts +++ b/packages/i18nify-js/src/modules/geo/getAllCountries.ts @@ -18,7 +18,9 @@ const getAllCountries = (): Promise< .then((res) => res.json()) .then((res) => res.metadata_information) .catch((err) => { - throw new Error(`Error in API response ${err}`); + throw new Error( + `An error occurred while fetching country metadata. The error details are: ${err.message}.`, + ); }); }; diff --git a/packages/i18nify-js/src/modules/geo/getCities.ts b/packages/i18nify-js/src/modules/geo/getCities.ts index 4c6584a7..4c3abfcf 100644 --- a/packages/i18nify-js/src/modules/geo/getCities.ts +++ b/packages/i18nify-js/src/modules/geo/getCities.ts @@ -21,7 +21,11 @@ const getCities = ( const stateCode = _stateCode && _stateCode.toUpperCase(); if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { - return Promise.reject(`Invalid country code: ${countryCode}`); + return Promise.reject( + new Error( + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code that is included in the supported list.`, + ), + ); } return fetch( @@ -39,14 +43,18 @@ const getCities = ( if (!res.states[stateCode]) { return Promise.reject( - `State code ${stateCode} missing in ${countryCode}`, + new Error( + `State code ${stateCode} is missing in ${countryCode}. Please ensure you provide a valid state code that exists within the specified country.`, + ), ); } return res.states[stateCode].cities; }) .catch((err) => { - throw new Error(`Error in API response ${err}`); + throw new Error( + `An error occurred while fetching city data. The error details are: ${err.message}.`, + ); }); }; diff --git a/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts b/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts index 856e3136..9e73c1ed 100644 --- a/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts +++ b/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts @@ -17,7 +17,9 @@ import { isCountryValid } from './utils'; */ const getFlagOfCountry = (_countryCode: CountryCodeType): GetFlagReturnType => { if (!isCountryValid(_countryCode)) { - throw new Error(`Invalid country code: ${_countryCode}`); + throw new Error( + `The provided country code is invalid. The received value was: ${_countryCode}. Please ensure you pass a valid country code that is included in the supported list.`, + ); } const countryCode = _countryCode.toLowerCase() as CountryCodeType; diff --git a/packages/i18nify-js/src/modules/geo/getStates.ts b/packages/i18nify-js/src/modules/geo/getStates.ts index 0e27bb1b..ef0c82f4 100644 --- a/packages/i18nify-js/src/modules/geo/getStates.ts +++ b/packages/i18nify-js/src/modules/geo/getStates.ts @@ -17,7 +17,11 @@ const getStates = (_countryCode: I18nifyCountryCodeType) => { const countryCode = _countryCode.toUpperCase(); if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { - return Promise.reject(`Invalid country code: ${countryCode}`); + return Promise.reject( + new Error( + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code that is included in the supported list.`, + ), + ); } return fetch( @@ -26,7 +30,9 @@ const getStates = (_countryCode: I18nifyCountryCodeType) => { .then((res) => res.json()) .then((res) => res.states) .catch((err) => { - throw new Error(`Error in API response ${err}`); + throw new Error( + `An error occurred while fetching state data. The error details are: ${err.message}.`, + ); }); }; diff --git a/packages/i18nify-js/src/modules/geo/getZipcodes.ts b/packages/i18nify-js/src/modules/geo/getZipcodes.ts index c074d8a3..8c1f5e20 100644 --- a/packages/i18nify-js/src/modules/geo/getZipcodes.ts +++ b/packages/i18nify-js/src/modules/geo/getZipcodes.ts @@ -40,7 +40,11 @@ const getZipcodes = (_countryCode: CountryCodeType, _stateCode?: string) => { const stateCode = _stateCode && _stateCode.toUpperCase(); if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { - return Promise.reject(`Invalid country code: ${countryCode}`); + return Promise.reject( + new Error( + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code that is included in the supported list.`, + ), + ); } return fetch( @@ -59,14 +63,16 @@ const getZipcodes = (_countryCode: CountryCodeType, _stateCode?: string) => { if (!res.states[stateCode]) { return Promise.reject( - `State code ${stateCode} missing in ${countryCode}`, + `State code ${stateCode} is missing in ${countryCode}. Please ensure you provide a valid state code that exists within the specified country.`, ); } return getZipcodesFromState(res, stateCode); }) .catch((err) => { - throw new Error(`Error in API response ${err}`); + throw new Error( + `An error occurred while fetching zipcode data. The error details are: ${err}.`, + ); }); }; diff --git a/packages/i18nify-js/src/modules/phoneNumber/formatPhoneNumber.ts b/packages/i18nify-js/src/modules/phoneNumber/formatPhoneNumber.ts index d4b84a0b..1a51719e 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/formatPhoneNumber.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/formatPhoneNumber.ts @@ -9,7 +9,10 @@ const formatPhoneNumber = ( countryCode?: CountryCodeType, ): string => { // Throw errors if phoneNumber is invalid - if (!phoneNumber) throw new Error('Parameter `phoneNumber` is invalid!'); + if (!phoneNumber) + throw new Error( + `Parameter 'phoneNumber' is invalid! The received value was: ${phoneNumber}. Please ensure you provide a valid phone number.`, + ); // Convert phoneNumber to string and clean it by removing non-numeric characters phoneNumber = phoneNumber.toString(); diff --git a/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts b/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts index 7bf9082a..aadd6f18 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts @@ -20,7 +20,10 @@ const getDialCodeByCountryCode = (countryCode: CountryCodeType): string => { * */ if (countryCode in dialCodeForAllCountries) return dialCodeForAllCountries[countryCode]; - else throw new Error(`Invalid countryCode: ${countryCode}`); + else + throw new Error( + `The provided country code is invalid. The received value was: ${countryCode}. Please ensure you pass a valid country code.`, + ); }; export default withErrorBoundary( diff --git a/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts b/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts index ef53d721..5a220b39 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts @@ -37,7 +37,9 @@ const getMaskedPhoneNumber = ({ } = maskingOptions; if (!countryCode && !phoneNumber) { - throw new Error('Either countryCode or phoneNumber is mandatory.'); + throw new Error( + `Either 'countryCode' or 'phoneNumber' is mandatory. Please provide a valid 'countryCode' or 'phoneNumber'.`, + ); } let maskedContactNumber: string; @@ -97,7 +99,9 @@ const getMaskedPhoneNumber = ({ // Retrieve the phone number formatting template using the country code maskedContactNumber = PHONE_FORMATTER_MAPPER[countryCode]; if (!maskedContactNumber) { - throw new Error(`Parameter "countryCode" is invalid: ${countryCode}`); + throw new Error( + `Parameter 'countryCode' is invalid. The received value was: ${countryCode}.`, + ); } dialCode = getDialCodeByCountryCode(countryCode); } diff --git a/packages/i18nify-js/src/modules/phoneNumber/parsePhoneNumber.ts b/packages/i18nify-js/src/modules/phoneNumber/parsePhoneNumber.ts index 0b6f4d6a..9959f335 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/parsePhoneNumber.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/parsePhoneNumber.ts @@ -18,7 +18,10 @@ const parsePhoneNumber = ( country?: CountryCodeType, ): PhoneInfo => { // Throw errors if phoneNumber is invalid - if (!phoneNumber) throw new Error('Parameter `phoneNumber` is invalid!'); + if (!phoneNumber) + throw new Error( + `Parameter 'phoneNumber' is invalid! The received value was: ${phoneNumber}. Please ensure you provide a valid phone number.`, + ); // Clean the phoneNumber by removing non-numeric characters phoneNumber = phoneNumber.toString(); From 8fd537b1cdfc7b481c000a002c7b564325d99b1d Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Wed, 3 Jul 2024 02:54:54 +0530 Subject: [PATCH 2/7] [fix]: fix failing UTs --- .../currency/__tests__/convertToMajorUnit.test.ts | 4 +++- .../currency/__tests__/convertToMinorUnit.test.ts | 4 +++- .../modules/currency/__tests__/formatNumber.test.ts | 11 ++++++----- .../currency/__tests__/formatNumberByParts.test.ts | 4 ++-- .../currency/__tests__/getCurrencySymbol.test.ts | 4 ++-- .../modules/dateTime/__tests__/formatDateTime.test.ts | 2 +- .../dateTime/__tests__/getRelativeTime.test.ts | 2 +- .../modules/dateTime/__tests__/getWeekdays.test.ts | 2 +- .../modules/dateTime/__tests__/parseDateTime.test.ts | 2 +- .../src/modules/geo/__tests__/getAllCountries.test.ts | 4 +++- .../src/modules/geo/__tests__/getCities.test.ts | 10 +++++++--- .../modules/geo/__tests__/getFlagOfCountry.test.ts | 6 ++++-- .../src/modules/geo/__tests__/getStates.test.ts | 8 ++++++-- .../src/modules/geo/__tests__/getZipcodes.test.ts | 10 +++++++--- .../phoneNumber/__tests__/formatPhoneNumber.test.ts | 2 +- .../__tests__/getDialCodeByCountryCode.test.ts | 4 +++- .../__tests__/getMaskedPhoneNumber.test.ts | 8 ++++++-- .../phoneNumber/__tests__/parsePhoneNumber.test.ts | 2 +- 18 files changed, 58 insertions(+), 31 deletions(-) diff --git a/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts index bb57c711..2699b17e 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts @@ -24,6 +24,8 @@ describe('currency - convertToMajorUnit', () => { convertToMajorUnit(100, { currency: unsupportedCurrencyCode as CurrencyCodeType, }); - }).toThrow('Unsupported currency XXX'); + }).toThrow( + `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code that is included in the supported list.`, + ); }); }); diff --git a/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts index e154db10..8920c58c 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts @@ -24,6 +24,8 @@ describe('currency - convertToMinorUnit', () => { convertToMinorUnit(100, { currency: unsupportedCurrencyCode as CurrencyCodeType, }); - }).toThrow('Unsupported currency XXX'); + }).toThrow( + `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code that is included in the supported list.`, + ); }); }); diff --git a/packages/i18nify-js/src/modules/currency/__tests__/formatNumber.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/formatNumber.test.ts index 5ffe7a2e..66670423 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/formatNumber.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/formatNumber.test.ts @@ -58,7 +58,7 @@ describe('formatNumber', () => { currency: 'USD', }), ).toThrow( - `Error: Parameter 'amount' is not a number. typeof amount: string`, + "Error: Parameter 'amount' is not a valid number. The received value was: invalid-amount of type string. Please ensure you pass a valid number.", ); }); @@ -92,7 +92,7 @@ describe('formatNumber', () => { it('should throw error with thousands separators', () => { expect(() => formatNumber('1,234,567.89', { currency: 'USD' })).toThrow( - `Error: Parameter 'amount' is not a number. typeof amount: string`, + "Error: Parameter 'amount' is not a valid number. The received value was: 1,234,567.89 of type string. Please ensure you pass a valid number.", ); }); @@ -103,7 +103,7 @@ describe('formatNumber', () => { intlOptions: { useGrouping: false }, }), ).toThrow( - `Error: Parameter 'amount' is not a number. typeof amount: string`, + "Error: Parameter 'amount' is not a valid number. The received value was: 1000,5 of type string. Please ensure you pass a valid number.", ); }); @@ -150,7 +150,8 @@ describe('formatNumber', () => { }); it('should rethrow the caught Error instance with the same message', () => { - const errorMessage = 'Error: Invalid currency code : undefined'; + const errorMessage = + 'Error: An error occurred while formatting the number: Invalid currency code : undefined'; expect(() => { formatNumber(123, { intlOptions: { currency: 'undefined' } } as any); @@ -162,7 +163,7 @@ describe('formatNumber', () => { formatNumber(123, { intlOptions: { style: 'hola' } } as any); }).toThrow( new Error( - 'Error: Value hola out of range for Intl.NumberFormat options property style', + 'Error: An error occurred while formatting the number: Value hola out of range for Intl.NumberFormat options property style', ), ); }); diff --git a/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts index 1f9dadd8..ac2fed98 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/formatNumberByParts.test.ts @@ -60,7 +60,7 @@ describe('formatNumberByParts', () => { locale: 'en-US', }), ).toThrow( - `Error: Parameter 'amount' is not a number. typeof amount: string`, + "Error: Parameter 'amount' is not a valid number. The received value was: not a number of type string. Please ensure you pass a valid number.", ); }); @@ -242,7 +242,7 @@ describe('formatNumberByParts', () => { formatNumberByParts(123, { intlOptions: { style: 'hola' } } as any); }).toThrow( new Error( - 'Error: Value hola out of range for Intl.NumberFormat options property style', + 'Error: An error occurred while formatting the number: Value hola out of range for Intl.NumberFormat options property style', ), ); }); diff --git a/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts index bb16ff24..a24acc62 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts @@ -11,14 +11,14 @@ describe('getCurrencySymbol', () => { it('should throw Error for an invalid currency code', () => { const currencyCode = 'XYZ'; // An invalid code expect(() => getCurrencySymbol(currencyCode as CurrencyCodeType)).toThrow( - 'Error: Invalid currencyCode: XYZ', + `Error: The provided currency code is invalid. The received value was: ${currencyCode}. Please ensure you pass a valid currency code that is included in the supported list.`, ); }); it('should throw Error for an empty string', () => { const currencyCode = ''; expect(() => getCurrencySymbol(currencyCode as CurrencyCodeType)).toThrow( - 'Error: Invalid currencyCode: ', + 'Error: The provided currency code is invalid. The received value was: . Please ensure you pass a valid currency code that is included in the supported list.', ); }); }); diff --git a/packages/i18nify-js/src/modules/dateTime/__tests__/formatDateTime.test.ts b/packages/i18nify-js/src/modules/dateTime/__tests__/formatDateTime.test.ts index e5749195..8e691854 100644 --- a/packages/i18nify-js/src/modules/dateTime/__tests__/formatDateTime.test.ts +++ b/packages/i18nify-js/src/modules/dateTime/__tests__/formatDateTime.test.ts @@ -251,7 +251,7 @@ describe('dateTime - formatDateTime', () => { intlOptions: { weekday: 'dummy' } as any, }), ).toThrow( - 'Error: Value dummy out of range for Intl.DateTimeFormat options property weekday', + 'Error: An error occurred while creating the DateFormatter instance: Value dummy out of range for Intl.DateTimeFormat options property weekday. Please ensure the provided options are valid and try again.', ); }); diff --git a/packages/i18nify-js/src/modules/dateTime/__tests__/getRelativeTime.test.ts b/packages/i18nify-js/src/modules/dateTime/__tests__/getRelativeTime.test.ts index fcef6867..d9e54ef6 100644 --- a/packages/i18nify-js/src/modules/dateTime/__tests__/getRelativeTime.test.ts +++ b/packages/i18nify-js/src/modules/dateTime/__tests__/getRelativeTime.test.ts @@ -59,7 +59,7 @@ describe('dateTime - getRelativeTime', () => { baseDate: oneDayAgo, }), ).toThrow( - 'Error: Value dummy out of range for Intl.RelativeTimeFormat options property style', + 'Error: An error occurred while creating the RelativeTimeFormat instance: Value dummy out of range for Intl.RelativeTimeFormat options property style. Please ensure the provided options are valid and try again.', ); }); diff --git a/packages/i18nify-js/src/modules/dateTime/__tests__/getWeekdays.test.ts b/packages/i18nify-js/src/modules/dateTime/__tests__/getWeekdays.test.ts index 2204bed7..c29a0f16 100644 --- a/packages/i18nify-js/src/modules/dateTime/__tests__/getWeekdays.test.ts +++ b/packages/i18nify-js/src/modules/dateTime/__tests__/getWeekdays.test.ts @@ -66,7 +66,7 @@ describe('dateTime - getWeekdays', () => { weekday: 'dummy' as any, }), ).toThrow( - 'Error: Value dummy out of range for Intl.DateTimeFormat options property weekday', + 'Error: An error occurred while creating the DateFormatter instance or formatting the weekdays: Value dummy out of range for Intl.DateTimeFormat options property weekday. Please ensure the provided options are valid and try again.', ); }); }); diff --git a/packages/i18nify-js/src/modules/dateTime/__tests__/parseDateTime.test.ts b/packages/i18nify-js/src/modules/dateTime/__tests__/parseDateTime.test.ts index 4524527b..bcd5357f 100644 --- a/packages/i18nify-js/src/modules/dateTime/__tests__/parseDateTime.test.ts +++ b/packages/i18nify-js/src/modules/dateTime/__tests__/parseDateTime.test.ts @@ -133,7 +133,7 @@ describe('dateTime - parseDateTime', () => { intlOptions: { weekday: 'dummy' } as any, }), ).toThrow( - 'Error: Value dummy out of range for Intl.DateTimeFormat options property weekday', + 'Error: An error occurred while parsing the date: Value dummy out of range for Intl.DateTimeFormat options property weekday. Please ensure the provided date and options are valid and try again.', ); }); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getAllCountries.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getAllCountries.test.ts index e4d5bc35..b57686ad 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getAllCountries.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getAllCountries.test.ts @@ -25,6 +25,8 @@ describe('getAllCountries', () => { it('handles API errors', async () => { global.fetch = jest.fn(() => Promise.reject('API Error')); - await expect(getAllCountries()).rejects.toThrow('Error in API response'); + await expect(getAllCountries()).rejects.toThrow( + 'An error occurred while fetching country metadata. The error details are: undefined.', + ); }); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts index a30bd7ed..41c4755c 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts @@ -30,7 +30,9 @@ describe('getCities', () => { const invalidCountryCode = 'XYZ'; // @ts-expect-error invalid country code for testing await expect(getCities(invalidCountryCode)).rejects.toEqual( - `Invalid country code: ${invalidCountryCode}`, + new Error( + `Invalid country code: XYZ. Please ensure you provide a valid country code that is included in the supported list.`, + ), ); }); @@ -38,12 +40,14 @@ describe('getCities', () => { const validCountryCode = 'IN'; const missingStateCode = 'XYZ'; await expect(getCities(validCountryCode, missingStateCode)).rejects.toThrow( - `State code ${missingStateCode} missing in ${validCountryCode}`, + `An error occurred while fetching city data. The error details are: State code ${missingStateCode} is missing in ${validCountryCode}. Please ensure you provide a valid state code that exists within the specified country..`, ); }); it('handles API errors', async () => { global.fetch = jest.fn(() => Promise.reject('API Error')); - await expect(getCities('IN')).rejects.toThrow('Error in API response'); + await expect(getCities('IN')).rejects.toThrow( + 'An error occurred while fetching city data. The error details are: undefined.', + ); }); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts index 608bed01..db411832 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts @@ -18,12 +18,14 @@ describe('geo - getFlagOfCountry', () => { const sampleInvalidCodes = 'XX'; expect(() => getFlagOfCountry(sampleInvalidCodes as CountryCodeType), - ).toThrow(`Invalid country code: ${sampleInvalidCodes}`); + ).toThrow( + `Error: The provided country code is invalid. The received value was: ${sampleInvalidCodes}. Please ensure you pass a valid country code that is included in the supported list.`, + ); }); it('should throw an error for an empty string', () => { expect(() => getFlagOfCountry('' as CountryCodeType)).toThrow( - 'Invalid country code: ', + 'Error: The provided country code is invalid. The received value was: . Please ensure you pass a valid country code that is included in the supported list.', ); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts index 758274ed..8c53d405 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts @@ -21,12 +21,16 @@ describe('getStates', () => { it('throws error for invalid country code', async () => { // @ts-expect-error invalid state code for testing await expect(() => getStates('XYZ')).rejects.toEqual( - `Invalid country code: XYZ`, + new Error( + `Invalid country code: XYZ. Please ensure you provide a valid country code that is included in the supported list.`, + ), ); }); it('handles API errors', async () => { global.fetch = jest.fn(() => Promise.reject('API Error')); - await expect(getStates('IN')).rejects.toThrow('Error in API response'); + await expect(getStates('IN')).rejects.toThrow( + 'An error occurred while fetching state data. The error details are: undefined.', + ); }); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts index 96121e5c..84f2207d 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts @@ -42,7 +42,9 @@ describe('getZipcodes', () => { const invalidCountryCode = 'XYZ'; // @ts-expect-error invalid country code for testing await expect(getZipcodes(invalidCountryCode)).rejects.toEqual( - `Invalid country code: ${invalidCountryCode}`, + new Error( + `Invalid country code: XYZ. Please ensure you provide a valid country code that is included in the supported list.`, + ), ); }); @@ -52,12 +54,14 @@ describe('getZipcodes', () => { await expect( getZipcodes(validCountryCode, missingStateCode), ).rejects.toThrow( - `State code ${missingStateCode} missing in ${validCountryCode}`, + `An error occurred while fetching zipcode data. The error details are: State code ${missingStateCode} is missing in ${validCountryCode}. Please ensure you provide a valid state code that exists within the specified country..`, ); }); it('handles API errors', async () => { global.fetch = jest.fn(() => Promise.reject('API Error')); - await expect(getZipcodes('IN')).rejects.toThrow('Error in API response'); + await expect(getZipcodes('IN')).rejects.toThrow( + 'An error occurred while fetching zipcode data. The error details are: API Error.', + ); }); }); diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/formatPhoneNumber.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/formatPhoneNumber.test.ts index 4a49d7d6..6e1af16d 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/formatPhoneNumber.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/formatPhoneNumber.test.ts @@ -26,7 +26,7 @@ describe('formatPhoneNumber', () => { const phoneNumber = ''; const countryCode = 'MY'; expect(() => formatPhoneNumber(phoneNumber, countryCode)).toThrow( - 'Parameter `phoneNumber` is invalid!', + "Error: Parameter 'phoneNumber' is invalid! The received value was: . Please ensure you provide a valid phone number.", ); }); }); diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts index d1fcacbf..4cc84eb1 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts @@ -20,7 +20,9 @@ describe('phoneNumber - getDialCodeByCountryCode', () => { it('should throw an error for invalid country codes', () => { const invalidCountryCode = 'XX'; // XX is not a valid country code expect(() => getDialCodeByCountryCode(invalidCountryCode as any)).toThrow( - `Invalid countryCode: ${invalidCountryCode}`, + new Error( + `Error: The provided country code is invalid. The received value was: ${invalidCountryCode}. Please ensure you pass a valid country code.`, + ), ); }); }); diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getMaskedPhoneNumber.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getMaskedPhoneNumber.test.ts index 29aadd7d..a4975e28 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getMaskedPhoneNumber.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getMaskedPhoneNumber.test.ts @@ -6,13 +6,17 @@ describe('phoneNumber - getMaskedPhoneNumber', () => { it('should throw error if no countryCode and phoneNumber are provided', () => { expect(() => getMaskedPhoneNumber({} as GetMaskedPhoneNumberOptions), - ).toThrow('Either countryCode or phoneNumber is mandatory.'); + ).toThrow( + "Error: Either 'countryCode' or 'phoneNumber' is mandatory. Please provide a valid 'countryCode' or 'phoneNumber'.", + ); }); it('should throw error for invalid country code when phone number is not provided', () => { expect(() => getMaskedPhoneNumber({ countryCode: 'ZZ' as CountryCodeType }), - ).toThrow('Parameter "countryCode" is invalid: ZZ'); + ).toThrow( + "Error: Parameter 'countryCode' is invalid. The received value was: ZZ.", + ); }); it('should handle invalid country code when phone number is provided', () => { diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/parsePhoneNumber.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/parsePhoneNumber.test.ts index c15005de..4cce88c9 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/parsePhoneNumber.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/parsePhoneNumber.test.ts @@ -44,7 +44,7 @@ describe('parsePhoneNumber function', () => { const phoneNumber = ''; expect(() => parsePhoneNumber(phoneNumber)).toThrow( - 'Parameter `phoneNumber` is invalid!', + "Error: Parameter 'phoneNumber' is invalid! The received value was: . Please ensure you provide a valid phone number.", ); }); }); From c4302a3a95e26b5e8f56b01c5c13596b3b234018 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Wed, 3 Jul 2024 02:59:26 +0530 Subject: [PATCH 3/7] Create mighty-oranges-behave.md --- .changeset/mighty-oranges-behave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/mighty-oranges-behave.md diff --git a/.changeset/mighty-oranges-behave.md b/.changeset/mighty-oranges-behave.md new file mode 100644 index 00000000..a2364294 --- /dev/null +++ b/.changeset/mighty-oranges-behave.md @@ -0,0 +1,5 @@ +--- +"@razorpay/i18nify-js": patch +--- + +enhance error message clarity and verbosity From 8dd0eae6ebf421a3933d790c87e6808e8f9c77ca Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Wed, 3 Jul 2024 03:01:39 +0530 Subject: [PATCH 4/7] minor refactoring --- .../src/modules/currency/__tests__/convertToMajorUnit.test.ts | 2 +- .../src/modules/currency/__tests__/convertToMinorUnit.test.ts | 2 +- .../src/modules/currency/__tests__/getCurrencySymbol.test.ts | 4 ++-- .../i18nify-js/src/modules/currency/convertToMajorUnit.ts | 2 +- .../i18nify-js/src/modules/currency/convertToMinorUnit.ts | 2 +- packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts index 2699b17e..55a995a7 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts @@ -25,7 +25,7 @@ describe('currency - convertToMajorUnit', () => { currency: unsupportedCurrencyCode as CurrencyCodeType, }); }).toThrow( - `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code that is included in the supported list.`, + `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code.`, ); }); }); diff --git a/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts index 8920c58c..a7b660de 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts @@ -25,7 +25,7 @@ describe('currency - convertToMinorUnit', () => { currency: unsupportedCurrencyCode as CurrencyCodeType, }); }).toThrow( - `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code that is included in the supported list.`, + `Error: The provided currency code is either empty or not supported. The received value was : ${unsupportedCurrencyCode}. Please ensure you pass a valid currency code.`, ); }); }); diff --git a/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts b/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts index a24acc62..c5e548d3 100644 --- a/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts +++ b/packages/i18nify-js/src/modules/currency/__tests__/getCurrencySymbol.test.ts @@ -11,14 +11,14 @@ describe('getCurrencySymbol', () => { it('should throw Error for an invalid currency code', () => { const currencyCode = 'XYZ'; // An invalid code expect(() => getCurrencySymbol(currencyCode as CurrencyCodeType)).toThrow( - `Error: The provided currency code is invalid. The received value was: ${currencyCode}. Please ensure you pass a valid currency code that is included in the supported list.`, + `Error: The provided currency code is invalid. The received value was: ${currencyCode}. Please ensure you pass a valid currency code.`, ); }); it('should throw Error for an empty string', () => { const currencyCode = ''; expect(() => getCurrencySymbol(currencyCode as CurrencyCodeType)).toThrow( - 'Error: The provided currency code is invalid. The received value was: . Please ensure you pass a valid currency code that is included in the supported list.', + 'Error: The provided currency code is invalid. The received value was: . Please ensure you pass a valid currency code.', ); }); }); diff --git a/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts index d014c6fb..60528cf8 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts @@ -24,7 +24,7 @@ const convertToMajorUnit = ( if (!options.currency || !currencyInfo) { throw new Error( - `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code that is included in the supported list.`, + `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code.`, ); } diff --git a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts index 57418279..364d07af 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts @@ -24,7 +24,7 @@ const convertToMinorUnit = ( if (!options.currency || !currencyInfo) { throw new Error( - `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code that is included in the supported list.`, + `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code.`, ); } diff --git a/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts b/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts index 2651e921..c7f5ba78 100644 --- a/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts +++ b/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts @@ -8,7 +8,7 @@ const getCurrencySymbol = (currencyCode: CurrencyCodeType): string => { return currencyInformation[currencyCode]?.symbol; else throw new Error( - `The provided currency code is invalid. The received value was: ${String(currencyCode)}. Please ensure you pass a valid currency code that is included in the supported list.`, + `The provided currency code is invalid. The received value was: ${String(currencyCode)}. Please ensure you pass a valid currency code.`, ); }; From 2aba6321dd24327a601f56f7d0bcbd4637580c13 Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Wed, 3 Jul 2024 03:06:39 +0530 Subject: [PATCH 5/7] minor refactoring --- .../i18nify-js/src/modules/geo/__tests__/getCities.test.ts | 2 +- .../src/modules/geo/__tests__/getFlagOfCountry.test.ts | 4 ++-- .../i18nify-js/src/modules/geo/__tests__/getStates.test.ts | 2 +- .../i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts | 2 +- packages/i18nify-js/src/modules/geo/getCities.ts | 2 +- packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts | 2 +- packages/i18nify-js/src/modules/geo/getStates.ts | 2 +- packages/i18nify-js/src/modules/geo/getZipcodes.ts | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts index 41c4755c..ad468604 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts @@ -31,7 +31,7 @@ describe('getCities', () => { // @ts-expect-error invalid country code for testing await expect(getCities(invalidCountryCode)).rejects.toEqual( new Error( - `Invalid country code: XYZ. Please ensure you provide a valid country code that is included in the supported list.`, + `Invalid country code: XYZ. Please ensure you provide a valid country code.`, ), ); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts index db411832..14890d0e 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getFlagOfCountry.test.ts @@ -19,13 +19,13 @@ describe('geo - getFlagOfCountry', () => { expect(() => getFlagOfCountry(sampleInvalidCodes as CountryCodeType), ).toThrow( - `Error: The provided country code is invalid. The received value was: ${sampleInvalidCodes}. Please ensure you pass a valid country code that is included in the supported list.`, + `Error: The provided country code is invalid. The received value was: ${sampleInvalidCodes}. Please ensure you pass a valid country code.`, ); }); it('should throw an error for an empty string', () => { expect(() => getFlagOfCountry('' as CountryCodeType)).toThrow( - 'Error: The provided country code is invalid. The received value was: . Please ensure you pass a valid country code that is included in the supported list.', + 'Error: The provided country code is invalid. The received value was: . Please ensure you pass a valid country code.', ); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts index 8c53d405..ea820f2b 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts @@ -22,7 +22,7 @@ describe('getStates', () => { // @ts-expect-error invalid state code for testing await expect(() => getStates('XYZ')).rejects.toEqual( new Error( - `Invalid country code: XYZ. Please ensure you provide a valid country code that is included in the supported list.`, + `Invalid country code: XYZ. Please ensure you provide a valid country code.`, ), ); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts index 84f2207d..14df2d5a 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts @@ -43,7 +43,7 @@ describe('getZipcodes', () => { // @ts-expect-error invalid country code for testing await expect(getZipcodes(invalidCountryCode)).rejects.toEqual( new Error( - `Invalid country code: XYZ. Please ensure you provide a valid country code that is included in the supported list.`, + `Invalid country code: XYZ. Please ensure you provide a valid country code.`, ), ); }); diff --git a/packages/i18nify-js/src/modules/geo/getCities.ts b/packages/i18nify-js/src/modules/geo/getCities.ts index 4c3abfcf..69d23d43 100644 --- a/packages/i18nify-js/src/modules/geo/getCities.ts +++ b/packages/i18nify-js/src/modules/geo/getCities.ts @@ -23,7 +23,7 @@ const getCities = ( if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { return Promise.reject( new Error( - `Invalid country code: ${countryCode}. Please ensure you provide a valid country code that is included in the supported list.`, + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code.`, ), ); } diff --git a/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts b/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts index 9e73c1ed..a3273b1e 100644 --- a/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts +++ b/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts @@ -18,7 +18,7 @@ import { isCountryValid } from './utils'; const getFlagOfCountry = (_countryCode: CountryCodeType): GetFlagReturnType => { if (!isCountryValid(_countryCode)) { throw new Error( - `The provided country code is invalid. The received value was: ${_countryCode}. Please ensure you pass a valid country code that is included in the supported list.`, + `The provided country code is invalid. The received value was: ${_countryCode}. Please ensure you pass a valid country code.`, ); } diff --git a/packages/i18nify-js/src/modules/geo/getStates.ts b/packages/i18nify-js/src/modules/geo/getStates.ts index ef0c82f4..513babd2 100644 --- a/packages/i18nify-js/src/modules/geo/getStates.ts +++ b/packages/i18nify-js/src/modules/geo/getStates.ts @@ -19,7 +19,7 @@ const getStates = (_countryCode: I18nifyCountryCodeType) => { if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { return Promise.reject( new Error( - `Invalid country code: ${countryCode}. Please ensure you provide a valid country code that is included in the supported list.`, + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code.`, ), ); } diff --git a/packages/i18nify-js/src/modules/geo/getZipcodes.ts b/packages/i18nify-js/src/modules/geo/getZipcodes.ts index 8c1f5e20..75805352 100644 --- a/packages/i18nify-js/src/modules/geo/getZipcodes.ts +++ b/packages/i18nify-js/src/modules/geo/getZipcodes.ts @@ -42,7 +42,7 @@ const getZipcodes = (_countryCode: CountryCodeType, _stateCode?: string) => { if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { return Promise.reject( new Error( - `Invalid country code: ${countryCode}. Please ensure you provide a valid country code that is included in the supported list.`, + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code.`, ), ); } From 2e497752ea39f634b1ca14eddf27fea4493215e3 Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Fri, 26 Jul 2024 02:55:15 +0530 Subject: [PATCH 6/7] resolve review comments --- .../i18nify-js/src/modules/currency/convertToMajorUnit.ts | 2 +- .../i18nify-js/src/modules/currency/convertToMinorUnit.ts | 2 +- packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts | 2 +- packages/i18nify-js/src/modules/dateTime/getWeekdays.ts | 1 - .../i18nify-js/src/modules/geo/__tests__/getCities.test.ts | 2 +- .../i18nify-js/src/modules/geo/__tests__/getStates.test.ts | 2 +- .../i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts | 2 +- packages/i18nify-js/src/modules/geo/getCities.ts | 2 +- packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts | 2 +- packages/i18nify-js/src/modules/geo/getStates.ts | 2 +- packages/i18nify-js/src/modules/geo/getZipcodes.ts | 2 +- .../phoneNumber/__tests__/getDialCodeByCountryCode.test.ts | 2 +- .../src/modules/phoneNumber/getDialCodeByCountryCode.ts | 2 +- .../src/modules/phoneNumber/getMaskedPhoneNumber.ts | 4 ++-- 14 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts index 60528cf8..6a462d0b 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts @@ -24,7 +24,7 @@ const convertToMajorUnit = ( if (!options.currency || !currencyInfo) { throw new Error( - `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code.`, + `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code. Check valid currency codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/currency/data.json`, ); } diff --git a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts index 364d07af..39765438 100644 --- a/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts +++ b/packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts @@ -24,7 +24,7 @@ const convertToMinorUnit = ( if (!options.currency || !currencyInfo) { throw new Error( - `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code.`, + `The provided currency code is either empty or not supported. The received value was ${(options.currency as any) === '' ? 'an empty string' : `: ${String(options.currency)}`}. Please ensure you pass a valid currency code. Check valid currency codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/currency/data.json`, ); } diff --git a/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts b/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts index c7f5ba78..999cbf89 100644 --- a/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts +++ b/packages/i18nify-js/src/modules/currency/getCurrencySymbol.ts @@ -8,7 +8,7 @@ const getCurrencySymbol = (currencyCode: CurrencyCodeType): string => { return currencyInformation[currencyCode]?.symbol; else throw new Error( - `The provided currency code is invalid. The received value was: ${String(currencyCode)}. Please ensure you pass a valid currency code.`, + `The provided currency code is invalid. The received value was: ${String(currencyCode)}. Please ensure you pass a valid currency code. Check valid currency codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/currency/data.json`, ); }; diff --git a/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts b/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts index 54d69cf0..f106cfec 100644 --- a/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts +++ b/packages/i18nify-js/src/modules/dateTime/getWeekdays.ts @@ -14,7 +14,6 @@ const getWeekdays = (options: { locale?: Locale; weekday?: 'long' | 'short' | 'narrow' | undefined; }): string[] => { - console.log({ options }); try { const locale = getLocale(options); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts index ad468604..47564666 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getCities.test.ts @@ -31,7 +31,7 @@ describe('getCities', () => { // @ts-expect-error invalid country code for testing await expect(getCities(invalidCountryCode)).rejects.toEqual( new Error( - `Invalid country code: XYZ. Please ensure you provide a valid country code.`, + `Invalid country code: XYZ. Please ensure you provide a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ), ); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts index ea820f2b..e1a11e19 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getStates.test.ts @@ -22,7 +22,7 @@ describe('getStates', () => { // @ts-expect-error invalid state code for testing await expect(() => getStates('XYZ')).rejects.toEqual( new Error( - `Invalid country code: XYZ. Please ensure you provide a valid country code.`, + `Invalid country code: XYZ. Please ensure you provide a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ), ); }); diff --git a/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts b/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts index 14df2d5a..da7d01d9 100644 --- a/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts +++ b/packages/i18nify-js/src/modules/geo/__tests__/getZipcodes.test.ts @@ -54,7 +54,7 @@ describe('getZipcodes', () => { await expect( getZipcodes(validCountryCode, missingStateCode), ).rejects.toThrow( - `An error occurred while fetching zipcode data. The error details are: State code ${missingStateCode} is missing in ${validCountryCode}. Please ensure you provide a valid state code that exists within the specified country..`, + `An error occurred while fetching zipcode data. The error details are: State code ${missingStateCode} is missing in ${validCountryCode}. Please ensure you provide a valid state code that exists within the specified country. Check valid state codes and country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json.`, ); }); diff --git a/packages/i18nify-js/src/modules/geo/getCities.ts b/packages/i18nify-js/src/modules/geo/getCities.ts index 69d23d43..c204d1f8 100644 --- a/packages/i18nify-js/src/modules/geo/getCities.ts +++ b/packages/i18nify-js/src/modules/geo/getCities.ts @@ -23,7 +23,7 @@ const getCities = ( if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { return Promise.reject( new Error( - `Invalid country code: ${countryCode}. Please ensure you provide a valid country code.`, + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ), ); } diff --git a/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts b/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts index a3273b1e..af642f3a 100644 --- a/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts +++ b/packages/i18nify-js/src/modules/geo/getFlagOfCountry.ts @@ -18,7 +18,7 @@ import { isCountryValid } from './utils'; const getFlagOfCountry = (_countryCode: CountryCodeType): GetFlagReturnType => { if (!isCountryValid(_countryCode)) { throw new Error( - `The provided country code is invalid. The received value was: ${_countryCode}. Please ensure you pass a valid country code.`, + `The provided country code is invalid. The received value was: ${_countryCode}. Please ensure you pass a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ); } diff --git a/packages/i18nify-js/src/modules/geo/getStates.ts b/packages/i18nify-js/src/modules/geo/getStates.ts index 513babd2..a2778e7e 100644 --- a/packages/i18nify-js/src/modules/geo/getStates.ts +++ b/packages/i18nify-js/src/modules/geo/getStates.ts @@ -19,7 +19,7 @@ const getStates = (_countryCode: I18nifyCountryCodeType) => { if (!I18NIFY_DATA_SUPPORTED_COUNTRIES.includes(countryCode)) { return Promise.reject( new Error( - `Invalid country code: ${countryCode}. Please ensure you provide a valid country code.`, + `Invalid country code: ${countryCode}. Please ensure you provide a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ), ); } diff --git a/packages/i18nify-js/src/modules/geo/getZipcodes.ts b/packages/i18nify-js/src/modules/geo/getZipcodes.ts index 75805352..6c770a78 100644 --- a/packages/i18nify-js/src/modules/geo/getZipcodes.ts +++ b/packages/i18nify-js/src/modules/geo/getZipcodes.ts @@ -63,7 +63,7 @@ const getZipcodes = (_countryCode: CountryCodeType, _stateCode?: string) => { if (!res.states[stateCode]) { return Promise.reject( - `State code ${stateCode} is missing in ${countryCode}. Please ensure you provide a valid state code that exists within the specified country.`, + `State code ${stateCode} is missing in ${countryCode}. Please ensure you provide a valid state code that exists within the specified country. Check valid state codes and country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ); } diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts index 4cc84eb1..70f86039 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeByCountryCode.test.ts @@ -21,7 +21,7 @@ describe('phoneNumber - getDialCodeByCountryCode', () => { const invalidCountryCode = 'XX'; // XX is not a valid country code expect(() => getDialCodeByCountryCode(invalidCountryCode as any)).toThrow( new Error( - `Error: The provided country code is invalid. The received value was: ${invalidCountryCode}. Please ensure you pass a valid country code.`, + `Error: The provided country code is invalid. The received value was: ${invalidCountryCode}. Please ensure you pass a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ), ); }); diff --git a/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts b/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts index aadd6f18..743b40c2 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/getDialCodeByCountryCode.ts @@ -22,7 +22,7 @@ const getDialCodeByCountryCode = (countryCode: CountryCodeType): string => { return dialCodeForAllCountries[countryCode]; else throw new Error( - `The provided country code is invalid. The received value was: ${countryCode}. Please ensure you pass a valid country code.`, + `The provided country code is invalid. The received value was: ${countryCode}. Please ensure you pass a valid country code. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ); }; diff --git a/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts b/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts index 5a220b39..f7898cee 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts @@ -38,7 +38,7 @@ const getMaskedPhoneNumber = ({ if (!countryCode && !phoneNumber) { throw new Error( - `Either 'countryCode' or 'phoneNumber' is mandatory. Please provide a valid 'countryCode' or 'phoneNumber'.`, + `Either 'countryCode' or 'phoneNumber' is mandatory. Please provide a valid 'countryCode' or 'phoneNumber'. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ); } @@ -100,7 +100,7 @@ const getMaskedPhoneNumber = ({ maskedContactNumber = PHONE_FORMATTER_MAPPER[countryCode]; if (!maskedContactNumber) { throw new Error( - `Parameter 'countryCode' is invalid. The received value was: ${countryCode}.`, + `Parameter 'countryCode' is invalid. The received value was: ${countryCode}. Check valid country codes here: https://github.com/razorpay/i18nify/blob/master/i18nify-data/country/metadata/data.json`, ); } dialCode = getDialCodeByCountryCode(countryCode); From a75fe67b961d80b878c556f8e257c298f6b0ea19 Mon Sep 17 00:00:00 2001 From: RgnDunes Date: Fri, 26 Jul 2024 02:57:09 +0530 Subject: [PATCH 7/7] resolve review comments --- .changeset/mighty-oranges-behave.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/mighty-oranges-behave.md b/.changeset/mighty-oranges-behave.md index a2364294..28361447 100644 --- a/.changeset/mighty-oranges-behave.md +++ b/.changeset/mighty-oranges-behave.md @@ -1,5 +1,5 @@ --- -"@razorpay/i18nify-js": patch +'@razorpay/i18nify-js': minor --- enhance error message clarity and verbosity