Skip to content

Commit b7971d8

Browse files
author
Dhwaneet Bhatt
authored
Merge pull request #66 from postmanlabs/feature/assign-user-errors
Assign user errors when curl conversion fails due to handled cases
2 parents d558b2b + 302f4ae commit b7971d8

File tree

7 files changed

+79
-16
lines changed

7 files changed

+79
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Assigned user errors for various handled errors
8+
59
## [v1.6.0] - 2023-04-17
610

711
### Added

src/UserError.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* constructor userError
3+
* @constructor
4+
* @param {*} message errorMessage
5+
* @param {*} data additional data to be reported
6+
*/
7+
class UserError extends Error {
8+
constructor(message, data) {
9+
super(message);
10+
this.name = 'UserError';
11+
this.data = data || {};
12+
}
13+
}
14+
15+
module.exports = UserError;

src/constants.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable max-len */
2+
3+
module.exports = {
4+
USER_ERRORS: {
5+
INVALID_FORMAT: 'Invalid format for cURL.',
6+
METHOD_NOT_SUPPORTED: (_, method) => { return `The method ${method} is not supported.`; },
7+
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.',
8+
UNABLE_TO_PARSE_NO_URL: 'Unable to parse: Could not identify the URL. Please use the --url option.',
9+
CANNOT_DETECT_URL: 'Could not detect the URL from cURL. Please make sure it\'s a valid cURL.',
10+
INPUT_WITHOUT_OPTIONS: 'Only the URL can be provided without an option preceding it. All other inputs must be specified via options.',
11+
MALFORMED_URL: 'Please check your cURL string for malformed URL.'
12+
}
13+
};
14+

src/convert.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = function (input, cb) {
1515
if (result.error) {
1616
return cb(null, {
1717
result: false,
18+
error: result.error,
1819
reason: result.error.message
1920
});
2021
}

src/getMetaData.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = function (input, cb) {
1515
if (result.error) {
1616
return cb(null, {
1717
result: false,
18+
error: result.error,
1819
reason: result.error.message
1920
});
2021
}

src/lib.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const commander = require('commander'),
44
shellQuote = require('../assets/shell-quote'),
55
unnecessaryOptions = require('../assets/unnecessaryOptions'),
66
supportedOptions = require('../assets/supportedOptions'),
7+
UserError = require('./UserError'),
8+
{ USER_ERRORS } = require('./constants'),
79
formDataOptions = ['-d', '--data', '--data-raw', '--data-binary', '--data-ascii'],
810
allowedOperators = ['<', '>', '(', ')'];
911

@@ -13,6 +15,13 @@ var program,
1315
requestUrl: '',
1416

1517
initialize: function() {
18+
/**
19+
* Collects values from the command line arguments and adds them to the memo array.
20+
*
21+
* @param {string} str - The argument value to collect.
22+
* @param {Array} memo - The array to add the collected values to.
23+
* @returns {Array} - The updated memo array.
24+
*/
1625
function collectValues(str, memo) {
1726
memo.push(str);
1827
return memo;
@@ -111,7 +120,7 @@ var program,
111120

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

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

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

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

370377
if (string.match(inCorrectlyFormedcURLRegex1) || string.match(inCorrectlyFormedcURLRegex2)) {
371-
throw Error('Please check your cURL string for malformed URL');
378+
throw new UserError(USER_ERRORS.MALFORMED_URL);
372379
}
373380
}
374381
else if (_.isFunction(arg.startsWith) && arg.startsWith('$') && arg.length > 1) {
@@ -420,8 +427,8 @@ var program,
420427
}
421428
catch (e) {
422429
if (e.message === 'process.exit is not a function') {
423-
// happened because of
424-
e.message = 'Invalid format for cURL.';
430+
// happened because of
431+
return { error: new UserError(USER_ERRORS.INVALID_FORMAT) };
425432
}
426433
return { error: e };
427434
}
@@ -445,7 +452,7 @@ var program,
445452
}
446453
}
447454
catch (e) {
448-
throw new Error('Unable to parse: Could not identify the URL. Please use the --url option.');
455+
throw new UserError(USER_ERRORS.UNABLE_TO_PARSE_NO_URL);
449456
}
450457
}
451458
/* eslint-enable */
@@ -479,7 +486,7 @@ var program,
479486
this.requestUrl = argStr;
480487
}
481488
else {
482-
throw new Error('Could not detect the URL from cURL. Please make sure it\'s a valid cURL');
489+
throw new UserError(USER_ERRORS.CANNOT_DETECT_URL);
483490
}
484491
},
485492

