Commit 3d3ed940 authored by Leo Iannacone's avatar Leo Iannacone

handling errors and carry out via socket

parent 9f66c106
...@@ -12,7 +12,10 @@ function __watch_build_status (socket, status) { ...@@ -12,7 +12,10 @@ function __watch_build_status (socket, status) {
var data = null var data = null
try { try {
data = JSON.parse(new_content) data = JSON.parse(new_content)
} catch (error) { return } } catch (err) {
utils.erros_handler('Broadcaster:__watch_build_status:JSON.parse(new_content) - ', err, socket)
return
}
if (data.status == config.status.package.building) { if (data.status == config.status.package.building) {
status.packages.push(data) status.packages.push(data)
} }
......
...@@ -3,6 +3,8 @@ var fs = require('fs') ...@@ -3,6 +3,8 @@ var fs = require('fs')
, config = require('./config.js') , config = require('./config.js')
, utils = require('./utils.js') , utils = require('./utils.js')
var _e = config.events.client
function __get_files_list_from_package(data, callback) { function __get_files_list_from_package(data, callback) {
package_path = utils.get_package_path(data) package_path = utils.get_package_path(data)
utils.get_files_list(package_path, false, function(files) { utils.get_files_list(package_path, false, function(files) {
...@@ -108,9 +110,12 @@ function __send_distribution_packages (event_name, socket, data) { ...@@ -108,9 +110,12 @@ function __send_distribution_packages (event_name, socket, data) {
} }
function __send_file (event_name, socket, data) { function __send_file (event_name, socket, data) {
file_path = utils.get_file_path(data) var file_path = utils.get_file_path(data)
fs.readFile(file_path, 'utf8', function (err, content) { fs.readFile(file_path, 'utf8', function (err, content) {
if (err) return; if (err) {
utils.errors_handler('client:__send_file', err, socket)
return
}
data.file.orig_name = file_path.split('/').pop() data.file.orig_name = file_path.split('/').pop()
data.file.content = content data.file.content = content
data.file.path = file_path.replace(config.debomatic.path, config.routes.debomatic) data.file.path = file_path.replace(config.debomatic.path, config.routes.debomatic)
...@@ -119,8 +124,7 @@ function __send_file (event_name, socket, data) { ...@@ -119,8 +124,7 @@ function __send_file (event_name, socket, data) {
} }
function __handler_get_file (socket, data) { function __handler_get_file (socket, data) {
var _e = config.events.client var file_path = utils.get_file_path(data)
file_path = utils.get_file_path(data)
utils.watch_path_onsocket(_e.file_newcontent, socket, data, file_path, function(event_name, socket, data) { utils.watch_path_onsocket(_e.file_newcontent, socket, data, file_path, function(event_name, socket, data) {
data.file.content = null data.file.content = null
socket.emit(event_name, data) socket.emit(event_name, data)
...@@ -132,8 +136,6 @@ function Client (socket) { ...@@ -132,8 +136,6 @@ function Client (socket) {
var socket = socket var socket = socket
var _e = config.events.client
this.start = function () { this.start = function () {
// init send distributions and status // init send distributions and status
utils.get_files_list(config.debomatic.path, true, function(distros) { utils.get_files_list(config.debomatic.path, true, function(distros) {
......
...@@ -3,6 +3,14 @@ var path = require('path') ...@@ -3,6 +3,14 @@ var path = require('path')
, Tail = require('tail').Tail , Tail = require('tail').Tail
, config = require('./config.js') , config = require('./config.js')
function __errors_handler(from, err, socket) {
if (! socket)
from = "NO SOCKET: " + from
console.error(from, err)
if (socket)
socket.emit(config.events.error, err.message)
}
function __check_no_backward(backward_path) { function __check_no_backward(backward_path) {
try { try {
return backward_path.indexOf('..') < 0 return backward_path.indexOf('..') < 0
...@@ -37,7 +45,7 @@ function __get_files_list(dir, onlyDirectories, callback) { ...@@ -37,7 +45,7 @@ function __get_files_list(dir, onlyDirectories, callback) {
fs.readdir(dir, function(err, files){ fs.readdir(dir, function(err, files){
result = []; result = [];
if (err) { if (err) {
console.error(err); __errors_handler("__get_files_list", err)
return; return;
} }
files.forEach( function(f) { files.forEach( function(f) {
...@@ -54,7 +62,10 @@ function __get_files_list(dir, onlyDirectories, callback) { ...@@ -54,7 +62,10 @@ function __get_files_list(dir, onlyDirectories, callback) {
result.push(f); result.push(f);
} }
} }
} catch (fs_error) {} } catch (fs_error) {
__errors_handler("__get_files_list:forEach", fs_err)
return
}
}); });
callback(result); callback(result);
}); });
...@@ -63,17 +74,19 @@ function __get_files_list(dir, onlyDirectories, callback) { ...@@ -63,17 +74,19 @@ function __get_files_list(dir, onlyDirectories, callback) {
function __watch_path_onsocket(event_name, socket, data, watch_path, updater) { function __watch_path_onsocket(event_name, socket, data, watch_path, updater) {
name = "watcher-" + event_name name = "watcher-" + event_name
socket.get(name, function (err, watcher) { socket.get(name, function (err, watcher) {
if (watcher) {
try {
watcher.unwatch()
} catch (errorWatchingDirectory) {
watcher.close()
}
}
try { try {
if (watcher) {
try {
watcher.unwatch()
} catch (errorWatchingDirectory) {
watcher.close()
}
}
fs.stat(watch_path, function(err, stats) { fs.stat(watch_path, function(err, stats) {
if (err) if (err) {
utils.errors_handler("__watch_path_onsocket:fs.stat", err, socket)
return return
}
if (stats.isDirectory()) { if (stats.isDirectory()) {
watcher = fs.watch(watch_path, {persistent: true}, function (event, fileName) { watcher = fs.watch(watch_path, {persistent: true}, function (event, fileName) {
if(event == 'rename') if(event == 'rename')
...@@ -85,12 +98,20 @@ function __watch_path_onsocket(event_name, socket, data, watch_path, updater) { ...@@ -85,12 +98,20 @@ function __watch_path_onsocket(event_name, socket, data, watch_path, updater) {
watcher.on('line', function(new_content) { watcher.on('line', function(new_content) {
data.file.new_content = new_content + '\n' data.file.new_content = new_content + '\n'
updater(event_name, socket, data) updater(event_name, socket, data)
}).on('error', function(err) {
watcher.unwatch()
__errors_handler("__watch_path_onsocket.Tail <- " + arguments.callee.caller.name, err, socket)
return
}) })
}
socket.set(name, watcher) socket.set(name, watcher)
}
}) })
} catch (err_watch) { console.error('utils.__watch_path_onsocket ' + err_watch)} } catch (err) {
__errors_handler("__watch_path_onsocket <- " + arguments.callee.caller.name, err, socket)
return
}
}) })
return true;
} }
function __generic_handler_watcher(event_name, socket, data, watch_path, callback) { function __generic_handler_watcher(event_name, socket, data, watch_path, callback) {
...@@ -126,6 +147,9 @@ utils = { ...@@ -126,6 +147,9 @@ utils = {
generic_handler_watcher: function(event_name, socket, data, watch_path, callback) { generic_handler_watcher: function(event_name, socket, data, watch_path, callback) {
return __generic_handler_watcher(event_name, socket, data, watch_path, callback); return __generic_handler_watcher(event_name, socket, data, watch_path, callback);
}, },
errors_handler: function(from, error, socket) {
return __errors_handler(from, error, socket)
}
} }
module.exports = utils module.exports = utils
...@@ -98,8 +98,6 @@ function Page_Generic() ...@@ -98,8 +98,6 @@ function Page_Generic()
distributions.set(socket_distributions) distributions.set(socket_distributions)
}); });
socket.on('error', function(socket_data_error) { console.error(socket_data_error) });
socket.on(_e.client.status, function(packages_status) { socket.on(_e.client.status, function(packages_status) {
status.set(packages_status) status.set(packages_status)
}) })
...@@ -107,5 +105,9 @@ function Page_Generic() ...@@ -107,5 +105,9 @@ function Page_Generic()
socket.on(_e.broadcast.status_update, function(package_status) { socket.on(_e.broadcast.status_update, function(package_status) {
status.update(package_status) status.update(package_status)
}) })
socket.on(_e.error, function(error) {
console.error(error)
})
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment