Permalink
Browse files

Push to 3.1.0

Added `returnErrors` option to Blade middleware
Live UI plugin: Fixed a bug to properly throw errors
  • Loading branch information...
1 parent 87450b6 commit 4fd513b6071453e8c4a88b7788b24a7bc15f7794 @bminer committed Mar 20, 2013
Showing with 37 additions and 13 deletions.
  1. +6 −0 README.md
  2. +20 −8 lib/blade.js
  3. +8 −3 lib/runtime.js
  4. +1 −1 package.json
  5. +1 −0 plugins/liveui.js
  6. +1 −1 smart.json
View
@@ -1230,6 +1230,12 @@ can compile the view stored at `sourcePath + "/homepage.blade"`
- `pluginsMount` - the URL path where Blade plugins will be served to the
browser (defaults to "/blade/plugins/"). Use `null` to disable this
functionality.
+ - `returnErrors` - if true, compilation errors are exposed to the client
+ (i.e. passed to the callback function that was passed to
+ `blade.Runtime.loadTemplate`); if false, compilation errors are passed
+ to the [error-handling middleware]
+ (http://expressjs.com/guide.html#error-handling). Defaults to
+ `process.env.NODE_ENV == "development"` if unspecified.
- `compileOptions` - options passed to `blade.compile()`. Defaults to:
```javascript
View
@@ -99,6 +99,8 @@ function middleware(sourcePath, options) {
options.runtimeMount = "/blade/blade.js";
if(typeof options.pluginsMount == "undefined")
options.pluginsMount = "/blade/plugins/";
+ if(typeof options.returnErrors == "undefined")
+ options.returnErrors = process.env.NODE_ENV == "development";
if(options.compileOptions == null)
options.compileOptions = {
"cache": process.env.NODE_ENV == "production",
@@ -161,16 +163,26 @@ function middleware(sourcePath, options) {
if(fullPath.indexOf(sourcePath) != 0)
return res.type("text/plain").send("403 Forbidden", 403); //malicious filename
compileFile(fullPath, options.compileOptions, function(err, tmpl) {
- if(err) return next(err);
res.type("application/javascript");
var viewName = JSON.stringify(filename.replace(path.sep, "/") );
- var deps = [];
- for(var i = 0; i < tmpl.dependencies.length; i++)
- deps[i] = tmpl.dependencies[i].replace(path.sep, "/");
- res.send("blade._cachedViews[" + viewName + "]=" + tmpl.toString() +
- ";if(blade._cb[" + viewName + "])blade._cb[" + viewName + "](" +
- JSON.stringify(tmpl.reldir.replace(path.sep, "/") ) + "," +
- JSON.stringify(deps) + "," + tmpl.unknownDependencies + ");");
+ if(err)
+ {
+ if(options.returnErrors)
+ res.send("var e=blade._cb[" + viewName + "];e&&e(" +
+ "new Error(" + JSON.stringify(err.message) + "));");
+ else
+ next(err);
+ }
+ else
+ {
+ var deps = [];
+ for(var i = 0; i < tmpl.dependencies.length; i++)
+ deps[i] = tmpl.dependencies[i].replace(path.sep, "/");
+ res.send("blade._cachedViews[" + viewName + "]=" + tmpl.toString() +
+ ";var e=blade._cb[" + viewName + "];e&&e(null," +
+ JSON.stringify(tmpl.reldir.replace(path.sep, "/") ) + "," +
+ JSON.stringify(deps) + "," + tmpl.unknownDependencies + ");");
+ }
});
}
else next();
View
@@ -254,19 +254,24 @@
}
//Function to be called if the template could not be loaded
function errorFunction(reason) {
- var cb = blade._cb[filename].cb; //array of callbacks
+ var callbacks = blade._cb[filename].cb; //array of callbacks
delete blade._cb[filename];
st.parentNode.removeChild(st);
- callCallbacks(cb, new Error("Blade Template [" + filename +
+ callCallbacks(callbacks, new Error("Blade Template [" + filename +
"] could not be loaded: " + (reason ? reason : "Request timed out") ) );
}
//Set a timer to return an Error after a timeout expires.
var timer = setTimeout(errorFunction, runtime.options.loadTimeout);
//Setup a callback to be called if the template is loaded successfully
- var tmp = blade._cb[filename] = function(dependenciesReldir, dependencies, unknownDependencies) {
+ var tmp = blade._cb[filename] = function(compileError, dependenciesReldir,
+ dependencies, unknownDependencies) {
+ //Clear timeouts and cleanup
clearTimeout(timer);
delete blade._cb[filename];
st.parentNode.removeChild(st);
+ //Check for compilation error
+ if(compileError)
+ return callCallbacks(tmp.cb, compileError);
//Load all dependencies, too
if(dependencies.length > 0)
{
View
@@ -15,7 +15,7 @@
"live binding",
"meteor"
],
- "version": "3.0.3",
+ "version": "3.1.0",
"homepage": "https://github.com/bminer/node-blade",
"repository": {
"type": "git",
View
@@ -308,6 +308,7 @@
return LiveUpdate.isolate(function () {
var ret;
tmpl(locals ? locals.observable || locals : {}, function(err, html, info) {
+ if(err) throw err;
//Remove event handler attributes
html = html.replace(/on[a-z]+\=\"return blade\.Runtime\.trigger\(this\,arguments\)\;\"/g, "");
//Return
View
@@ -3,7 +3,7 @@
"author": "Blake Miner <miner.blake@gmail.com> (http://www.blakeminer.com/)",
"description": "Blade - HTML Template Compiler, inspired by Jade & Haml",
"homepage": "https://github.com/bminer/node-blade",
- "version": "3.0.3",
+ "version": "3.1.0",
"git": "https://github.com/bminer/node-blade.git",
"packages": {
}

0 comments on commit 4fd513b

Please sign in to comment.