jan-swiecki / node-hotload

Hot load (hot require) for NodeJS. It's like JRebel but for NodeJS.
MIT License
19 stars 12 forks source link

Option to catch all un handled errors #2

Open majimboo opened 10 years ago

majimboo commented 10 years ago

Hot reloading is very useful in game server development. As I am developing a game server right now, I'm using node-hotload. And just replace all hotload to require on production. This way any changes I don't need to restart and go back to the entry of the client where the bug found is already somewhere far in the 3D world.

Would be great if you can also add an option to catch all unhandled errors, so if the newly reloaded module contains error, it still won't crash the application.

http://stackoverflow.com/questions/19909904/how-to-handle-all-exceptions-in-node-js

majimboo commented 10 years ago

Seems like this would be a better article for handling errors:

http://www.nodewiz.biz/nodejs-error-handling-pattern/

jan-swiecki commented 10 years ago

Thanks for the feedback! I'll look into this as soon as I can (daily jobs gets in the way:) )

majimboo commented 10 years ago

I really wish that I can use this with non-object modules. I need to reload my classes. Isn't a class an object? LOL :D anyways, this is a great project. Thank you for this. Someone I know is working on something similar but is using node.js vm module to do it, so it works even with objects or non-objects.

jan-swiecki commented 10 years ago

Glad you like it :)

Classes? You mean functions?:) Please provide example. I'm thinking how to reload "non-object" modules, but its tricky. For example string is immutable and if we wanted to reload it we would have to mutate it in-memory. I tried to reasearch this but everyone is saying "string is immutable, you cannot do it". I'm probably going to look inside nodejs and v8 source to achieve this.

Soon I will add functionality that lets you omit hotload function and just use require and hot reload will just work (with some configuration steps). I will not break API though so don't worry:).

(I write "non-objects" with quote because everything in javascript is an object. We can probably call them plain objects.)

majimboo commented 10 years ago

Example of my Class

 module exports = World = function(config) {
     this.config = config
 }

 World.prototype.turn = function()

Hotload doesn't allow this to be reloaded. :(

jan-swiecki commented 10 years ago

We have the same problem as with string here. I don't know if it is possible to achive this but I will definitely try to do this. In the meantime you can do this if you want (it's ugly, I know):

World = function(config) {
    this.config = config
}

module.exports = {
    class: World
}

And then use it like:

var WorldWrapper = hotload("./World.js");

var world = new WorldWrapper.World();
majimboo commented 10 years ago

Doing this, but it doesn't seem to reload when I change the file or rather it doesn't detect that I changed the file maybe that is why it is not reloading.

jan-swiecki commented 10 years ago

Even if you instantiate new World object after reload? Because it will not reload objects created with new before file change. Maybe you want to reload all prototype methods and fields inside existing object instances?

You can try to explain on code example (eg. you can show me your code but remove all unnecessary code, or write some simple example). I.e. file before + file after and what should be the behaviour of main program with hotload.

majimboo commented 10 years ago

Yes, that is right, the prototypes are the one I want, the instance doesn't matter as it remains the same.

jan-swiecki commented 10 years ago

Reloading prototypes should not be a problem and is indeed nice thing to have. I will try to add this functionality.

jan-swiecki commented 10 years ago

Fixed in 70c1ee1a908916b462d99ca5fc9921a36aeff8df. Can you verify? It just doesn't reload object at all when there is an error - it just notifies about it in the console. Do you need the functionality to add error callback handlers?

I've put prototype reloading problem into #3 issue.

majimboo commented 10 years ago

Actually I find that reloading the prototypes/class more useful. After a few research catching all unhandled errors can make the application go crazy. Thanks though, I can test tonight.