Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make server not crash when there's a syntax error in server.js #44

Open
michael opened this issue Jun 21, 2015 · 13 comments
Open

Make server not crash when there's a syntax error in server.js #44

michael opened this issue Jun 21, 2015 · 13 comments
Assignees

Comments

@michael
Copy link
Member

michael commented Jun 21, 2015

See tahi-journal.

Seems this is not enough error handling:

    browserify({ debug: true, cache: false })
      .add(path.join(__dirname, "client", "app.js"))
      .bundle()
      .pipe(res)
      .on('error', function(req, res, next) {
        res.status(500).send("Internal error.");
      });

I tried adding a try-catch but still i get:

http://127.0.0.1:5000/

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Parsing file /Users/michael/projects/plos/tahi-journal/client/writer/components/content_toolbar.js: Unexpected token (86:4)
    at Deps.parseDeps (/Users/michael/projects/plos/tahi-journal/node_modules/browserify/node_modu
@Integral
Copy link
Member

@michael, did you mean syntax error inside browserify bundle?

@michael
Copy link
Member Author

michael commented Jun 21, 2015

Exactly. Server crashes then.

@Integral
Copy link
Member

OK, but how do you then know that it fails? Via terminal message?

On Sun, Jun 21, 2015, 15:52 Michael Aufreiter notifications@github.com
wrote:

Exactly. Server crashes then.


Reply to this email directly or view it on GitHub
#44 (comment).

@michael
Copy link
Member Author

michael commented Jun 21, 2015

Ideally it outputs the error on the screen too, not only console.

@Integral
Copy link
Member

@michael, why did you drop out browserify middleware? It is not crushes the server and you can use it like this:
app.get('/app.js', browserify(__dirname + '/client/app.js'));

@michael
Copy link
Member Author

michael commented Jun 21, 2015

We wanted less overhead, because we don't use any smart caching and fole watching, that browserify-middleware implements. Oliver just made the change. It's not so tragic when the server crashes for now, but if you have an idea how to fix it, please try and push it if its working.

@Integral
Copy link
Member

ok, you can do it like this https://github.com/substance/tahi-journal/commit/ce3da633127ceaa8cbf6e4ddc008dfc16cfa1de5
also you can get rid of try/catch and use just res.end()
it will print error in terminal console and in screen if you are trying to view js file, but not for template (because browserify works for js files), but perhaps you can write some script to send it to browser console

@Integral
Copy link
Member

and of course it will not crash your server ;)

@michael
Copy link
Member Author

michael commented Jun 21, 2015

bravo daniel! :)

seems that order of the handler did matter. Do we really need the try catch block inside the error handler?

@Integral
Copy link
Member

yes, it did! because you need to attach error listner to bundle function ;)
we need to do try/catch either res.end() to close connection, otherwise express will crash server because of with few errors you will try to use res few times while you are already send response.
also when you are sending error you will somehow see it in browser (red message in console), but with res.end i'm not sure...

@michael
Copy link
Member Author

michael commented Jun 21, 2015

I did this now. So it prints the error in the console in the browser also.

@michael
Copy link
Member Author

michael commented Jun 21, 2015

@Integral
Copy link
Member

and now try http://localhost:5000/1
you will get express error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants