Skip to content

Commit 438aa00

Browse files
committed
Refactor error handling in stack, cache provider, and entry modules to utilize centralized message constants for improved maintainability and clarity.
1 parent 634b88c commit 438aa00

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

src/core/cache-provider/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import localstorage from './localstorage';
2+
import MESSAGES from './messages';
3+
24
const CacheProvider = {};
35

46
CacheProvider.providers = function (provider) {
57
if (provider) {
68
return localstorage;
79
} else {
8-
console.error('Missing cache provider. Provide a valid provider and try again.');
10+
console.error(MESSAGES.CACHE_PROVIDER_MISSING);
911
}
1012
};
1113

src/core/lib/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Request from './request';
22
import Result from '../modules/result';
3+
import MESSAGES from '../messages';
34

45
/**
56
* @method addSpread
@@ -28,7 +29,7 @@ export function transform (type) {
2829
this._query[type].BASE = query;
2930
return this;
3031
} else {
31-
console.error('Invalid parameters. Expected a string or an array of field names.');
32+
console.error(MESSAGES.TRANSFORM_INVALID_SINGLE_PARAM);
3233
}
3334
break;
3435
case 2:
@@ -38,11 +39,11 @@ export function transform (type) {
3839
this._query[type][arguments[0]] = query;
3940
return this;
4041
} else {
41-
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.');
42+
console.error(MESSAGES.TRANSFORM_INVALID_DOUBLE_PARAM);
4243
}
4344
break;
4445
default:
45-
console.error('Invalid parameters. Provide either one parameter (field name or array) or two parameters (reference field UID and field name or array).');
46+
console.error(MESSAGES.TRANSFORM_INVALID_PARAM_COUNT);
4647
}
4748
};
4849
}

src/core/messages.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Centralized error and informational messages for Contentstack JavaScript SDK
3+
* @module messages
4+
*/
5+
6+
const MESSAGES = {
7+
// Cache Provider Messages
8+
CACHE_PROVIDER_MISSING: 'Missing cache provider. Provide a valid provider and try again.',
9+
CACHE_POLICY_INVALID: 'Invalid cache policy. Provide a valid policy value and try again.',
10+
11+
// Stack Initialization Messages
12+
STACK_INVALID_PARAMS_OBJECT: 'Invalid parameters. The specified API Key, Delivery Token, or Environment Name is invalid.',
13+
STACK_INVALID_PARAMS_STRING: 'Invalid string parameters. Provide valid API Key, Delivery Token, and Environment Name.',
14+
STACK_INVALID_PARAMS_GENERIC: 'Invalid parameters. Provide valid parameters to initialize the Contentstack javascript-SDK Stack.',
15+
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!",
16+
17+
// Entry Messages
18+
ENTRY_UID_REQUIRED: "Entry UID required. Provide an entry UID. e.g. .Entry('entry_uid')",
19+
ENTRY_INCLUDE_OWNER_DEPRECATED: 'The includeOwner function is deprecated. This functionality is no longer supported. Please remove this method from your code.',
20+
ENTRY_INVALID_ARGUMENT: 'Invalid argument. Argument should be a String or an Array.',
21+
ENTRY_LANGUAGE_INVALID: 'Invalid language code. Argument should be a String.',
22+
ENTRY_ADD_QUERY_INVALID: 'Invalid query parameters. First argument should be a String.',
23+
ENTRY_ADD_PARAM_INVALID: 'Invalid parameters. Both key and value should be strings.',
24+
25+
// Query/Transform Messages (only/except)
26+
TRANSFORM_INVALID_SINGLE_PARAM: 'Invalid parameters. Expected a string or an array of field names.',
27+
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.',
28+
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).',
29+
30+
// Request/Error Messages
31+
REQUEST_ERROR_OCCURRED: (error) => `An error occurred: ${error}`,
32+
};
33+
34+
export default MESSAGES;
35+

src/core/modules/entry.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Utils from '../lib/utils';
2+
import MESSAGES from '../messages';
23

