Skip to content

Instantly share code, notes, and snippets.

@TechNinjaWeb
Created December 19, 2015 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechNinjaWeb/f46a2b2fd8c911392561 to your computer and use it in GitHub Desktop.
Save TechNinjaWeb/f46a2b2fd8c911392561 to your computer and use it in GitHub Desktop.
Error Handling Test Modules
m.buildView = function(element, node) {
try {
if (element !== 'string' || element !== 'object') throw new Error("Element needs to be a string or Object");
// Set Element Equal to 1st Query Selector for Element
if (element === 'string') element = Array.prototype.slice.call(document.querySelectorAll(element))[0];
// Set Element Equal to HTML Element
if (element === 'object' && element.constructor.name.search('HTML') >= 0) element = element;
m.alertClient('log', "ELEMENT IS?", element);
} catch (e) {
m.throwError(e.message, {arguments: arguments});
}
try {
// Check if node is an Array
if (Array.isArray(node))
node.forEach(function(n){
// If it's an object, append to element
if (n === 'object') element.appendChild(n);
else throw new Error('Node is Not an Array or Object');
});
// Is Node a stand alone Object
else if (typeof node === 'object')
// Append it to Element
element.appendChild(node);
// Throw Error if Not
else throw new Error('Node is Not an Array or Object');
} catch (e) {
// Display message to Client
m.throwError(e.message, {arguments: arguments});
// Attempt to resolve conflict
} finally {
try {
// Do the element and node arguments exist
if (!!element && !!node)
// If so, is the node a string?
if (node === 'string')
throw new Error('Node is String. Expecting Object or Array');
if (element !== 'string')
throw new Error('Element needs to be string');
} catch (e) {
m.throwError(e.message, {arguments: arguments});
} finally {
m.alertClient('warn', "function will return arguments",arguments);
return arguments
}
}
}
(function tnModules(m) {
// Recycle Original Module or Create New
m = m === window ? m.tnModules = {} : m;
// Client Messages Off By Default
m.alerts = {on: false};
// Hide Property From Tree
Object.defineProperty(m, 'alerts', {
enumerable: false,
writable: true
});
// Begin Class Functions
m.makeNodes = function(nodes) {
// Node must be an array or obj with
// type: 'node type', properties: [{prop: val}]
// Function will return array of nodes
try {
if (!nodes || Object.keys(nodes).length <= 0) throw new Error('Object has empty properties');
} catch (e) {
return m.throwError(e.message, {arguments: arguments, line: e.lineNumber});
}
if (Array.isArray(nodes))
return nodes.reduce(function(cb, node, i, a) {
var n = document.createElement(node.type);
node.properties.forEach(function(props) {
for (var prop in props) {
n[prop] = props[prop];
}
});
cb.push(n);
return cb;
}, []);
else if (typeof nodes === 'object' && !!nodes.type) {
var n = document.createElement(nodes.type);
nodes.properties.forEach(function(props) {
for (var prop in props) {
n[prop] = props[prop];
}
});
return n;
} else try {
if (nodes !== 'object' && !node.type) throw new Error('Argument needs to be an Object with type property');
} catch (e) {
m.throwError(e.message, {arguments: arguments, line: e.lineNumber});
} finally {
// Return basic div element
m.alertClient('warn', "Returning Basic Div Element");
return document.createElement('div');
}
}
m.buildView = function(element, node) {
try {
// Failsafes
if (!element) throw new Error('No Element Defined');
if (!node) throw new Error('No Node Defined');
if (!element && !node) throw new Error('You Didnt Pass Any Arguments');
} catch (e) {
m.throwError(e.message, {arguments: arguments, line: e.lineNumber});
} finally {
try {
// Check if node is an Array
if (Array.isArray(node))
node.forEach(function(n){
// If it's an object, append to element
if (n === 'object') element.appendChild(n);
else throw new Error('Node is Not an Object');
});
// If Node is a stand-alone object
if (typeof node === 'object' && Object.hasOwnProperty('type'))
// Append it to Element
element.appendChild(node);
else throw new Error('Node is Not an Object');
} catch (e) {
m.throwError(e.message, {arguments: arguments, line: e.lineNumber});
}
}
}
m.getDomNodes = function(name) {
// Name can be string or Array
// If Array, then join all into one string
if (Array.isArray(name)) {
return Array.prototype.slice.call(document.querySelectorAll(name.join(',')));
} else if (name == 'string')
return Array.prototype.slice.call(document.querySelectorAll(name));
}
m.returnPromise = function(func, args, callback) {
return new Promise(function(res, rej) {
});
}
m.throwError = function(msg, args) {
console.error(msg, args);
}
m.alertClient = function(type, msg, args) {
if (m.alerts.on)
switch (type) {
case 'log':
console[type](msg, {arguments: args});
break;
case 'info':
console[type](msg, {arguments: args});
break;
case 'warn':
console[type](msg, {arguments: args});
break;
}
}
// Export Module to Window
return m;
}(this.tnModules || this))
// var nodes = [{
// type: 'div',
// properties: [{
// "innerHTML": 'Hello, world',
// "style.marginLeft": '12px'
// }]
// }, {
// type: 'ul',
// properties: [{
// "innerHTML": 'this is ul',
// "data": {
// d: 'heres some data'
// }
// }]
// }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment