diff --git a/.talismanrc b/.talismanrc index dfec72f..962a75f 100644 --- a/.talismanrc +++ b/.talismanrc @@ -4,4 +4,10 @@ fileignoreconfig: - filecontent - filename: package-lock.json checksum: 20cc76958aec3163d9b695e49bcdfbef7244fc473e31f8eb69b88133622f4fb4 +- filename: src/index.ts + checksum: 3563972e6fef958b021444c41ef04f5f32e83d1d51f7faea62ad1633130b6f1a +- filename: src/messages.ts + checksum: 5fc01b165e2fec0c969b954b15ab9201eaabcdce766c8b007907377f015f0043 +- filename: typings/messages.d.ts + checksum: f79568b5a95311acea1892c644554091c2b38d169a4867380693fa09e2639d95 version: "" diff --git a/package-lock.json b/package-lock.json index 4738bba..8901001 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/datasync-asset-store-filesystem", - "version": "2.3.0", + "version": "2.3.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/datasync-asset-store-filesystem", - "version": "2.3.0", + "version": "2.3.1", "license": "MIT", "dependencies": { "debug": "^4.3.4", diff --git a/package.json b/package.json index c527a92..247685e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/datasync-asset-store-filesystem", - "version": "2.3.0", + "version": "2.3.1", "description": "Fillesystem asset store for DataSync libraries. Stores Contentstack asset-files in filesystem", "main": "./dist", "types": "./typings", diff --git a/src/filesystem.ts b/src/filesystem.ts index 35565c6..3cf03e3 100644 --- a/src/filesystem.ts +++ b/src/filesystem.ts @@ -30,6 +30,7 @@ import { validateUnPublishAsset, sanitizePath } from './utils' +import { messages } from './messages' const debug = Debug('asset-store-filesystem') @@ -76,7 +77,7 @@ export class FSAssetStore { * @returns {Promise} returns the asset object, if successful. */ public download(asset) { - debug('Asset download invoked ' + JSON.stringify(asset)) + debug(messages.info.assetDownloadInitiated(asset)) return new Promise((resolve, reject) => { try { @@ -112,12 +113,12 @@ export class FSAssetStore { return resolve(asset) }) } else { - return reject(`Failed to download asset ${JSON.stringify(asset)}`) + return reject(messages.errors.assetDownloadFailed(asset)) } }) .catch(reject) } catch (error) { - debug(`${asset.uid} asset download failed`) + debug(messages.info.assetDownloadFailed(asset.uid)) return reject(error) } @@ -132,7 +133,7 @@ export class FSAssetStore { * @returns {Promise} returns the asset object, if successful. */ public delete(assets: IAsset[]) { - debug('Asset deletion called for', JSON.stringify(assets)) + debug(messages.info.assetDeletionInitiated(), JSON.stringify(assets)) const asset = assets[0] return new Promise((resolve, reject) => { @@ -147,11 +148,11 @@ export class FSAssetStore { .then(() => rimraf(folderPath)) .then(() => resolve(asset)) .catch((error) => { - debug(`Error while removing ${folderPath} asset file`); + debug(messages.info.assetFileRemovalError(folderPath)); return reject(error); }); } else { - debug(`${folderPath} did not exist!`) + debug(messages.info.folderPathNotExist(folderPath)) return resolve(asset) } @@ -169,7 +170,7 @@ export class FSAssetStore { * @returns {Promise} returns the asset object, if successful. */ public unpublish(asset: IAsset) { - debug(`Asset unpublish called ${JSON.stringify(asset)}`) + debug(messages.info.assetUnpublishInitiated(asset)) return new Promise((resolve, reject) => { try { @@ -181,11 +182,11 @@ export class FSAssetStore { .then(() => rimraf(filePath)) .then(() => resolve(asset)) .catch((error) => { - debug(`Error while removing ${filePath} asset file`); + debug(messages.info.assetFileRemovalError(filePath)); return reject(error); }); } - debug(`${filePath} did not exist!`) + debug(messages.info.filePathNotExist(filePath)) return resolve(asset) } catch (error) { diff --git a/src/index.ts b/src/index.ts index 174cfde..5eff9b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { compact, merge } from 'lodash' import { join } from 'path' import { defaultConfig } from './config' import { FSAssetStore } from './filesystem' +import { messages } from './messages' let assetStoreConfig let assetStoreInstance @@ -45,7 +46,7 @@ export const getAssetLocation = (asset, config) => { if (asset[k]) { values.push(asset[k]) } else { - throw new TypeError(`The key ${keys[i]} did not exist on ${JSON.stringify(asset)}`) + throw new TypeError(messages.errors.keyNotExist(keys[i], asset)) } } else { values.push(keys[i]) @@ -89,7 +90,7 @@ export const getFileLocation = (asset, config) => { if (asset[k]) { values.push(asset[k]) } else { - throw new TypeError(`The key ${keys[i]} did not exist on ${JSON.stringify(asset)}`) + throw new TypeError(messages.errors.keyNotExist(keys[i], asset)) } } else { values.push(keys[i]) @@ -132,7 +133,7 @@ export function start(config) { assetStoreInstance = new FSAssetStore(assetStoreConfig) return resolve(assetStoreInstance) } catch (error) { - debug('Failed to load content-store due to', error) + debug(messages.errors.contentStoreLoadFailed(), error) reject(error) } }) diff --git a/src/messages.ts b/src/messages.ts new file mode 100644 index 0000000..4d2c59c --- /dev/null +++ b/src/messages.ts @@ -0,0 +1,39 @@ +/*! + * contentstack-sync-asset-store-filesystem + * copyright (c) Contentstack LLC + * MIT Licensed + */ + +/** + * Centralized error and log messages for the asset store filesystem + */ +export const messages = { + // Error messages + errors: { + keyNotExist: (key: string, asset: any) => + `The key '${key}' does not exist on: ${JSON.stringify(asset)}`, + contentStoreLoadFailed: () => + 'Failed to load content store:', + assetDownloadFailed: (asset: any) => + `Failed to download asset ${JSON.stringify(asset)}`, + }, + + // Info/Debug messages + info: { + assetDownloadInitiated: (asset: any) => + `Asset download initiated for: ${JSON.stringify(asset)}`, + assetDownloadFailed: (uid: string) => + `Download failed for asset: ${uid}`, + assetDeletionInitiated: () => + 'Asset deletion initiated:', + assetFileRemovalError: (path: string) => + `An error occurred while removing the asset file at: ${path}`, + folderPathNotExist: (path: string) => + `Folder path does not exist: ${path}`, + assetUnpublishInitiated: (asset: any) => + `Asset unpublish initiated for: ${JSON.stringify(asset)}`, + filePathNotExist: (path: string) => + `File path does not exist: ${path}`, + }, +} + diff --git a/test/index.js b/test/index.js index 72fe5ed..8067c01 100644 --- a/test/index.js +++ b/test/index.js @@ -8,6 +8,7 @@ const assetConnector = require('../dist') const config = require('../dist/config') const filesystem = require('fs') +const { messages } = require('../dist/messages') let connector = null let conf = { @@ -128,7 +129,8 @@ describe('# asset test', function () { test('# download non existent asset', function () { return connector.download(asset_data2).then(function () { }).catch(error =>{ - expect(error).toBe('blt9c4ef3c49f7b18h9 Asset download failed') + // Verify that download fails with correct error message + expect(error).toBe(messages.errors.assetDownloadFailed(asset_data2)) }) }) diff --git a/typings/messages.d.ts b/typings/messages.d.ts new file mode 100644 index 0000000..d0eff5e --- /dev/null +++ b/typings/messages.d.ts @@ -0,0 +1,23 @@ +/*! + * contentstack-sync-asset-store-filesystem + * copyright (c) Contentstack LLC + * MIT Licensed + */ + +export declare const messages: { + errors: { + keyNotExist: (key: string, asset: any) => string; + contentStoreLoadFailed: () => string; + assetDownloadFailed: (asset: any) => string; + }; + info: { + assetDownloadInitiated: (asset: any) => string; + assetDownloadFailed: (uid: string) => string; + assetDeletionInitiated: () => string; + assetFileRemovalError: (path: string) => string; + folderPathNotExist: (path: string) => string; + assetUnpublishInitiated: (asset: any) => string; + filePathNotExist: (path: string) => string; + }; +}; +