From 634b88ca1a28f75ee838ddafd039d1f69d0ba83f Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Mon, 24 Nov 2025 10:12:17 +0530 Subject: [PATCH 1/4] Refactor error and warning messages for clarity and consistency across stack, cache provider, and utility functions. Update deprecation warning in entry module to provide clearer guidance for removal. --- src/core/cache-provider/index.js | 2 +- src/core/lib/utils.js | 6 +++--- src/core/modules/entry.js | 2 +- src/core/stack.js | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/core/cache-provider/index.js b/src/core/cache-provider/index.js index 7f12b9be..b4e1131f 100755 --- a/src/core/cache-provider/index.js +++ b/src/core/cache-provider/index.js @@ -5,7 +5,7 @@ CacheProvider.providers = function (provider) { if (provider) { return localstorage; } else { - console.error('Kindly provide valid provider.'); + console.error('Missing cache provider. Provide a valid provider and try again.'); } }; diff --git a/src/core/lib/utils.js b/src/core/lib/utils.js index 4aaa9330..72141314 100755 --- a/src/core/lib/utils.js +++ b/src/core/lib/utils.js @@ -28,7 +28,7 @@ export function transform (type) { this._query[type].BASE = query; return this; } else { - console.error('Kindly provide valid parameters'); + console.error('Invalid parameters. Expected a string or an array of field names.'); } break; case 2: @@ -38,11 +38,11 @@ export function transform (type) { this._query[type][arguments[0]] = query; return this; } else { - console.error('Kindly provide valid parameters'); + console.error('Invalid parameters. Expected first parameter as a string (reference field UID) and second parameter as a string or an array of field names.'); } break; default: - console.error('Kindly provide valid parameters'); + console.error('Invalid parameters. Provide either one parameter (field name or array) or two parameters (reference field UID and field name or array).'); } }; } diff --git a/src/core/modules/entry.js b/src/core/modules/entry.js index 48de0083..33b60603 100755 --- a/src/core/modules/entry.js +++ b/src/core/modules/entry.js @@ -294,7 +294,7 @@ export default class Entry { * @instance */ includeOwner () { - console.warn('The includeOwner function is deprecated.'); + console.warn('The includeOwner function is deprecated. This functionality is no longer supported. Please remove this method from your code.'); this._query.include_owner = true; return this; } diff --git a/src/core/stack.js b/src/core/stack.js index dea47b8e..7f650871 100755 --- a/src/core/stack.js +++ b/src/core/stack.js @@ -57,11 +57,11 @@ export default class Stack { debug: false, logHandler: (level, data) => { if (level === 'error' && data) { - console.error(`[error] ${data}`); + console.error(`Error: ${data}`); } else if (level === 'warning' && data) { - console.warn(`[warning] ${data}`); + console.warn(`Warning: ${data}`); } else if (level === 'info' && data) { - console.info(`[info] ${data}`); + console.info(`Info: ${data}`); } } }; @@ -426,7 +426,7 @@ export default class Stack { * .then(function(result) { * // 'result' is a single contentType information. * }).catch((error) => { - * console.log(error) + * console.error('An error occurred:', error.message || error) * }); * @returns {promise} * @instance From 438aa00166b97f3bdb31bf12bc471833819a55f3 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Mon, 24 Nov 2025 10:47:37 +0530 Subject: [PATCH 2/4] Refactor error handling in stack, cache provider, and entry modules to utilize centralized message constants for improved maintainability and clarity. --- src/core/cache-provider/index.js | 4 +++- src/core/lib/utils.js | 7 ++++--- src/core/messages.js | 35 ++++++++++++++++++++++++++++++++ src/core/modules/entry.js | 15 +++++++------- src/core/stack.js | 21 ++++++++++--------- 5 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 src/core/messages.js diff --git a/src/core/cache-provider/index.js b/src/core/cache-provider/index.js index b4e1131f..654f33d2 100755 --- a/src/core/cache-provider/index.js +++ b/src/core/cache-provider/index.js @@ -1,11 +1,13 @@ import localstorage from './localstorage'; +import MESSAGES from './messages'; + const CacheProvider = {}; CacheProvider.providers = function (provider) { if (provider) { return localstorage; } else { - console.error('Missing cache provider. Provide a valid provider and try again.'); + console.error(MESSAGES.CACHE_PROVIDER_MISSING); } }; diff --git a/src/core/lib/utils.js b/src/core/lib/utils.js index 72141314..a40b0e8a 100755 --- a/src/core/lib/utils.js +++ b/src/core/lib/utils.js @@ -1,5 +1,6 @@ import Request from './request'; import Result from '../modules/result'; +import MESSAGES from '../messages'; /** * @method addSpread @@ -28,7 +29,7 @@ export function transform (type) { this._query[type].BASE = query; return this; } else { - console.error('Invalid parameters. Expected a string or an array of field names.'); + console.error(MESSAGES.TRANSFORM_INVALID_SINGLE_PARAM); } break; case 2: @@ -38,11 +39,11 @@ export function transform (type) { this._query[type][arguments[0]] = query; return this; } else { - console.error('Invalid parameters. Expected first parameter as a string (reference field UID) and second parameter as a string or an array of field names.'); + console.error(MESSAGES.TRANSFORM_INVALID_DOUBLE_PARAM); } break; default: - console.error('Invalid parameters. Provide either one parameter (field name or array) or two parameters (reference field UID and field name or array).'); + console.error(MESSAGES.TRANSFORM_INVALID_PARAM_COUNT); } }; } diff --git a/src/core/messages.js b/src/core/messages.js new file mode 100644 index 00000000..6196d4dc --- /dev/null +++ b/src/core/messages.js @@ -0,0 +1,35 @@ +/** + * Centralized error and informational messages for Contentstack JavaScript SDK + * @module messages + */ + +const MESSAGES = { + // Cache Provider Messages + CACHE_PROVIDER_MISSING: 'Missing cache provider. Provide a valid provider and try again.', + CACHE_POLICY_INVALID: 'Invalid cache policy. Provide a valid policy value and try again.', + + // Stack Initialization Messages + STACK_INVALID_PARAMS_OBJECT: 'Invalid parameters. The specified API Key, Delivery Token, or Environment Name is invalid.', + STACK_INVALID_PARAMS_STRING: 'Invalid string parameters. Provide valid API Key, Delivery Token, and Environment Name.', + STACK_INVALID_PARAMS_GENERIC: 'Invalid parameters. Provide valid parameters to initialize the Contentstack javascript-SDK Stack.', + STACK_OBSOLETE_FUNCTION: "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!", + + // Entry Messages + ENTRY_UID_REQUIRED: "Entry UID required. Provide an entry UID. e.g. .Entry('entry_uid')", + ENTRY_INCLUDE_OWNER_DEPRECATED: 'The includeOwner function is deprecated. This functionality is no longer supported. Please remove this method from your code.', + ENTRY_INVALID_ARGUMENT: 'Invalid argument. Argument should be a String or an Array.', + ENTRY_LANGUAGE_INVALID: 'Invalid language code. Argument should be a String.', + ENTRY_ADD_QUERY_INVALID: 'Invalid query parameters. First argument should be a String.', + ENTRY_ADD_PARAM_INVALID: 'Invalid parameters. Both key and value should be strings.', + + // Query/Transform Messages (only/except) + TRANSFORM_INVALID_SINGLE_PARAM: 'Invalid parameters. Expected a string or an array of field names.', + TRANSFORM_INVALID_DOUBLE_PARAM: 'Invalid parameters. Expected first parameter as a string (reference field UID) and second parameter as a string or an array of field names.', + TRANSFORM_INVALID_PARAM_COUNT: 'Invalid parameters. Provide either one parameter (field name or array) or two parameters (reference field UID and field name or array).', + + // Request/Error Messages + REQUEST_ERROR_OCCURRED: (error) => `An error occurred: ${error}`, +}; + +export default MESSAGES; + diff --git a/src/core/modules/entry.js b/src/core/modules/entry.js index 33b60603..dfde537a 100755 --- a/src/core/modules/entry.js +++ b/src/core/modules/entry.js @@ -1,4 +1,5 @@ import * as Utils from '../lib/utils'; +import MESSAGES from '../messages'; /** * @class @@ -83,7 +84,7 @@ export default class Entry { this.queryCachePolicy = policy; } } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide the valid policy'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.CACHE_POLICY_INVALID); } return this; } @@ -135,7 +136,7 @@ export default class Entry { } return this; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Argument should be a String or an Array.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_INVALID_ARGUMENT); } } @@ -160,7 +161,7 @@ export default class Entry { this._query.locale = language_code; return this; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Argument should be a String.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_LANGUAGE_INVALID); } } @@ -179,7 +180,7 @@ export default class Entry { this._query[key] = value; return this; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'First argument should be a String.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_ADD_QUERY_INVALID); } } @@ -294,7 +295,7 @@ export default class Entry { * @instance */ includeOwner () { - console.warn('The includeOwner function is deprecated. This functionality is no longer supported. Please remove this method from your code.'); + console.warn(MESSAGES.ENTRY_INCLUDE_OWNER_DEPRECATED); this._query.include_owner = true; return this; } @@ -338,7 +339,7 @@ export default class Entry { this._query[key] = value; return this; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid parameters.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_ADD_PARAM_INVALID); } } @@ -391,7 +392,7 @@ export default class Entry { const options = Utils.mergeDeep(this.fetchOptions, fetchOptions); return Utils.sendRequest(Utils.mergeDeep({}, this), options); } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide an entry uid. e.g. .Entry('asset_uid')"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_UID_REQUIRED); } } } diff --git a/src/core/stack.js b/src/core/stack.js index 7f650871..b4e39821 100755 --- a/src/core/stack.js +++ b/src/core/stack.js @@ -6,6 +6,7 @@ import Query from './modules/query'; import Taxonomy from './modules/taxonomy'; import Request from './lib/request'; import CacheProvider from './cache-provider/index'; +import MESSAGES from './messages'; const errorRetry = [408, 429]; /** @@ -118,10 +119,10 @@ export default class Stack { this.environment = stack_arguments[0].environment; return this; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid object parameters. The specified API Key, Delivery Token, or Environment Name is invalid.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_OBJECT); } case 3: - if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION); if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') { this.headers = { api_key: stack_arguments[0], @@ -130,10 +131,10 @@ export default class Stack { this.environment = stack_arguments[2]; return this; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING); } case 4: - if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION); if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') { this.headers = { api_key: stack_arguments[0], @@ -141,7 +142,7 @@ export default class Stack { }; this.environment = stack_arguments[2]; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING); } if (stack_arguments[3]) { if (typeof stack_arguments[3] === 'string' && stack_arguments[3] !== undefined && stack_arguments[3] !== 'us') { @@ -152,7 +153,7 @@ export default class Stack { } return this; case 5: - if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION); if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') { this.headers = { api_key: stack_arguments[0], @@ -160,7 +161,7 @@ export default class Stack { }; this.environment = stack_arguments[2]; } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING); } if (stack_arguments[3]) { @@ -175,7 +176,7 @@ export default class Stack { } return this; default: - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack.'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_GENERIC); } } @@ -240,7 +241,7 @@ export default class Stack { this.queryCachePolicy = policy; } } else { - if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide the valid policy'); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.CACHE_POLICY_INVALID); } return this; } @@ -426,7 +427,7 @@ export default class Stack { * .then(function(result) { * // 'result' is a single contentType information. * }).catch((error) => { - * console.error('An error occurred:', error.message || error) + * console.error(MESSAGES.REQUEST_ERROR_OCCURRED(error.message || error)) * }); * @returns {promise} * @instance From e00833fb0477578d99c411e42cd80e68869649de Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Mon, 24 Nov 2025 12:37:26 +0530 Subject: [PATCH 3/4] workflow fix --- .talismanrc | 2 ++ src/core/cache-provider/index.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.talismanrc b/.talismanrc index 63b1f3e9..5f386be1 100644 --- a/.talismanrc +++ b/.talismanrc @@ -35,4 +35,6 @@ fileignoreconfig: checksum: a467e56edcb43858512c47bd82c76dbf8799d57837f03c247e2cebe27ca5eaa8 - filename: src/core/lib/utils.js checksum: 7ae53c3be5cdcd1468d66577c9450adc53e9c6aaeaeabc4275e87a47aa709850 + - filename: src/core/messages.js + checksum: 7da74b6911bb30b238459646c84e5094393a0067757de45c2361c49dcfa28719 version: "" diff --git a/src/core/cache-provider/index.js b/src/core/cache-provider/index.js index 654f33d2..577d6a65 100755 --- a/src/core/cache-provider/index.js +++ b/src/core/cache-provider/index.js @@ -1,5 +1,5 @@ import localstorage from './localstorage'; -import MESSAGES from './messages'; +import MESSAGES from '../messages'; const CacheProvider = {}; From c63622077d1d4946925726e29e4cffae588bd430 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Mon, 24 Nov 2025 12:43:38 +0530 Subject: [PATCH 4/4] workflow fix 2 --- src/core/messages.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/messages.js b/src/core/messages.js index 6196d4dc..d015bb72 100644 --- a/src/core/messages.js +++ b/src/core/messages.js @@ -28,8 +28,7 @@ const MESSAGES = { TRANSFORM_INVALID_PARAM_COUNT: 'Invalid parameters. Provide either one parameter (field name or array) or two parameters (reference field UID and field name or array).', // Request/Error Messages - REQUEST_ERROR_OCCURRED: (error) => `An error occurred: ${error}`, + REQUEST_ERROR_OCCURRED: (error) => `An error occurred: ${error}` }; export default MESSAGES; -