Skip to content

Commit 40749b8

Browse files
Merge pull request #70 from contentstack/feat/DX-3457-improve-error-msgs
updated error messages
2 parents 784b521 + d863a78 commit 40749b8

File tree

4 files changed

+139
-31
lines changed

4 files changed

+139
-31
lines changed

src/messages.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Centralized error messages and warnings for the DataSync MongoDB SDK
3+
* This file contains all user-facing messages for consistency and maintainability
4+
*/
5+
6+
export const ErrorMessages = {
7+
// Configuration errors
8+
INVALID_MONGODB_URI: (uri: any) => `MongoDB connection URL: ${uri} must be of type string`,
9+
INVALID_DBNAME: 'Content store dbName should be of type string and not empty',
10+
11+
// Sorting errors
12+
INVALID_ASCENDING_PARAMS: 'Invalid parameters for .ascending(). Expected a valid string field name',
13+
INVALID_DESCENDING_PARAMS: 'Invalid parameters for .descending(). Expected a valid string field name',
14+
15+
// Language errors
16+
INVALID_LANGUAGE_PARAMS: 'Invalid parameters for .language(). Expected a valid language code string',
17+
18+
// Logical operator errors
19+
INVALID_AND_PARAMS: 'Invalid parameters for .and(). Expected an array of query objects',
20+
INVALID_OR_PARAMS: 'Invalid parameters for .or(). Expected an array of query objects',
21+
22+
// Comparison operator errors
23+
INVALID_LESSTHAN_PARAMS: 'Invalid key or value parameters for .lessThan(). Expected a string key and a value',
24+
INVALID_LESSTHAN_OR_EQUAL_PARAMS: 'Invalid key or value parameters for .lessThanOrEqualTo(). Expected a string key and a value',
25+
INVALID_GREATERTHAN_PARAMS: 'Invalid key or value parameters for .greaterThan(). Expected a string key and a value',
26+
INVALID_GREATERTHAN_OR_EQUAL_PARAMS: 'Invalid key or value parameters for .greaterThanOrEqualTo(). Expected a string key and a value',
27+
INVALID_NOTEQUAL_PARAMS: 'Invalid key or value parameters for .notEqualTo(). Expected a string key and a value',
28+
INVALID_CONTAINED_IN_PARAMS: 'Invalid key or value parameters for .containedIn(). Expected a string key and an array value',
29+
INVALID_NOT_CONTAINED_IN_PARAMS: 'Invalid key or value parameters for .notContainedIn(). Expected a string key and an array value',
30+
INVALID_EXISTS_PARAMS: 'Invalid key parameter for .exists(). Expected a valid string field name',
31+
INVALID_NOT_EXISTS_PARAMS: 'Invalid key parameter for .notExists(). Expected a valid string field name',
32+
33+
// Content type errors
34+
MISSING_CONTENT_TYPE_UID: 'Content type UID is required. Please provide a valid content type UID',
35+
MISSING_CONTENT_TYPE_FOR_ENTRY: 'Please call .contentType() before .entry()',
36+
MISSING_CONTENT_TYPE_FOR_ENTRIES: 'Please call .contentType() before .entries()',
37+
38+
// Pagination errors
39+
INVALID_LIMIT_VALUE: 'Invalid value for .limit(). Expected a positive numeric value',
40+
INVALID_SKIP_VALUE: 'Invalid value for .skip(). Expected a non-negative numeric value',
41+
42+
// Projection errors
43+
INVALID_ONLY_PARAMS: 'Invalid field values for .only(). Expected a non-empty array of field names',
44+
INVALID_EXCEPT_PARAMS: 'Invalid field values for .except(). Expected a non-empty array of field names',
45+
46+
// Query errors
47+
INVALID_REGEX_PARAMS: 'Invalid field or pattern parameters for .regex(). Expected string values for both field and pattern',
48+
INVALID_TAGS_PARAMS: 'Invalid field values for .tags(). Expected an array of tag values',
49+
INVALID_WHERE_PARAMS: 'Invalid expression for .where(). Expected a valid expression or function',
50+
INVALID_QUERY_REFERENCES_PARAMS: 'Invalid query object for .queryReferences(). Expected a valid query object',
51+
INVALID_INCLUDE_PARAMS: 'Invalid reference field path for .include(). Expected a valid string or array of strings',
52+
53+
// Query validation errors
54+
INVALID_QUERY: 'Invalid query provided. Please ensure your query is properly formatted',
55+
INVALID_QUERIES: 'Invalid queries provided. Please ensure all queries are properly formatted',
56+
} as const
57+
58+
export const WarningMessages = {
59+
// Performance warnings
60+
SLOW_INCLUDE_REFERENCES: '.includeReferences(...) is a relatively slow query. Consider limiting the depth or using .include() for specific references',
61+
} as const
62+