34
/**
45
* @class
@@ -83,7 +84,7 @@ export default class Entry {
8384
this.queryCachePolicy = policy;
8485
}
8586
} else {
86-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide the valid policy');
87+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.CACHE_POLICY_INVALID);
8788
}
8889
return this;
8990
}
@@ -135,7 +136,7 @@ export default class Entry {
135136
}
136137
return this;
137138
} else {
138-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Argument should be a String or an Array.');
139+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_INVALID_ARGUMENT);
139140
}
140141
}
141142

@@ -160,7 +161,7 @@ export default class Entry {
160161
this._query.locale = language_code;
161162
return this;
162163
} else {
163-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Argument should be a String.');
164+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_LANGUAGE_INVALID);
164165
}
165166
}
166167

@@ -179,7 +180,7 @@ export default class Entry {
179180
this._query[key] = value;
180181
return this;
181182
} else {
182-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'First argument should be a String.');
183+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_ADD_QUERY_INVALID);
183184
}
184185
}
185186

@@ -294,7 +295,7 @@ export default class Entry {
294295
* @instance
295296
*/
296297
includeOwner () {
297-
console.warn('The includeOwner function is deprecated. This functionality is no longer supported. Please remove this method from your code.');
298+
console.warn(MESSAGES.ENTRY_INCLUDE_OWNER_DEPRECATED);
298299
this._query.include_owner = true;
299300
return this;
300301
}
@@ -338,7 +339,7 @@ export default class Entry {
338339
this._query[key] = value;
339340
return this;
340341
} else {
341-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid parameters.');
342+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_ADD_PARAM_INVALID);
342343
}
343344
}
344345

@@ -391,7 +392,7 @@ export default class Entry {
391392
const options = Utils.mergeDeep(this.fetchOptions, fetchOptions);
392393
return Utils.sendRequest(Utils.mergeDeep({}, this), options);
393394
} else {
394-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide an entry uid. e.g. .Entry('asset_uid')");
395+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_UID_REQUIRED);
395396
}
396397
}
397398
}

src/core/stack.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Query from './modules/query';
66
import Taxonomy from './modules/taxonomy';
77
import Request from './lib/request';
88
import CacheProvider from './cache-provider/index';
9+
import MESSAGES from './messages';
910
const errorRetry = [408, 429];
1011

1112
/**
@@ -118,10 +119,10 @@ export default class Stack {
118119
this.environment = stack_arguments[0].environment;
119120
return this;
120121
} else {
121-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid object parameters. The specified API Key, Delivery Token, or Environment Name is invalid.');
122+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_OBJECT);
122123
}
123124
case 3:
124-
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!");
125+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION);
125126
if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') {
126127
this.headers = {
127128
api_key: stack_arguments[0],
@@ -130,18 +131,18 @@ export default class Stack {
130131
this.environment = stack_arguments[2];
131132
return this;
132133
} else {
133-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.');
134+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING);
134135
}
135136
case 4:
136-
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!");
137+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION);
137138
if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') {
138139
this.headers = {
139140
api_key: stack_arguments[0],
140141
access_token: stack_arguments[1]
141142
};
142143
this.environment = stack_arguments[2];
143144
} else {
144-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.');
145+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING);
145146
}
146147
if (stack_arguments[3]) {
147148
if (typeof stack_arguments[3] === 'string' && stack_arguments[3] !== undefined && stack_arguments[3] !== 'us') {
@@ -152,15 +153,15 @@ export default class Stack {
152153
}
153154
return this;
154155
case 5:
155-
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!");
156+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION);
156157
if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') {
157158
this.headers = {
158159
api_key: stack_arguments[0],
159160
access_token: stack_arguments[1]
160161
};
161162
this.environment = stack_arguments[2];
162163
} else {
163-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.');
164+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING);
164165
}
165166

166167
if (stack_arguments[3]) {
@@ -175,7 +176,7 @@ export default class Stack {
175176
}
176177
return this;
177178
default:
178-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack.');
179+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_GENERIC);
179180
}
180181
}
181182

@@ -240,7 +241,7 @@ export default class Stack {
240241
this.queryCachePolicy = policy;
241242
}
242243
} else {
243-
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide the valid policy');
244+
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.CACHE_POLICY_INVALID);
244245
}
245246
return this;
246247
}
@@ -426,7 +427,7 @@ export default class Stack {
426427
* .then(function(result) {
427428
* // 'result' is a single contentType information.
428429
* }).catch((error) => {
429-
* console.error('An error occurred:', error.message || error)
430+
* console.error(MESSAGES.REQUEST_ERROR_OCCURRED(error.message || error))
430431
* });
431432
* @returns {promise}
432433
* @instance

0 commit comments

Comments
 (0)