Permalink
Browse files

handle errors gracefully

  • Loading branch information...
1 parent 4ab5ce6 commit 70a431c4810ba2df5bd7f4ee2b4db82f1114e23c @netroy committed Nov 13, 2012
Showing with 34 additions and 25 deletions.
  1. +27 −23 lib/io-handler.js
  2. +7 −2 public/js/app.js
View
@@ -47,38 +47,42 @@
// Tell the client that they are watching a new file now
unwatch();
- // send some back log
- fs.stat(filePath, function(err, stats) {
+ try {
+ // send some back log
+ fs.stat(filePath, function(err, stats) {
- if (err) {
- throw err;
- }
+ if (err) {
+ throw err;
+ }
- if (stats.size === 0){
- socket.emit('clear');
- return;
- }
+ if (stats.size === 0){
+ socket.emit('clear');
+ return;
+ }
- var start = (stats.size > backlog_size) ? (stats.size - backlog_size) : 0;
- streamData(filePath, start, stats.size, socket);
+ var start = (stats.size > backlog_size) ? (stats.size - backlog_size) : 0;
+ streamData(filePath, start, stats.size, socket);
- // watch the file now
- fs.watchFile(filePath, function(curr, prev) {
+ // watch the file now
+ fs.watchFile(filePath, function(curr, prev) {
- if(prev.size > curr.size) {
- return;
- }
+ if(prev.size > curr.size) {
+ return;
+ }
- streamData(filePath, prev.size, curr.size, socket);
+ streamData(filePath, prev.size, curr.size, socket);
- // All well, mark this file as last one
- lastFilePath = filePath;
+ // All well, mark this file as last one
+ lastFilePath = filePath;
+ });
});
- });
-
- // stop watching the file
- socket.on('disconnect', unwatch);
+ } catch(err) {
+ socket.emit('error', err.message);
+ }
});
+
+ // stop watching the file
+ socket.on('disconnect', unwatch);
};
}
View
@@ -55,17 +55,20 @@ $(function() {
});
selector.bind('change',function(e){
+
var log = selector[0];
+
if(log.selectedIndex === 0){
- $('#info,#tail').empty();
+ $('#info, #tail').empty();
return;
}
+
+ buffer.empty();
socket.emit('request', log.options[log.selectedIndex].value);
});
});
socket.on('select', function(fileName) {
- buffer.empty();
notice.html('watching ' + fileName);
});
@@ -93,5 +96,7 @@ $(function() {
buffer.empty();
});
+ socket.on('err', log);
+
socket.on('message', log);
});

0 comments on commit 70a431c

Please sign in to comment.