src/stack.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import {
2323
validateConfig,
2424
validateURI,
2525
} from './util'
26+
import {
27+
ErrorMessages,
28+
WarningMessages,
29+
} from './messages'
2630

2731
interface IShelf {
2832
path: string,
@@ -102,7 +106,7 @@ export class Stack {
102106
*/
103107
public ascending(field) {
104108
if (typeof this.q.content_type_uid !== 'string' || typeof field !== 'string' || field.length === 0) {
105-
throw new Error('Kindly provide valid parameters for .ascending!')
109+
throw new Error(ErrorMessages.INVALID_ASCENDING_PARAMS)
106110
} else if (this.internal.sort && typeof this.internal.sort === 'object') {
107111
this.internal.sort[field] = 1
108112
} else {
@@ -140,7 +144,7 @@ export class Stack {
140144
*/
141145
public descending(field) {
142146
if (typeof this.q.content_type_uid !== 'string' || typeof field !== 'string' || field.length === 0) {
143-
throw new Error('Kindly provide valid parameters for .descending()!')
147+
throw new Error(ErrorMessages.INVALID_DESCENDING_PARAMS)
144148
} else if (this.internal.sort && typeof this.internal.sort === 'object') {
145149
this.internal.sort[field] = -1
146150
} else {
@@ -220,7 +224,7 @@ export class Stack {
220224
*/
221225
public language(code) {
222226
if (typeof code !== 'string' || code.length === 0) {
223-
throw new Error('Kindly pass valid parameters for .language()!')
227+
throw new Error(ErrorMessages.INVALID_LANGUAGE_PARAMS)
224228
}
225229
this.q.locale = code
226230

@@ -257,7 +261,7 @@ export class Stack {
257261
*/
258262
public and(queries) {
259263
if (typeof queries !== 'object' || !Array.isArray(queries)) {
260-
throw new Error('Kindly provide valid parameters for .and()!')
264+
throw new Error(ErrorMessages.INVALID_AND_PARAMS)
261265
} else if (this.q.query && typeof this.q.query === 'object') {
262266
this.q.query = merge(this.q.query, {
263267
$and: queries,
@@ -301,7 +305,7 @@ export class Stack {
301305
*/
302306
public or(queries) {
303307
if (typeof queries !== 'object' || !Array.isArray(queries)) {
304-
throw new Error('Kindly provide valid parameters for .or()!')
308+
throw new Error(ErrorMessages.INVALID_OR_PARAMS)
305309
} else if (this.q.query && typeof this.q.query === 'object') {
306310
this.q.query = merge(this.q.query, {
307311
$or: queries,
@@ -343,7 +347,7 @@ export class Stack {
343347
*/
344348
public lessThan(key, value) {
345349
if (typeof key !== 'string' || typeof value === 'undefined') {
346-
throw new Error('Kindly pass valid key and value parameters for \'.lessThan()\'')
350+
throw new Error(ErrorMessages.INVALID_LESSTHAN_PARAMS)
347351
} else if (this.q.query && typeof this.q.query === 'object') {
348352
this.q.query[key] = {
349353
$lt: value,
@@ -387,7 +391,7 @@ export class Stack {
387391
*/
388392
public lessThanOrEqualTo(key, value) {
389393
if (typeof key !== 'string' || typeof value === 'undefined') {
390-
throw new Error('Kindly pass valid key and value parameters for \'.lessThanOrEqualTo()\'')
394+
throw new Error(ErrorMessages.INVALID_LESSTHAN_OR_EQUAL_PARAMS)
391395
} else if (this.q.query && typeof this.q.query === 'object') {
392396
this.q.query[key] = {
393397
$lte: value,
@@ -431,7 +435,7 @@ export class Stack {
431435
*/
432436
public greaterThan(key, value) {
433437
if (typeof key !== 'string' || typeof value === 'undefined') {
434-
throw new Error('Kindly pass valid key and value parameters for \'.greaterThan()\'')
438+
throw new Error(ErrorMessages.INVALID_GREATERTHAN_PARAMS)
435439
} else if (this.q.query && typeof this.q.query === 'object') {
436440
this.q.query[key] = {
437441
$gt: value,
@@ -475,7 +479,7 @@ export class Stack {
475479
*/
476480
public greaterThanOrEqualTo(key, value) {
477481
if (typeof key !== 'string' || typeof value === 'undefined') {
478-
throw new Error('Kindly pass valid key and value parameters for \'.greaterThanOrEqualTo()\'')
482+
throw new Error(ErrorMessages.INVALID_GREATERTHAN_OR_EQUAL_PARAMS)
479483
} else if (this.q.query && typeof this.q.query === 'object') {
480484
this.q.query[key] = {
481485
$gte: value,
@@ -524,7 +528,7 @@ export class Stack {
524528
*/
525529
public notEqualTo(key, value) {
526530
if (typeof key !== 'string' || typeof value === 'undefined') {
527-
throw new Error('Kindly pass valid key and value parameters for \'.notEqualTo()\'')
531+
throw new Error(ErrorMessages.INVALID_NOTEQUAL_PARAMS)
528532
} else if (this.q.query && typeof this.q.query === 'object') {
529533
this.q.query[key] = {
530534
$ne: value,
@@ -574,7 +578,7 @@ export class Stack {
574578
*/
575579
public containedIn(key, value) {
576580
if (typeof key !== 'string' || typeof value !== 'object' || !(value instanceof Array)) {
577-
throw new Error('Kindly pass valid key and value parameters for \'.containedIn()\'')
581+
throw new Error(ErrorMessages.INVALID_CONTAINED_IN_PARAMS)
578582
} else if (this.q.query && typeof this.q.query === 'object') {
579583
this.q.query[key] = {
580584
$in: value,
@@ -624,7 +628,7 @@ export class Stack {
624628
*/
625629
public notContainedIn(key, value) {
626630
if (typeof key !== 'string' || typeof value !== 'object' || !(value instanceof Array)) {
627-
throw new Error('Kindly pass valid key and value parameters for \'.notContainedIn()\'')
631+
throw new Error(ErrorMessages.INVALID_NOT_CONTAINED_IN_PARAMS)
628632
} else if (this.q.query && typeof this.q.query === 'object') {
629633
this.q.query[key] = {
630634
$nin: value,
@@ -674,7 +678,7 @@ export class Stack {
674678
*/
675679
public exists(key) {
676680
if (typeof key !== 'string') {
677-
throw new Error('Kindly pass valid key for \'.exists()\'')
681+
throw new Error(ErrorMessages.INVALID_EXISTS_PARAMS)
678682
} else if (this.q.query && typeof this.q.query === 'object') {
679683
this.q.query[key] = {
680684
$exists: true,
@@ -723,7 +727,7 @@ export class Stack {
723727
*/
724728
public notExists(key) {
725729
if (typeof key !== 'string') {
726-
throw new Error('Kindly pass valid key for \'.notExists()\'')
730+
throw new Error(ErrorMessages.INVALID_NOT_EXISTS_PARAMS)
727731
} else if (this.q.query && typeof this.q.query === 'object') {
728732
this.q.query[key] = {
729733
$exists: false,
@@ -766,7 +770,7 @@ export class Stack {
766770

767771
return stack
768772
}
769-
throw new Error('Kindly pass the content type\'s uid')
773+
throw new Error(ErrorMessages.MISSING_CONTENT_TYPE_UID)
770774
}
771775

772776
/**
@@ -793,7 +797,7 @@ export class Stack {
793797
*/
794798
public entry(uid ? ) {
795799
if (!(this.q.content_type_uid)) {
796-
throw new Error('Kindly call \'contentType()\' before \'entry()\'!')
800+
throw new Error(ErrorMessages.MISSING_CONTENT_TYPE_FOR_ENTRY)
797801
}
798802
if (uid && typeof uid === 'string') {
799803
this.q.query = this.q.query || {}
@@ -830,7 +834,7 @@ export class Stack {
830834

831835
return this
832836
}
833-
throw new Error('Kindly call \'contentType()\' before \'entries()\'!')
837+
throw new Error(ErrorMessages.MISSING_CONTENT_TYPE_FOR_ENTRIES)
834838
}
835839

836840
/**
@@ -1013,7 +1017,7 @@ export class Stack {
10131017

10141018
return this
10151019
}
1016-
throw new Error('Kindly provide a valid \'numeric\' value for \'limit()\'')
1020+
throw new Error(ErrorMessages.INVALID_LIMIT_VALUE)
10171021
}
10181022

10191023
/**
@@ -1046,7 +1050,7 @@ export class Stack {
10461050

10471051
return this
10481052
}
1049-
throw new Error('Kindly provide a valid \'numeric\' value for \'skip()\'')
1053+
throw new Error(ErrorMessages.INVALID_SKIP_VALUE)
10501054
}
10511055

10521056
/**
@@ -1106,7 +1110,7 @@ export class Stack {
11061110
*/
11071111
public only(fields) {
11081112
if (!fields || typeof fields !== 'object' || !(fields instanceof Array) || fields.length === 0) {
1109-
throw new Error('Kindly provide valid \'field\' values for \'only()\'')
1113+
throw new Error(ErrorMessages.INVALID_ONLY_PARAMS)
11101114
}
11111115
this.internal.only = this.internal.only || {}
11121116
this.internal.only._id = 0
@@ -1144,7 +1148,7 @@ export class Stack {
11441148
*/
11451149
public except(fields) {
11461150
if (!fields || typeof fields !== 'object' || !(fields instanceof Array) || fields.length === 0) {
1147-
throw new Error('Kindly provide valid \'field\' values for \'except()\'')
1151+
throw new Error(ErrorMessages.INVALID_EXCEPT_PARAMS)
11481152
}
11491153
this.internal.except = this.internal.except || {}
11501154
fields.forEach((field) => {
@@ -1184,7 +1188,7 @@ export class Stack {
11841188
*/
11851189
public regex(field, pattern, options = 'i') {
11861190
if (!(field) || !(pattern) || typeof field !== 'string' || typeof pattern !== 'string') {
1187-
throw new Error('Kindly provide a valid field and pattern value for \'.regex()\'')
1191+
throw new Error(ErrorMessages.INVALID_REGEX_PARAMS)
11881192
} else if (this.q.query && typeof this.q.query === 'object') {
11891193
this.q.query = merge(this.q.query, {
11901194
[field]: {
@@ -1227,7 +1231,7 @@ export class Stack {
12271231
*/
12281232
public tags(values) {
12291233
if (!values || typeof values !== 'object' || !(values instanceof Array)) {
1230-
throw new Error('Kindly provide valid \'field\' values for \'tags()\'')
1234+
throw new Error(ErrorMessages.INVALID_TAGS_PARAMS)
12311235
}
12321236
// filter non-string keys
12331237
remove(values, (value) => {
@@ -1282,7 +1286,7 @@ export class Stack {
12821286
*/
12831287
public where(expr) {
12841288
if (!(expr)) {
1285-
throw new Error('Kindly provide a valid field and expr/fn value for \'.where()\'')
1289+
throw new Error(ErrorMessages.INVALID_WHERE_PARAMS)
12861290
} else if (this.q.query && typeof this.q.query === 'object') {
12871291
if (typeof expr === 'function') {
12881292
expr = expr.toString()
@@ -1440,7 +1444,7 @@ export class Stack {
14401444
return this
14411445
}
14421446

1443-
throw new Error('Kindly pass a query object for \'.queryReferences()\'')
1447+
throw new Error(ErrorMessages.INVALID_QUERY_REFERENCES_PARAMS)
14441448
}
14451449

14461450
/**
@@ -1477,7 +1481,7 @@ export class Stack {
14771481
* @returns {Stack} Returns 'this' instance (of Stack)
14781482
*/
14791483
public includeReferences(depth?: number) {
1480-
console.warn('.includeReferences() is a relatively slow query..!')
1484+
console.warn(WarningMessages.SLOW_INCLUDE_REFERENCES)
14811485
if (typeof depth === 'number') {
14821486
this.q.referenceDepth = depth
14831487
}
@@ -1501,7 +1505,7 @@ export class Stack {
15011505
*/
15021506
public include(fields) {
15031507
if (fields.length === 0) {
1504-
throw new Error('Kindly pass a valid reference field path to \'.include()\' ')
1508+
throw new Error(ErrorMessages.INVALID_INCLUDE_PARAMS)
15051509
} else if (typeof fields === 'string') {
15061510
this.internal.includeSpecificReferences = [fields]
15071511
} else {
@@ -2099,7 +2103,7 @@ export class Stack {
20992103

21002104
private async getReferencePath(query, locale, currentInclude) {
21012105
if (!this.sanityQueryAny(query)) {
2102-
throw new Error('Invalid query provided');
2106+
throw new Error(ErrorMessages.INVALID_QUERY)
21032107
}
21042108
const querySanitize = this.sanitizeQueryBucket(query)
21052109
const schemas = await this.db.collection(getCollectionName({
@@ -2195,7 +2199,7 @@ export class Stack {
21952199
private async fetchEntries(query: IQuery, locale: string, paths: string[], include: string[], includeAll:
21962200
boolean = false) {
21972201
if (!this.sanitizeIQuery(query)) {
2198-
throw new Error('Invalid queries provided');
2202+
throw new Error(ErrorMessages.INVALID_QUERIES)
21992203
}
22002204
const sanitizeQuery = this.sanitizeQueryBucket(query)
22012205
const result = await this.db.collection(getCollectionName({

src/util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { uniq } from 'lodash'
8+
import { ErrorMessages } from './messages'
89

910
/**
1011
* @private
@@ -16,7 +17,7 @@ import { uniq } from 'lodash'
1617
*/
1718
export const validateURI = (uri) => {
1819
if (typeof uri !== 'string' || uri.length === 0) {
19-
throw new Error(`Mongodb connection url: ${uri} must be of type string`)
20+
throw new Error(ErrorMessages.INVALID_MONGODB_URI(uri))
2021
}
2122

2223
return uri
@@ -59,7 +60,7 @@ const getParents = (child, mapping) => {
5960

6061
const validateContentStore = (contentStore) => {
6162
if (typeof contentStore.dbName !== 'string' || contentStore.dbName.length === 0) {
62-
throw new Error('Contentstore dbName should be of type string and not empty!')
63+
throw new Error(ErrorMessages.INVALID_DBNAME)
6364
}
6465

6566
if (typeof contentStore.collectionName === 'string') {

0 commit comments

Comments
 (0)