|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var formatMessage = function(message) {
|
|
|
|
message = '\'' + message + '\'';
|
|
|
|
for (var i = 0; i < 3; i++) {
|
|
|
|
message += '.replace(\'{' + i + '}\', arguments[' + i + '])';
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
var defineElement = function(fullName, baseClass, message) {
|
|
|
|
return fullName + ' = function() {\n' +
|
|
|
|
' this.message = ' + formatMessage(message) + ';\n' +
|
|
|
|
' this.stack = this.message + \'\\n\' + (new Error()).stack;\n' +
|
|
|
|
'};\n' +
|
|
|
|
fullName + '.prototype = Object.create(' + baseClass + '.prototype);\n' +
|
|
|
|
fullName + '.prototype.name = "' + fullName + '";\n\n';
|
|
|
|
};
|
|
|
|
|
|
|
|
var traverseNode = function(baseClass, errorDefinition) {
|
|
|
|
var className = baseClass + '.' + errorDefinition.name;
|
|
|
|
var generated = defineElement(className, baseClass, errorDefinition.message);
|
|
|
|
if (errorDefinition.errors) {
|
|
|
|
generated += childDefinitions(className, errorDefinition.errors);
|
|
|
|
}
|
|
|
|
return generated;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* jshint latedef: false */
|
|
|
|
var childDefinitions = function(parent, childDefinitions) {
|
|
|
|
var generated = '';
|
|
|
|
_.each(childDefinitions, function(childDefinition) {
|
|
|
|
generated += traverseNode(parent, childDefinition);
|
|
|
|
});
|
|
|
|
return generated;
|
|
|
|
};
|
|
|
|
/* jshint latedef: true */
|
|
|
|
|
|
|
|
var traverseRoot = function(errorsDefinition) {
|
|
|
|
var fullName = 'bitcore.Error';
|
|
|
|
var path = 'Error';
|
|
|
|
var generated = '\'use strict\';\n\nvar inherits = require(\'inherits\');\n\n';
|
|
|
|
generated += '/** AUTOGENERATED FILE. DON\'T EDIT, MODIFY "lib/errors/spec.js" INSTEAD */\n\n';
|
|
|
|
generated += 'var bitcore = {};\n\n';
|
|
|
|
generated += defineElement(fullName, path, 'Internal error');
|
|
|
|
generated += childDefinitions(fullName, errorsDefinition);
|
|
|
|
generated += 'module.exports = bitcore.Error;\n';
|
|
|
|
return generated;
|
|
|
|
};
|
|
|
|
|
|
|
|
var data = require('./spec');
|
|
|
|
fs.writeFileSync(__dirname + '/index.js', traverseRoot(data));
|