Default error handler for node express - node.js

I've read the error handling guidance given for the express module, but would like to dig a little deeper and see exactly what the default error handler in express actually does (and hence what my custom error handler should and shouldn't do, if it can pass on some of the basic error handling procedure on to the default handler). It may just be showing my ignorance, but I can't for the life of me find where the default error handler is actually defined in the express source code. Please can someone help me out here.

Pretty sure the default handler just logs to stderr. Look at the logerror function and how it's used in the "finalhandler".
https://github.com/expressjs/express/blob/master/lib/application.js

Related

Deployed Node.js application to bluemix, error : cannot find concept-insights-devoxx.mybluemix.net/css/style.css

The style.css lives in public/css folder and config/express.js has
app.use(express.static(__dirname + '/../public'));
Still I am getting the following error
Please help , Thanks, Sandhya
I don't think your missing css is the problem. It would just mess up your styling on the page.
The TypeError above that seems more to the point. An object property "offsetTop" was referred to, but the object doesn't actually exist (or something that was supposed to create it, didn't return anything).
I'd guess that might have been what caused the 502 error, or perhaps the other way around (i.e. a proper response could not be received from the api call, and for some reason that case was not considered by whoever wrote the logic that comes after that).
Do you have the relevant code that you could post perhaps?
Thanks for the answer! The problem was in manifest.yml, where I was binding to a different instance of Concept Insights service. Therefore, the credentials given in app.js for the instance of Concept Insights populated with my corpus were getting overwritten.
Thanks for looking at this issue !
Sandhya

Globle error filter in node js

I am working on node from last 2-3 months on a project. Now I want to handle errors from a single point in node. For example : I have several api functions in my project. Many of them are taking _id as an api input. I need to parse this id using mongoose objectid before using in query. Now if the format of _id is not valid, it will throw the casting error. It could be handled by mongoose object isvalid property. But my purpose is that, at any place if it is not handled in code I want to catch the error and log it to my log file and send a common message like 'error occurs' to the UI. I want to add a common error handler for all the api that do the logging and error handling for my api, like we use .net MVC - error handler filer through the application.
I have tried using domain. But in domain.on('error',func(err){}); it is not working. I put my api functions call in domain.run();
If any body have any suggestion for me, please let me know.
Take a look at the domain module, if your app powered by express you can use the package - express-domain-middleware

How to handle Node js app errors to prevent crashing

I am new to node and what I would call, real server-side programming (vs PHP). I was setting up a user database with MongoDB, Mongoose and a simple mongoose user plugin that came with a schema and password stuff to use. You can add validation to Mongoose for your fields like so
schema.path('email').validate(function (email) {
if (this.skipValidation) return true
return email.trim().length
}, 'Please provide a valid email')
(this is not my code). I noticed though when I passed an invalid or blank email, .trim() failed and the entire server crashed. This is very worrisome to me because things like this don't happen in your good ol' WAMP stack. If you have a bug, 99.9% of the time it's just the browser that is affected.
Now that I am delving into lower level programming, do I have to be paranoid about every incoming variable to a simple function? Is there a tried-and-true error system I should follow?
Just check before using the variable with trim, if it is !null for example:
if(!email) {
return false;
}
And if you want to run your app forever, rather use PM2.
If you are interested in running forever, read this interesting post http://devo.ps/blog/goodbye-node-forever-hello-pm2/
You may consider using forever to keep your node.js program running. Even it crashes, it restarts automatically and the error is logged as well.
Note: Although you could actually catch all exceptions to prevent the node.js from crashing, it is not recommended.
One of our strategies is to make use of Node.js Domain to handle errors - http://nodejs.org/api/domain.html
You should set up a error logging node modules like Winston, once configured produces useful error/exceptions.
Have a look in this answer for how to catch error within your node implementation, though specific to expressjs but relevant.
Once you catch exceptions, it prevents unexpected crashes.

RPC JSONP error object conventions

Im building a php json(p) rpc server and currently i am stuck with not finding sufficient information on how to handle/respond if a jsonp call throws an error inside the called function/method. With json rpc 2.0 its well documented - with jsonp its not. how to return errors from the application level of the service called?
Suppose a call like /service.php?var=test&callback=jsonpCallback is requested. in a positive case the result would be something like jsonpCallback({"result": "test"}). Suppose on the application level an exception is thrown? what do i return and how?
Do i return an error object inside the callback? or just a json (custom) error object as string? If there is something like an jsonp error object convention where to find it?
Does make sense to allow for a custom error callback in the GET request which can be called like: /service.php?var=test&callback=jsonpCallback&errorCallback=jsonpErrorCallback
Any advice highly appreciated.

How to debug a '4005 Path Not Found' error?

I am trying to integrate Pusher with my web application that uses backbone.js. I'm following the Pusher with Backbone guide in the Pusher docs.
So I'm seeing this error pop up in the web console on application start up
Pusher : Error : {"type":"PusherError","data":{"code":4005,"message":"Path not found"}}
What is this 'path' that cannot be found? I found nothing in the Pusher docs.
A 4005 error generally means that the WebSocket URL you are using doesn't identify an application to connect to.
In terms of usage of the Pusher JavaScript library this means you've likely supplied an empty string as the app_key to the Pusher constructor.
var pusher = new Pusher('');
You can see this in action here:
http://jsbin.com/evulaj/1/edit
Open the JavaScript console to see the error. You can also check the app_key value set by checking pusher.key.
Note: I appreciate this error is a bit cryptic. I'll see if we can remedy this
in my case, wsHost: process.env.MIX_PUSHER_WS_HOST was giving an empty value, and fixed it by changing .env file.
PUSHER_WS_HOST=my.host.com
MIX_PUSHER_WS_HOST="${PUSHER_WS_HOST}"
or you can do:
wsHost: window.location.hostname

Resources