@@ -666,7 +673,7 @@ var program,
666673
return this.validate(curlString, false);
667674
}
668675

669-
return { result: false, reason: e.message };
676+
return { result: false, reason: e.message, error: e };
670677
}
671678
},
672679

@@ -796,7 +803,7 @@ var program,
796803
}
797804
}
798805
if (e.message === 'process.exit is not a function') {
799-
e.message = 'Invalid format for cURL.';
806+
return { error: new UserError(USER_ERRORS.INVALID_FORMAT) };
800807
}
801808
return { error: e };
802809
}

test/conversion.test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('validate', function () {
1414
expect(result.result).to.equal(false);
1515
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
1616
' Please use the --url option.');
17+
expect(result.error).to.have.property('message', result.reason);
1718
done();
1819
});
1920
});
@@ -41,6 +42,7 @@ describe('getMetaData', function () {
4142
expect(result.result).to.equal(false);
4243
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
4344
' Please use the --url option.');
45+
expect(result.error).to.have.property('message', result.reason);
4446
done();
4547
});
4648
});
@@ -53,6 +55,7 @@ describe('getMetaData', function () {
5355
expect(result.result).to.equal(false);
5456
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
5557
' Please use the --url option.');
58+
expect(result.error).to.have.property('message', result.reason);
5659
done();
5760
});
5861
});
@@ -68,6 +71,7 @@ describe('Curl converter should', function() {
6871
expect(result.result).to.equal(false);
6972
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
7073
' Please use the --url option.');
74+
expect(result.error).to.have.property('message', result.reason);
7175
done();
7276
});
7377
});
@@ -80,10 +84,22 @@ describe('Curl converter should', function() {
8084
expect(result.result).to.equal(false);
8185
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
8286
' Please use the --url option.');
87+
expect(result.error).to.have.property('message', result.reason);
8388
done();
8489
});
8590
});
8691

92+
it('throw an error when an invalid method is specificied', function (done) {
93+
convert({
94+
type: 'string',
95+
data: 'curl --request INVALIDMETHOD --url http://www.google.com'
96+
}, function (err, result) {
97+
expect(result.result).to.equal(false);
98+
expect(result.reason).to.equal('The method INVALIDMETHOD is not supported.');
99+
expect(result.error).to.have.property('message', result.reason);
100+
done();
101+
});
102+
});
87103

88104
it('throw an error for a cURL without URL defined correctly', function (done) {
89105
convert({
@@ -93,6 +109,7 @@ describe('Curl converter should', function() {
93109
expect(result.result).to.equal(false);
94110
expect(result.reason).to.equal('Unable to parse: Could not identify the URL.' +
95111
' Please use the --url option.');
112+
expect(result.error).to.have.property('message', result.reason);
96113
done();
97114
});
98115
});
@@ -247,7 +264,8 @@ describe('Curl converter should', function() {
247264
}, function (err, result) {
248265
expect(result.result).to.equal(false);
249266
expect(result.reason).to.equal('Unable to parse: Both (--head/-I) and' +
250-
' (-d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode) are not supported');
267+
' (-d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode) are not supported.');
268+
expect(result.error).to.have.property('message', result.reason);
251269
done();
252270
});
253271
});
@@ -937,7 +955,8 @@ describe('Curl converter should', function() {
937955
test?bar=1&baz=2`
938956
}, function (err, result) {
939957
expect(result.result).to.equal(false);
940-
expect(result.reason).to.equal('Please check your cURL string for malformed URL');
958+
expect(result.reason).to.equal('Please check your cURL string for malformed URL.');
959+
expect(result.error).to.have.property('message', result.reason);
941960
done();
942961
});
943962
});
@@ -951,7 +970,8 @@ describe('Curl converter should', function() {
951970
bar=1&baz=2`
952971
}, function (err, result) {
953972
expect(result.result).to.equal(false);
954-
expect(result.reason).to.equal('Please check your cURL string for malformed URL');
973+
expect(result.reason).to.equal('Please check your cURL string for malformed URL.');
974+
expect(result.error).to.have.property('message', result.reason);
955975
done();
956976
});
957977
});
@@ -966,7 +986,8 @@ describe('Curl converter should', function() {
966986
"test.com/?bar=1&baz=2`
967987
}, function (err, result) {
968988
expect(result.result).to.equal(false);
969-
expect(result.reason).to.equal('Please check your cURL string for malformed URL');
989+
expect(result.reason).to.equal('Please check your cURL string for malformed URL.');
990+
expect(result.error).to.have.property('message', result.reason);
970991
done();
971992
});
972993
});

0 commit comments

Comments
 (0)