From 5b0a26827a844e2202db26bb3a2fb79a7b94bdce Mon Sep 17 00:00:00 2001 From: "Paul J. Davis" Date: Thu, 29 Oct 2009 15:10:16 -0400 Subject: [PATCH] Add Chris's toSource() for error messages. --- examples/error_nested_array_bounds.txt | 2 +- examples/error_nested_missing.txt | 2 +- examples/error_not_found.txt | 2 +- mustache.js | 13 ++++++------- test/mustache_spec.rb | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/examples/error_nested_array_bounds.txt b/examples/error_nested_array_bounds.txt index 7c865e7..53ed206 100644 --- a/examples/error_nested_array_bounds.txt +++ b/examples/error_nested_array_bounds.txt @@ -1 +1 @@ -ERROR: 'foo.5' not found in context +ERROR: 'foo.5' not found in context: {foo:[1, 2]} diff --git a/examples/error_nested_missing.txt b/examples/error_nested_missing.txt index d85c393..1954b64 100644 --- a/examples/error_nested_missing.txt +++ b/examples/error_nested_missing.txt @@ -1 +1 @@ -ERROR: 'foo.bar' not found in context +ERROR: 'foo.bar' not found in context: {not_foo:{not_bar:"yay error!"}} diff --git a/examples/error_not_found.txt b/examples/error_not_found.txt index fc8d6b7..df9e39c 100644 --- a/examples/error_not_found.txt +++ b/examples/error_not_found.txt @@ -1 +1 @@ -ERROR: 'foo' not found in context +ERROR: 'foo' not found in context: {bar:2} diff --git a/mustache.js b/mustache.js index 44dc394..b0ad601 100644 --- a/mustache.js +++ b/mustache.js @@ -30,10 +30,10 @@ var Mustache = function() { */ render_partial: function(name, context, partials) { if(typeof(context[name]) != "object") { - throw({message: "subcontext for '" + name + "' is not an object"}); + throw({message: "Context for '" + name + "' is not an object."}); } if(!partials || !partials[name]) { - throw({message: "unknown_partial"}); + throw({message: "No template for partial '" + name + "'."}); } return this.render(partials[name], context[name], partials); }, @@ -134,10 +134,7 @@ var Mustache = function() { if(typeof value === "function") { return value.apply(context); } - if(value !== undefined) { - return value; - } - throw({message: "'" + name + "' not found in context"}); + return value; }, // Utility methods @@ -167,7 +164,9 @@ var Mustache = function() { while(parts.length) { p = parts.shift(); if(ctx[p] === undefined) { - throw({message: "'" + name + "' not found in context"}); + src = context.toSource(); + src = src.replace(/^\(|\)$/g, ""); + throw({message: "'" + name + "' not found in context: " + src}); } ctx = ctx[p]; } diff --git a/test/mustache_spec.rb b/test/mustache_spec.rb index f4bbb22..4bd3ec6 100644 --- a/test/mustache_spec.rb +++ b/test/mustache_spec.rb @@ -37,7 +37,7 @@ describe "mustache" do print('ERROR: ' + e.message); } JS - run_js(js).should == "ERROR: 'x' not found in context\n" + run_js(js).should == "ERROR: 'x' not found in context: {list:[{}]}\n" end non_partials.each do |testname|