Skip to content

Commit

Permalink
Merge pull request #81 from postmanlabs/feature/error-classification
Browse files Browse the repository at this point in the history
Added support for reporting UserErrors when provided definition is invalid.
  • Loading branch information
VShingala committed Feb 14, 2024
2 parents dae7368 + 362af88 commit 29865ee
Show file tree
Hide file tree
Showing 11 changed files with 4,384 additions and 573 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 16.x, 18.x]
node-version: [16.x, 18.x, 20.x]
steps:
- name: Get Code
uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions .npmignore
Expand Up @@ -9,6 +9,7 @@
/.gitignore
/test/
/scripts/
/npm/

### Borrowed from .gitignore
### ========================
Expand All @@ -20,6 +21,8 @@

## Directory-based project format:
.idea/
.vscode/

# if you remove the above rule, at least ignore the following:

# User-specific stuff:
Expand Down
15 changes: 15 additions & 0 deletions lib/UserError.js
@@ -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;
8 changes: 4 additions & 4 deletions lib/convert.js
Expand Up @@ -6,6 +6,7 @@ const raml = require('./../assets/raml-1-parser'),
helper = require('./helper.js'),
getOptions = require('./options').getOptions,
{ Node, Trie } = require('./trie.js'),
{ generateError } = require('./generateError.js'),

// This is the default collection name if one can't be inferred from the RAML 1.0 spec
COLLECTION_NAME = 'Converted from RAML 1.0';
Expand Down Expand Up @@ -250,10 +251,9 @@ var converter = {
}]
});
}
catch (e) {
return cb(null, {
result: false,
reason: e.toString()
catch (originalError) {
return generateError(ramlString, originalError, (constructedError) => {
return cb(constructedError);
});
}

Expand Down
46 changes: 46 additions & 0 deletions lib/generateError.js
@@ -0,0 +1,46 @@
const _ = require('lodash'),
wap = require('webapi-parser').WebApiParser,
UserError = require('./UserError');

/**
* Validates given RAML 1.0 definition using WebAPI Parser and
* generates UserError or Unhandled error depending upon type of error.
*
* @param {Object} definition - RAML definition
* @param {*} error - Original Error object
* @param {Function} cb - Callback function
* @returns {*} - Generated Error object
*/
function generateError (definition, error, cb) {
wap.raml10.parse(definition, '')
.then((model) => {
wap.raml10.validate(model)
.then((report) => {
if (report.conforms || report.results.length === 0) {
if (error instanceof Error) {
return cb(error);
}

const errorMessage = typeof error === 'string' ? error :
_.get(error, 'message', 'Failed to generate collection.');

return cb(new Error(errorMessage));
}

let lastReportRes = report.results.pop(),
message = lastReportRes.message;

return cb(new UserError(message, error));
})
.catch(() => {
return cb(new UserError('Provided RAML 1.0 definition is invalid.', error));
});
})
.catch(() => {
return cb(new UserError('Provided RAML 1.0 definition is invalid.', error));
});
}

module.exports = {
generateError
};
6 changes: 4 additions & 2 deletions lib/helper.js
Expand Up @@ -108,7 +108,7 @@ function addTraitsToMethod(method, traits) {
method.is.forEach(function(trait) {
for (property in traits[trait]) {
if (property !== 'name') {
if (modified_method[property]) {
if (modified_method[property] && typeof modified_method[property] === 'object') {
Object.assign(modified_method[property], traits[trait][property]);
}
else {
Expand Down Expand Up @@ -1058,7 +1058,9 @@ helper = {

return resolvedResource;
});
}
},

addTraitsToMethod
};

module.exports = helper;

0 comments on commit 29865ee

Please sign in to comment.