Skip to content

Commit

Permalink
Added support for reporting UserErrors when provided definition is in…
Browse files Browse the repository at this point in the history
…valid
  • Loading branch information
VShingala committed Jan 31, 2024
1 parent dae7368 commit 15c11ba
Show file tree
Hide file tree
Showing 9 changed files with 4,380 additions and 572 deletions.
15 changes: 15 additions & 0 deletions lib/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;
8 changes: 4 additions & 4 deletions lib/convert.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 15c11ba

Please sign in to comment.