Skip to content

Commit

Permalink
catching JSON parse errors and throwing a more explicit one
Browse files Browse the repository at this point in the history
  • Loading branch information
anutron committed Nov 4, 2011
1 parent 086d153 commit a3019ff
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Source/BehaviorAPI.js
Expand Up @@ -95,15 +95,19 @@ provides: [BehaviorAPI]
},
//gets the data-behaviorname-options object and parses it as JSON
_getOptions: function(){
if (!this.options){
var options = this.element.getData(this.prefix + '-options', '{}');
if (options && options.substring(0,1) != '{') options = '{' + options + '}';
var isSecure = JSON.isSecure(options);
if (!isSecure) throw new Error('warning, options value for element is not parsable, check your JSON format for quotes, etc.');
this.options = isSecure ? JSON.decode(options) : {};
for (option in this.options) {
this.options[option.camelCase()] = this.options[option];
try {
if (!this.options){
var options = this.element.getData(this.prefix + '-options', '{}');
if (options && options.substring(0,1) != '{') options = '{' + options + '}';
var isSecure = JSON.isSecure(options);
if (!isSecure) throw new Error('warning, options value for element is not parsable, check your JSON format for quotes, etc.');
this.options = isSecure ? JSON.decode(options) : {};
for (option in this.options) {
this.options[option.camelCase()] = this.options[option];
}
}
} catch (e){
throw new Error('Could not get options from element; check your syntax. ' + this.prefix + '-options: "' + this.element.getData(this.prefix + '-options', '{}') + '"');
}
return this.options;
},
Expand Down

0 comments on commit a3019ff

Please sign in to comment.