mopup
v1.0.1
Published
Exception handling functions for prototyping
Downloads
3
Readme
mopup
This is a set of functions that you can use in prototyping.
It uses the uncaughtException handler mechanism to handle purposely thrown exceptions
that include a function for "mopping up".
It's a sort of crude way to allow throwing exceptions from anywhere (including callbacks).
Yes, I know ...
Yes, I know all about the uncaughtException handler being disavowed by Felix, and how you can't guarantee that your program will be in any particular state, and how you should NEVER EVER EVER use it to recover from errors.
So ...
THIS IS NOT FOR PRODUCTION SOFTWARE. ONLY USE IT FOR PROTOTYPING AND PROOF-OF-CONCEPT WORK. DO NOT USE THIS FOR ANYTHING TRULY IMPORTANT.
Okay? Okay. Good.
Now then, here's how it works. You throw an exception from anywhere you want, as long as you include a function that will clean up after yourself. The function is attached to the Error object before it's thrown.
When the exception is caught by the uncaughtException handler (included), it's examined and if said callback is present, it calls that and then just returns without exiting the program.
If there is no handler attached, the program exits.
Functions included
mopup = require("mopup");
mopup.fail(message, cleanup_function);
mopup.fail_if(condition, cleanup_function);
mopup.fail_if(condition, message, cleanup_function);
mopup.die_if(condition)
mopup.die_if(condition, message)
If you want to use my other totally wrong-headed, fool-hearty module called "g", you can simplify things a bit:
require("g")("mopup");
fail(message, cleanup_function);
fail_if(condition, cleanup_function);
fail_if(condition, message, cleanup_function);
die_if(condition)
die_if(condition, message)
Example
require("g")("mopup");
function http_handler(req, res) {
var mop = function() {
res.end("fail");
}
req.headers.forEach(function(h) {
fail_if(h == "foo", "DANGER: FOO HEADER DETECTED", mop);
});
res.end("okay");
}
Disclaimer
SO THIS IS NOT FOR PRODUCTION SOFTWARE. ONLY USE IT FOR PROTOTYPING AND PROOF-OF-CONCEPTS WORK. DO NOT USE THIS FOR ANYTHING TRULY IMPORTANT.