Skip to content

Commit

Permalink
fix(contract): clear error when can't parse call arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jul 19, 2022
1 parent 9d16eb7 commit 34ae285
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/commands/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import { nodeOption, jsonOption, gasOption } from '../arguments';

const callArgs = new Argument('[args]', 'JSON-encoded arguments array of contract call')
.argParser((argsText) => {
const args = JSON.parse(argsText);
let args;
try {
args = JSON.parse(argsText);
} catch (error) {
throw new CliError(`Can't parse contract arguments: ${error.message}`);
}
if (!Array.isArray(args)) throw new CliError(`Call arguments should be an array, got ${argsText} instead`);
return args;
})
Expand Down
11 changes: 11 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { expect } from 'chai';
import { decode } from '@aeternity/aepp-sdk';
import { executeProgram, getSdk, WALLET_NAME } from './index';
import contractProgram from '../src/commands/contract';
import CliError from '../src/utils/CliError';

const executeContract = (args) => executeProgram(contractProgram, args);

Expand Down Expand Up @@ -117,6 +118,16 @@ describe('CLI Contract Module', function contractTests() {
fs.unlinkSync(descrPath);
fs.unlinkSync(contractBytecodeFile);
});

it('throws error if arguments invalid', async () => {
await expect(executeContract([
'deploy',
WALLET_NAME, '--password', 'test',
'--contractSource', contractSourceFile,
'[3',
'--json',
])).to.be.rejectedWith(CliError, 'Can\'t parse contract arguments: Unexpected end of JSON input');
});
});

describe('Call', () => {
Expand Down

0 comments on commit 34ae285

Please sign in to comment.