Skip to content

Commit

Permalink
Merge pull request #66 from postmanlabs/feature/assign-user-errors
Browse files Browse the repository at this point in the history
Assign user errors when curl conversion fails due to handled cases
  • Loading branch information
Dhwaneet Bhatt authored Jun 22, 2023
2 parents d558b2b + 302f4ae commit b7971d8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Changed

- Assigned user errors for various handled errors

## [v1.6.0] - 2023-04-17

### Added
Expand Down
15 changes: 15 additions & 0 deletions src/UserError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* constructor userError
* @constructor
* @param {*} message errorMessage
* @param {*} data additional data to be reported
*/
class UserError extends Error {
constructor(message, data) {
super(message);
this.name = 'UserError';
this.data = data || {};
}
}

module.exports = UserError;
14 changes: 14 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable max-len */

module.exports = {
USER_ERRORS: {
INVALID_FORMAT: 'Invalid format for cURL.',
METHOD_NOT_SUPPORTED: (_, method) => { return `The method ${method} is not supported.`; },
UNABLE_TO_PARSE_HEAD_AND_DATA: 'Unable to parse: Both (--head/-I) and (-d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode) are not supported.',
UNABLE_TO_PARSE_NO_URL: 'Unable to parse: Could not identify the URL. Please use the --url option.',
CANNOT_DETECT_URL: 'Could not detect the URL from cURL. Please make sure it\'s a valid cURL.',
INPUT_WITHOUT_OPTIONS: 'Only the URL can be provided without an option preceding it. All other inputs must be specified via options.',
MALFORMED_URL: 'Please check your cURL string for malformed URL.'
}
};

1 change: 1 addition & 0 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function (input, cb) {
if (result.error) {
return cb(null, {
result: false,
error: result.error,
reason: result.error.message
});
}
Expand Down
1 change: 1 addition & 0 deletions src/getMetaData.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function (input, cb) {
if (result.error) {
return cb(null, {
result: false,
error: result.error,
reason: result.error.message
});
}
Expand Down
31 changes: 19 additions & 12 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const commander = require('commander'),
shellQuote = require('../assets/shell-quote'),
unnecessaryOptions = require('../assets/unnecessaryOptions'),
supportedOptions = require('../assets/supportedOptions'),
UserError = require('./UserError'),
{ USER_ERRORS } = require('./constants'),
formDataOptions = ['-d', '--data', '--data-raw', '--data-binary', '--data-ascii'],
allowedOperators = ['<', '>', '(', ')'];

Expand All @@ -13,6 +15,13 @@ var program,
requestUrl: '',

initialize: function() {
/**
* Collects values from the command line arguments and adds them to the memo array.
*
* @param {string} str - The argument value to collect.
* @param {Array} memo - The array to add the collected values to.
* @returns {Array} - The updated memo array.
*/
function collectValues(str, memo) {
memo.push(str);
return memo;
Expand Down Expand Up @@ -111,7 +120,7 @@ var program,

if (validMethods.indexOf(curlObj.request.toUpperCase()) === -1) {
// the method is still not valid
throw new Error('The method ' + curlObj.request + ' is not supported');
throw new UserError(USER_ERRORS.METHOD_NOT_SUPPORTED`${curlObj.request}`);
}
}

Expand All @@ -120,8 +129,7 @@ var program,
if ((curlObj.data.length > 0 || curlObj.dataAscii.length > 0 ||
curlObj.dataBinary || curlObj.dataUrlencode.length > 0) &&
curlObj.head && !curlObj.get) {
throw new Error('Unable to parse: Both (--head/-I) and' +
' (-d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode) are not supported');
throw new UserError(USER_ERRORS.UNABLE_TO_PARSE_HEAD_AND_DATA);
}

/**
Expand All @@ -130,8 +138,7 @@ var program,
* once it fails here using convertForCMDFormat()
*/
if (curlObj.args.length > 1 && _.includes(curlObj.args, '^')) {
throw new Error('Only the URL can be provided without an option preceding it.' +
' All other inputs must be specified via options.');
throw new UserError(USER_ERRORS.INPUT_WITHOUT_OPTIONS);
}
},

Expand Down Expand Up @@ -368,7 +375,7 @@ var program,
inCorrectlyFormedcURLRegex2 = /(\w+=\w+&?)/g; // checks - foo?bar=1&baz=2

if (string.match(inCorrectlyFormedcURLRegex1) || string.match(inCorrectlyFormedcURLRegex2)) {
throw Error('Please check your cURL string for malformed URL');
throw new UserError(USER_ERRORS.MALFORMED_URL);
}
}
else if (_.isFunction(arg.startsWith) && arg.startsWith('$') && arg.length > 1) {
Expand Down Expand Up @@ -420,8 +427,8 @@ var program,
}
catch (e) {
if (e.message === 'process.exit is not a function') {
// happened because of
e.message = 'Invalid format for cURL.';
// happened because of
return { error: new UserError(USER_ERRORS.INVALID_FORMAT) };
}
return { error: e };
}
Expand All @@ -445,7 +452,7 @@ var program,
}
}
catch (e) {
throw new Error('Unable to parse: Could not identify the URL. Please use the --url option.');
throw new UserError(USER_ERRORS.UNABLE_TO_PARSE_NO_URL);
}
}
/* eslint-enable */
Expand Down Expand Up @@ -479,7 +486,7 @@ var program,
this.requestUrl = argStr;
}
else {
throw new Error('Could not detect the URL from cURL. Please make sure it\'s a valid cURL');
throw new UserError(USER_ERRORS.CANNOT_DETECT_URL);
}
},

