Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
19 changes: 10 additions & 9 deletions src/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
validateUnPublishAsset,
sanitizePath
} from './utils'
import { messages } from './messages'

const debug = Debug('asset-store-filesystem')

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) => {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
}
})
Expand Down
39 changes: 39 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
@@ -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}`,
},
}

4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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))
})
})

Expand Down
23 changes: 23 additions & 0 deletions typings/messages.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
};

Loading