add error handler for resources

This commit is contained in:
oleg 2015-07-05 20:18:51 +03:00
parent 6d664213c5
commit fec6633a7a
5 changed files with 23 additions and 21 deletions

View File

@ -9,7 +9,7 @@ catchRev:
notify: notify:
on: on:
- done # - done
# - error # - error
- change - change
to: to:

View File

@ -7,7 +7,7 @@ var Steppy = require('twostep').Steppy,
module.exports = function(app) { module.exports = function(app) {
var resource = app.dataio.resource('builds'); var resource = app.dataio.resource('builds');
resource.use('readAll', function(req, res) { resource.use('readAll', function(req, res, next) {
Steppy( Steppy(
function() { function() {
var findParams = _(req.data).pick('offset', 'limit'); var findParams = _(req.data).pick('offset', 'limit');
@ -19,13 +19,11 @@ module.exports = function(app) {
function(err, builds) { function(err, builds) {
res.send(builds); res.send(builds);
}, },
function(err) { next
console.log(err.stack || err)
}
); );
}); });
resource.use('read', function(req, res) { resource.use('read', function(req, res, next) {
Steppy( Steppy(
function() { function() {
var findParams = {}; var findParams = {};
@ -35,9 +33,7 @@ module.exports = function(app) {
function(err, build) { function(err, build) {
res.send(build[0]); res.send(build[0]);
}, },
function(err) { next
console.log(err.stack || err)
}
); );
}); });

View File

@ -0,0 +1,9 @@
'use strict';
module.exports = function(err, req, res, next) {
console.error(
'Error is occurred during requesting ' +
req.resource.namespace.name + ' ' + req.action + ':',
err.stack || err
);
};

View File

@ -1,9 +1,11 @@
'use strict'; 'use strict';
var _ = require('underscore'); var _ = require('underscore'),
errorHandler = require('./errorHandler');
module.exports = function(app) { module.exports = function(app) {
_(['builds', 'projects']).each(function(resource) { _(['builds', 'projects']).each(function(resource) {
require('./' + resource)(app); var resource = require('./' + resource)(app);
resource.use(errorHandler);
}); });
}; };

View File

@ -6,7 +6,8 @@ var Steppy = require('twostep').Steppy,
Distributor = require('../lib/distributor').Distributor, Distributor = require('../lib/distributor').Distributor,
db = require('../db'), db = require('../db'),
path = require('path'), path = require('path'),
fs = require('fs'); fs = require('fs'),
utils = require('../lib/utils');
module.exports = function(app) { module.exports = function(app) {
@ -58,9 +59,7 @@ module.exports = function(app) {
} }
}) })
.on('end', callback) .on('end', callback)
.on('error', function(err) { .on('error', utils.logErrorCallback);
console.log(err.stack || err);
});
}); });
buildDataResourcesHash[build.id] = buildDataResource; buildDataResourcesHash[build.id] = buildDataResource;
}; };
@ -97,9 +96,7 @@ module.exports = function(app) {
writeStreamsHash[filePath] = fs.createWriteStream( writeStreamsHash[filePath] = fs.createWriteStream(
getBuildLogPath(build.id), {encoding: 'utf8'} getBuildLogPath(build.id), {encoding: 'utf8'}
); );
writeStreamsHash[filePath].on('error', function(err) { writeStreamsHash[filePath].on('error', utils.logErrorCallback);
console.error(err.stack || err);
});
} }
// TODO: close ended files // TODO: close ended files
writeStreamsHash[filePath].write(data); writeStreamsHash[filePath].write(data);
@ -117,9 +114,7 @@ module.exports = function(app) {
distributor.run({ distributor.run({
projectName: projectName, projectName: projectName,
initiator: {type: 'user'} initiator: {type: 'user'}
}, function(err, build) { }, utils.logErrorCallback);
console.log('>>> err, build = ', err && err.stack || err, build);
});
res.send(); res.send();
}); });