Skip to content

Commit 9a59678

Browse files
Merge pull request #45 from contentstack/feat/DX-3457-improve-error-msgs
updated error messages
2 parents 8b0a115 + 8534863 commit 9a59678

File tree

8 files changed

+150
-44
lines changed

8 files changed

+150
-44
lines changed

src/connection.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Debug from 'debug'
88
import { isEmpty, isPlainObject, merge } from 'lodash'
99
import { Db, MongoClient } from 'mongodb'
1010
import { validateMongodbConfig } from './util/validations'
11+
import { MESSAGES } from './util/messages'
1112

1213
const debug = Debug('connection')
1314
interface IMongo {
@@ -42,10 +43,10 @@ export const connect = (config) => {
4243
const dbName = mongoConfig.dbName
4344
const options = mongoConfig.options
4445

45-
debug('connection url', connectionUri)
46-
debug('db name', dbName)
47-
debug('collection names', mongoConfig.collection)
48-
debug('db options', JSON.stringify(options))
46+
debug(MESSAGES.CONNECTION.URL, connectionUri)
47+
debug(MESSAGES.CONNECTION.DB_NAME, dbName)
48+
debug(MESSAGES.CONNECTION.COLLECTION_NAMES, mongoConfig.collection)
49+
debug(MESSAGES.CONNECTION.DB_OPTIONS, JSON.stringify(options))
4950

5051
if (mongoConfig.indexes && isPlainObject(mongoConfig.indexes) && !(isEmpty(mongoConfig.indexes))) {
5152
indexes = merge(indexes, mongoConfig.indexes)
@@ -56,7 +57,7 @@ export const connect = (config) => {
5657
return client.connect().then(() => {
5758
instance.db = client.db(dbName)
5859
instance.client = client
59-
console.info(`Mongodb connection to ${connectionUri} established successfully!`)
60+
console.info(MESSAGES.CONNECTION.SUCCESS(connectionUri))
6061

6162
return resolve(instance)
6263
})

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { config as internalConfig } from './config'
1010
import { connect } from './connection'
1111
import { Mongodb } from './mongodb'
1212
import { sanitizeConfig } from './util/index'
13+
import { MESSAGES } from './util/messages'
1314

1415
import {
1516
validateAssetConnectorInstance,
@@ -103,7 +104,7 @@ export const start = (connector: IAssetConnector, config?: IConfig) => {
103104
return connect(appConfig)
104105
.then((mongo) => {
105106
mongoClient = new Mongodb(mongo, assetConnectorInstance, appConfig.contentStore, appConfig)
106-
debug('Mongo connector instance created successfully!')
107+
debug(MESSAGES.CONNECTOR.INSTANCE_CREATED)
107108

108109
return resolve(mongoClient)
109110
})

src/mongodb.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getCollectionName,
1414
getLocalesFromCollections,
1515
} from "./util/index";
16+
import { MESSAGES } from "./util/messages";
1617
import {
1718
validateAssetDelete,
1819
validateAssetPublish,
@@ -93,7 +94,7 @@ export class Mongodb {
9394
* @returns {Promise} Returns a promise
9495
*/
9596
public publishAsset(data) {
96-
debug(`Asset publish called ${JSON.stringify(data)}`);
97+
debug(MESSAGES.ASSET.PUBLISH_INITIATED(data));
9798

9899
return new Promise(async (resolve, reject) => {
99100
try {
@@ -127,7 +128,7 @@ export class Mongodb {
127128
upsert: true,
128129
}
129130
);
130-
debug(`Asset publish result ${JSON.stringify(result)}`);
131+
debug(MESSAGES.ASSET.PUBLISH_RESULT(result));
131132

132133
return resolve(data);
133134
} catch (error) {
@@ -142,7 +143,7 @@ export class Mongodb {
142143
* @returns {Promise} Returns a promise
143144
*/
144145
public updateContentType(contentType) {
145-
debug(`Entry publish called ${JSON.stringify(contentType)}`);
146+
debug(MESSAGES.ENTRY.PUBLISH_INITIATED(contentType));
146147

147148
return new Promise(async (resolve, reject) => {
148149
try {
@@ -164,11 +165,7 @@ export class Mongodb {
164165
upsert: true,
165166
}
166167
);
167-
debug(
168-
`Content type update result ${JSON.stringify(
169-
contentTypeUpdateResult
170-
)}`
171-
);
168+
debug(MESSAGES.CONTENT_TYPE.UPDATE_RESULT(contentTypeUpdateResult));
172169

173170
return resolve(contentType);
174171
} catch (error) {
@@ -183,7 +180,7 @@ export class Mongodb {
183180
* @returns {Promise} Returns a promise
184181
*/
185182
public publishEntry(entry) {
186-
debug(`Entry publish called ${JSON.stringify(entry)}`);
183+
debug(MESSAGES.ENTRY.PUBLISH_INITIATED(entry));
187184

188185
return new Promise((resolve, reject) => {
189186
try {
@@ -220,7 +217,7 @@ export class Mongodb {
220217
}
221218
)
222219
.then((entryPublishResult) => {
223-
debug(`Entry publish result ${JSON.stringify(entryPublishResult)}`);
220+
debug(MESSAGES.ENTRY.PUBLISH_RESULT(entryPublishResult));
224221

225222
return resolve(entry);
226223
})
@@ -270,7 +267,7 @@ export class Mongodb {
270267
* @returns {Promise} Returns a promise
271268
*/
272269
private unpublishEntry(entry) {
273-
debug(`Delete entry called ${JSON.stringify(entry)}`);
270+
debug(MESSAGES.ENTRY.DELETE_INITIATED(entry));
274271

275272
return new Promise((resolve, reject) => {
276273
try {
@@ -284,7 +281,7 @@ export class Mongodb {
284281
uid: entry.uid,
285282
})
286283
.then((result) => {
287-
debug(`Delete entry result ${JSON.stringify(result)}`);
284+
debug(MESSAGES.ENTRY.DELETE_RESULT(result));
288285

289286
return resolve(entry);
290287
})
@@ -301,7 +298,7 @@ export class Mongodb {
301298
* @returns {Promise} Returns a promise
302299
*/
303300
private deleteEntry(entry) {
304-
debug(`Delete entry called ${JSON.stringify(entry)}`);
301+
debug(MESSAGES.ENTRY.DELETE_INITIATED(entry));
305302

306303
return new Promise((resolve, reject) => {
307304
try {
@@ -314,7 +311,7 @@ export class Mongodb {
314311
uid: entry.uid,
315312
})
316313
.then((result) => {
317-
debug(`Delete entry result ${JSON.stringify(result)}`);
314+
debug(MESSAGES.ENTRY.DELETE_RESULT(result));
318315

319316
return resolve(entry);
320317
})
@@ -331,7 +328,7 @@ export class Mongodb {
331328
* @returns {Promise} Returns a promise
332329
*/
333330
private unpublishAsset(asset) {
334-
debug(`Unpublish asset called ${JSON.stringify(asset)}`);
331+
debug(MESSAGES.ASSET.UNPUBLISH_INITIATED(asset));
335332

336333
return new Promise((resolve, reject) => {
337334
try {
@@ -348,7 +345,7 @@ export class Mongodb {
348345
uid: asset.uid,
349346
})
350347
.then((result) => {
351-
debug(`Asset unpublish status: ${JSON.stringify(result)}`);
348+
debug(MESSAGES.ASSET.UNPUBLISH_RESULT(result));
352349
if (!result?.value) {
353350
return resolve(asset);
354351
}
@@ -367,19 +364,13 @@ export class Mongodb {
367364
.toArray()
368365
.then((assets) => {
369366
if (assets.length === 0) {
370-
debug(
371-
`Only published object of ${JSON.stringify(
372-
asset
373-
)} was present`
374-
);
367+
debug(MESSAGES.ASSET.ONLY_PUBLISHED_PRESENT(asset));
375368

376369
return this.assetStore
377370
.unpublish(result.value)
378371
.then(() => resolve(asset));
379372
}
380-
debug(
381-
"Asset existed in pubilshed and RTE/Markdown form. Removed published asset object."
382-
);
373+
debug(MESSAGES.ASSET.EXISTED_IN_MULTIPLE_FORMS);
383374

384375
return resolve(asset);
385376
});
@@ -397,7 +388,7 @@ export class Mongodb {
397388
* @returns {Promise} Returns a promise
398389
*/
399390
private deleteAsset(asset) {
400-
debug(`Delete asset called ${JSON.stringify(asset)}`);
391+
debug(MESSAGES.ASSET.DELETE_INITIATED(asset));
401392

402393
return new Promise((resolve, reject) => {
403394
try {
@@ -413,7 +404,7 @@ export class Mongodb {
413404
.toArray()
414405
.then((result) => {
415406
if (result.length === 0) {
416-
debug("Asset did not exist!");
407+
debug(MESSAGES.ASSET.DOES_NOT_EXIST);
417408

418409
return resolve(asset);
419410
}
@@ -445,7 +436,7 @@ export class Mongodb {
445436
* @returns {Promise} Returns a promise
446437
*/
447438
private deleteContentType(contentType) {
448-
debug(`Delete content type called ${JSON.stringify(contentType)}`);
439+
debug(MESSAGES.CONTENT_TYPE.DELETE_INITIATED(contentType));
449440

450441
return new Promise(async (resolve, reject) => {
451442
try {
@@ -485,9 +476,7 @@ export class Mongodb {
485476
_content_type_uid: uid,
486477
})
487478
.then((entriesDeleteResult) => {
488-
debug(
489-
`Delete entries result ${JSON.stringify(entriesDeleteResult)}`
490-
);
479+
debug(MESSAGES.CONTENT_TYPE.DELETE_ENTRIES_RESULT(entriesDeleteResult));
491480

492481
return this.db
493482
.collection(collection.name)
@@ -496,11 +485,7 @@ export class Mongodb {
496485
uid,
497486
})
498487
.then((contentTypeDeleteResult) => {
499-
debug(
500-
`Content type delete result ${JSON.stringify(
501-
contentTypeDeleteResult
502-
)}`
503-
);
488+
debug(MESSAGES.CONTENT_TYPE.DELETE_RESULT(contentTypeDeleteResult));
504489

505490
return resolve(0);
506491
});

src/process.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
import { getMongoClient } from './'
13+
import { MESSAGES } from './util/messages'
1314

1415
/**
1516
* @description Handles process exit. Stops the current application and manages a graceful shutdown
@@ -18,7 +19,7 @@ import { getMongoClient } from './'
1819
const handleExit = (signal) => {
1920
const killDuration = (process.env.KILLDURATION) ? calculateKillDuration() : 15000
2021
// tslint:disable-next-line: no-console
21-
console.info(`Received ${signal}. This will shut down the process in ${killDuration}ms..`)
22+
console.info(MESSAGES.PROCESS.SHUTDOWN(signal, killDuration))
2223
setInterval(abort, killDuration)
2324
}
2425

@@ -30,7 +31,7 @@ const handleExit = (signal) => {
3031
* @param {Object} error - Unhandled error object
3132
*/
3233
const unhandledErrors = (error) => {
33-
console.error('Unhandled exception caught. Locking down process for 10s to recover..')
34+
console.error(MESSAGES.PROCESS.UNHANDLED_ERROR)
3435
console.error(error)
3536
}
3637

src/util/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import { getConfig } from '../index'
99

10+
export { MESSAGES } from './messages'
11+
1012
interface IContentStore {
1113
collectionName?: string,
1214
collection?: {

src/util/messages.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*!
2+
* Contentstack Mongodb Content Connector
3+
* Copyright (c) 2019 Contentstack LLC
4+
* MIT Licensed
5+
*/
6+
7+
/**
8+
* Centralized messages for logging and error handling
9+
* This file contains all log messages, error messages, and console outputs
10+
* used throughout the application for consistency and maintainability.
11+
*/
12+
13+
export const MESSAGES = {
14+
// Connection messages
15+
CONNECTION: {
16+
URL: 'Connection URL',
17+
DB_NAME: 'Database name',
18+
COLLECTION_NAMES: 'Collection names',
19+
DB_OPTIONS: 'Database options',
20+
SUCCESS: (connectionUri: string) => `MongoDB connection to ${connectionUri} established successfully.`,
21+
},
22+
23+
// MongoDB connector messages
24+
CONNECTOR: {
25+
INSTANCE_CREATED: 'Mongo connector instance created successfully.',
26+
},
27+
28+
// Asset operation messages
29+
ASSET: {
30+
PUBLISH_INITIATED: (data: any) => `Asset publish initiated with: ${JSON.stringify(data)}`,
31+
PUBLISH_RESULT: (result: any) => `Asset publish result: ${JSON.stringify(result)}`,
32+
UNPUBLISH_INITIATED: (asset: any) => `Asset unpublish initiated for: ${JSON.stringify(asset)}`,
33+
UNPUBLISH_RESULT: (result: any) => `Asset unpublish result: ${JSON.stringify(result)}`,
34+
DELETE_INITIATED: (asset: any) => `Asset delete initiated for: ${JSON.stringify(asset)}`,
35+
DOES_NOT_EXIST: 'Asset does not exist.',
36+
ONLY_PUBLISHED_PRESENT: (asset: any) => `Only published object of ${JSON.stringify(asset)} was present`,
37+
EXISTED_IN_MULTIPLE_FORMS: 'Asset existed in pubilshed and RTE/Markdown form. Removed published asset object.',
38+
},
39+
40+
// Entry operation messages
41+
ENTRY: {
42+
PUBLISH_INITIATED: (entry: any) => `Entry publish initiated with: ${JSON.stringify(entry)}`,
43+
PUBLISH_RESULT: (result: any) => `Entry publish result: ${JSON.stringify(result)}`,
44+
DELETE_INITIATED: (entry: any) => `Entry delete initiated for: ${JSON.stringify(entry)}`,
45+
DELETE_RESULT: (result: any) => `Entry delete result: ${JSON.stringify(result)}`,
46+
},
47+
48+
// Content type operation messages
49+
CONTENT_TYPE: {
50+
UPDATE_RESULT: (result: any) => `Content type update result ${JSON.stringify(result)}`,
51+
DELETE_INITIATED: (contentType: any) => `Content type delete initiated for: ${JSON.stringify(contentType)}`,
52+
DELETE_RESULT: (result: any) => `Content type delete result ${JSON.stringify(result)}`,
53+
DELETE_ENTRIES_RESULT: (result: any) => `Delete entries result ${JSON.stringify(result)}`,
54+
},
55+
56+
// Process management messages
57+
PROCESS: {
58+
SHUTDOWN: (signal: string, killDuration: number) => `Received ${signal}. Shutting down the process in ${killDuration} ms.`,
59+
UNHANDLED_ERROR: 'An unexpected error occurred. Locking the process for 10 seconds to recover.',
60+
},
61+
62+
// Logger messages
63+
LOGGER: {
64+
MESSAGE_MISSING: 'Message missing or could not be resolved.',
65+
},
66+
};
67+
68+
export default MESSAGES;
69+

typings/util/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* MIT Licensed
55
*
66
*/
7+
export { MESSAGES } from './messages';
78
interface IContentStore {
89
collectionName?: string;
910
collection?: {
@@ -31,4 +32,3 @@ export declare const getLocalesFromCollections: (collections: {
3132
name: string;
3233
locale: string;
3334
}[];
34-
export {};

0 commit comments

Comments
 (0)