Expand Down Expand Up @@ -666,7 +673,7 @@ var program,
return this.validate(curlString, false);
}

return { result: false, reason: e.message };
return { result: false, reason: e.message, error: e };
}
},

Expand Down Expand Up @@ -796,7 +803,7 @@ var program,
}
}
if (e.message === 'process.exit is not a function') {
e.message = 'Invalid format for cURL.';
return { error: new UserError(USER_ERRORS.INVALID_FORMAT) };
}
return { error: e };
}
Expand Down
29 changes: 25 additions & 4 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('validate', function () {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
' Please use the --url option.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand Down Expand Up @@ -41,6 +42,7 @@ describe('getMetaData', function () {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
' Please use the --url option.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand All @@ -53,6 +55,7 @@ describe('getMetaData', function () {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
' Please use the --url option.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand All @@ -68,6 +71,7 @@ describe('Curl converter should', function() {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
' Please use the --url option.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand All @@ -80,10 +84,22 @@ describe('Curl converter should', function() {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
' Please use the --url option.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});

it('throw an error when an invalid method is specificied', function (done) {
convert({
type: 'string',
data: 'curl --request INVALIDMETHOD --url http://www.google.com'
}, function (err, result) {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('The method INVALIDMETHOD is not supported.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});

it('throw an error for a cURL without URL defined correctly', function (done) {
convert({
Expand All @@ -93,6 +109,7 @@ describe('Curl converter should', function() {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
' Please use the --url option.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand Down Expand Up @@ -247,7 +264,8 @@ describe('Curl converter should', function() {
}, function (err, result) {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Unable to parse: Both (--head/-I) and' +
' (-d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode) are not supported');
' (-d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode) are not supported.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand Down Expand Up @@ -937,7 +955,8 @@ describe('Curl converter should', function() {
test?bar=1&baz=2`
}, function (err, result) {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Please check your cURL string for malformed URL');
expect(result.reason).to.equal('Please check your cURL string for malformed URL.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand All @@ -951,7 +970,8 @@ describe('Curl converter should', function() {
bar=1&baz=2`
}, function (err, result) {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Please check your cURL string for malformed URL');
expect(result.reason).to.equal('Please check your cURL string for malformed URL.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand All @@ -966,7 +986,8 @@ describe('Curl converter should', function() {
"test.com/?bar=1&baz=2`
}, function (err, result) {
expect(result.result).to.equal(false);
expect(result.reason).to.equal('Please check your cURL string for malformed URL');
expect(result.reason).to.equal('Please check your cURL string for malformed URL.');
expect(result.error).to.have.property('message', result.reason);
done();
});
});
Expand Down

0 comments on commit b7971d8

Please sign in to comment.