tags:

views:

94

answers:

3

Hey all,

Consider the following code:

var meryl = require('meryl'),
  merylex = require('meryl-extras'),
  staticfile = merylex('staticfile'),
  httpserv = require('http');

meryl.p('GET /static/<filepath>', staticfile({root: 'static', path: 'filepath'}));

httpserv.createServer(meryl.cgi()).listen(3000);

This works great, until you request a file that doesn't exist. If I request /static/non-existent-file, the server crashes and says:

/home/user/.node_libraries/meryl-extras/lib/plugins/staticfile.js:224
        chain();
        ^
TypeError: undefined is not a function
    at CALL_NON_FUNCTION (native)
    at /home/user/.node_libraries/meryl-extras/lib/plugins/staticfile.js:224:9
    at [object Object]. (fs:58:5)
    at [object Object].emit (events:27:15)
    at fs:655:12
    at node.js:608:9

How can I catch this, so my app doesn't crash? Unfortunately, being such a new language, there doesn't seem to be a whole lot of examples that I've been able to find.

Thanks.

A: 

I'm not sure but I think you can check that chain is not undefined before you move on.

if(chain){ //do your stuff } else { //crash }
Fopfong
This ended up just being a bug in meryl. :|
Sam
+1  A: 

Hi Sam, your code seems ok, but make sure you're using latest Meryl releases. Meryl is not stable yet and currently it is heavily under development. Updating Meryl will solve your issues.

Currently Meryl is v0.4.1 and Meryl Extras 0.1.4.

To update Meryl simply use npm

> npm install meryl
> npm install meryl-extras

Hope this helps.

Best.

coffeemate
A: 

Being JavaScript it seems like you should be able to do:

try {
// do stuff
} catch (e) {
// handle exceptions
}
indieinvader