Skip to content

Commit

Permalink
Add correct error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Rijs committed Jan 30, 2013
1 parent 80a01ed commit 42c7fdd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
18 changes: 13 additions & 5 deletions README.md
Expand Up @@ -5,12 +5,20 @@ Control a ClamAV daemon over TCP or Unix Domain Sockets.

var scanner = clam({port:6666}, null, function () {

this.version(function (v) {
console.log('Now connected to clamd: ' + v);
this.version(function (err, version) {
if (err) {
console.log(err);
} else {
console.log('Now connected to clamd: ' + version);
}
});

this.scan('~/joe/something.zip', function (result) {
console.log(result);
this.scan('~/joe/something.zip', function (err, isClean) {
if (err) {
console.log(err);
} else {
console.log('State of file: ' + (isClean ? 'clean' : 'infected'));
}
});

});
Expand All @@ -19,6 +27,6 @@ Control a ClamAV daemon over TCP or Unix Domain Sockets.
console.log('Scanner session closed' + (had_error ? ' with error.' : '.'));
});

scanner.on('error' function (err) {
scanner.on('error', function (err) {
console.log(err);
});
17 changes: 15 additions & 2 deletions clam.js
Expand Up @@ -81,11 +81,24 @@ module.exports = function (conn_opts, opts, continuation) {
};

self.version = function (cb) {
_raw('version', null, cb);
_raw('version', null, function (data) {
cb(null, data);
});
};

self.scan = function (path, cb) {
_raw('scan', path, cb);
_raw('scan', path, function (data) {
var match = data.match(/(OK|FOUND|ERROR)$/);
if (match === null) {
cb(new Error('Malformed response.' + data));
}
switch (match[1]) {
case 'OK': cb(null, true); break;
case 'FOUND': cb(null, false); break;
case 'ERROR': cb(new Error(data)); break;
default: cb(new Error('Malformed response.')); break;
}
});
};

return self;
Expand Down

0 comments on commit 42c7fdd

Please sign in to comment.