Skip to content

Commit

Permalink
Added error handling for invalid collection file (#26)
Browse files Browse the repository at this point in the history
* Updated test name

* Removed dependency

* Updated version

* Added strict check

* Added entry point

* Added entry point

* Updated tests

* Added file parsing error handling

* Added file parsing error handling

* Delete index.js
  • Loading branch information
praveendvd authored Feb 2, 2022
1 parent 71d5687 commit 99e6fa4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ Added main entry point to the package to allow to be used as a dependency

### Fixed

## [2.0.7] - 2022-02-02

### Added

Added error handling for invalid source collection

### Fixed
2 changes: 2 additions & 0 deletions __test__/__snapshots__/test-cli-unit.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,5 @@ Options:
exports[`Validate index.js unit tests Validate cli output messages for non interactive Should show help if arguments are missing 7`] = `[MockFunction]`;

exports[`Validate index.js unit tests Validate cli output messages for non interactive Should throw error if exception thrown doesnt contain error code ENOENT 1`] = `"Error: Custom error"`;

exports[`Validate index.js unit tests Validate cli output messages for non interactive Should throw invalid collection if a valid collection is not provided 1`] = `"exit"`;
10 changes: 10 additions & 0 deletions __test__/test-cli-unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,17 @@ describe('Validate index.js unit tests', () => {
jest.clearAllMocks()
mockConsoleLog.mockImplementation(() => { throw new CustomError(256) });
await expect(postman_url_updater.createNewCollection).rejects.toThrowErrorMatchingSnapshot()
expect(process.exit).not.toBeCalled()
});

it('Should throw invalid collection if a valid collection is not provided', async () => {
resetAndReimportUrlUpdator({ c: "__test__/__snapshots__/test-cli-unit.test.js.snap", r: "{{baseURL}}/{{path}}", w: "{{baseURL}}/{{path}}", s: "new_collection.json", p: undefined, i: undefined });
postman_url_updater = require('../src/index.js')
jest.clearAllMocks()
await expect(postman_url_updater.createNewCollection).rejects.toThrowErrorMatchingSnapshot()
expect(console.error).toBeCalledWith(`Invalid/corrupted collection provided. Please provide path to valid source collection`);
});

})

describe('Validate cli output messages for interactive', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-collection-url-updater",
"version": "2.0.6",
"version": "2.0.7",
"description": "Postman collection url updater is a command line utility project that can update urls across multiple postman requests in your postman collection. It allows to update postman request urls in bulk inside the postman collection",
"scripts": {
"test": "jest"
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ async function startConvert() {
fs.outputFileSync(DESTINATIONPATH, JSON.stringify(sourceCollection.toJSON(), null, 2))
console.log(chalk.green('File saved to: ') + chalk.yellowBright(DESTINATIONPATH))
} catch (e) {
if (!(e.code === 'ENOENT')) throw Error(e)
console.error(chalk.red(e.message))
if (!(e.code === 'ENOENT')
&&
!(e.stack && e.stack.includes("at JSON.parse"))) throw Error(e);

console.error(chalk.red((e.stack && e.stack.includes("at JSON.parse")) ? "Invalid/corrupted collection provided. Please provide path to valid source collection"
: (e.message)))
process.exit()
}
}
Expand Down

0 comments on commit 99e6fa4

Please sign in to comment.