Skip to content

Commit c48e612

Browse files
author
WebCoder49
committed
Clean up: Add sensible error messages
1 parent fa249a9 commit c48e612

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

code-input.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ var codeInput = {
8585
* @param {Object} template - a Template object instance - see `codeInput.templates`
8686
*/
8787
registerTemplate: function (templateName, template) {
88+
if(!(typeof templateName == "string" || templateName instanceof String)) throw TypeError(`Template for "${templateName}" must be a string.`);
89+
if(!(typeof template.highlight == "function" || template.highlight instanceof Function)) throw TypeError(`Template for "${templateName}" invalid, because the highlight function provided is not a function; it is "${template.highlight}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
90+
if(!(typeof template.includeCodeInputInHighlightFunc == "boolean" || template.includeCodeInputInHighlightFunc instanceof Boolean)) throw TypeError(`Template for "${templateName}" invalid, because the includeCodeInputInHighlightFunc value provided is not a true or false; it is "${template.includeCodeInputInHighlightFunc}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
91+
if(!(typeof template.preElementStyled == "boolean" || template.preElementStyled instanceof Boolean)) throw TypeError(`Template for "${templateName}" invalid, because the preElementStyled value provided is not a true or false; it is "${template.preElementStyled}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
92+
if(!(typeof template.isCode == "boolean" || template.isCode instanceof Boolean)) throw TypeError(`Template for "${templateName}" invalid, because the isCode value provided is not a true or false; it is "${template.isCode}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
93+
if(!Array.isArray(template.plugins)) throw TypeError(`Template for "${templateName}" invalid, because the plugin array provided is not an array; it is "${template.plugins}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
94+
template.plugins.forEach((plugin, i) => {
95+
if(!(plugin instanceof codeInput.Plugin)) {
96+
throw TypeError(`Template for "${templateName}" invalid, because the plugin provided at index ${i} is not valid; it is "${template.plugins[i]}". Please make sure you use one of the constructors in codeInput.templates, and that you provide the correct arguments.`);
97+
}
98+
});
99+
88100
codeInput.usedTemplates[templateName] = template;
89101
// Add waiting code-input elements wanting this template from queue
90102
if (templateName in codeInput.templateNotYetRegisteredQueue) {
@@ -257,7 +269,7 @@ var codeInput = {
257269
*/
258270
Plugin: class {
259271
constructor() {
260-
console.log("code-input: plugin: Created plugin!");
272+
console.log("code-input: plugin: Created plugin");
261273

262274
codeInput.observedAttributes = codeInput.observedAttributes.concat(self.observedAttributes);
263275
}

0 commit comments

Comments
 (0)