updated error logging

This commit is contained in:
bill bittner 2017-06-27 22:41:48 -07:00
parent 434aa996d7
commit c26724bfd8
7 changed files with 50 additions and 62 deletions

View file

@ -1,31 +1,21 @@
const db = require('../../models'); const db = require('../../models');
const logger = require('winston'); const logger = require('winston');
function createAnalyticsRecord (action, url, ipAddress, result) {
logger.silly('creating record for analytics');
if (result && (typeof result !== 'string')) {
result = result.toString();
}
db.Analytics.create({
action,
url,
ipAddress,
result,
})
.then()
.catch(error => {
logger.error('sequelize error', error);
});
};
module.exports = { module.exports = {
postRequestAnalytics (url, ipAddress, result) { postToAnalytics: (action, url, ipAddress, result) => {
createAnalyticsRecord('request', url, ipAddress, result); logger.silly('creating record for analytics');
}, if (result && (typeof result !== 'string')) {
postPublishAnalytics (url, ipAddress, result) { result = result.toString();
createAnalyticsRecord('publish', url, ipAddress, result); }
}, db.Analytics.create({
postShowAnalytics (url, ipAddress, result) { action,
createAnalyticsRecord('show', url, ipAddress, result); url,
ipAddress,
result,
})
.then()
.catch(error => {
logger.error('sequelize error', error);
});
}, },
}; };

View file

@ -1,15 +1,20 @@
const logger = require('winston'); const logger = require('winston');
const postToAnalytics = require('../helpers/libraries/analytics');
module.exports = { module.exports = {
handleRequestError (error, res) { handleRequestError (action, originalUrl, ip, error, res) {
logger.error('Request Error >>', error); logger.error('Request Error >>', error);
if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') { if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') {
postToAnalytics(action, originalUrl, ip, 'success (no claims)');
res.status(307).render('noClaims'); res.status(307).render('noClaims');
} else if (error.response) { } else if (error.response) {
postToAnalytics(action, originalUrl, ip, 'error.response.data.error.messsage');
res.status(error.response.status).send(error.response.data.error.message); res.status(error.response.status).send(error.response.data.error.message);
} else if (error.code === 'ECONNREFUSED') { } else if (error.code === 'ECONNREFUSED') {
postToAnalytics(action, originalUrl, ip, 'Connection refused. The daemon may not be running.');
res.status(503).send('Connection refused. The daemon may not be running.'); res.status(503).send('Connection refused. The daemon may not be running.');
} else { } else {
postToAnalytics(action, originalUrl, ip, error);
res.status(400).send(JSON.stringify(error)); res.status(400).send(JSON.stringify(error));
} }
}, },

View file

@ -5,7 +5,7 @@ const publishController = require('../controllers/publishController.js');
const lbryApi = require('../helpers/libraries/lbryApi.js'); const lbryApi = require('../helpers/libraries/lbryApi.js');
const publishHelpers = require('../helpers/libraries/publishHelpers.js'); const publishHelpers = require('../helpers/libraries/publishHelpers.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { postRequestAnalytics, postPublishAnalytics } = require('../helpers/libraries/analytics'); const postToAnalytics = require('../helpers/libraries/analytics');
module.exports = app => { module.exports = app => {
// route to run a claim_list request on the daemon // route to run a claim_list request on the daemon
@ -14,12 +14,11 @@ module.exports = app => {
lbryApi lbryApi
.getClaimsList(params.claim) .getClaimsList(params.claim)
.then(claimsList => { .then(claimsList => {
postRequestAnalytics(originalUrl, ip, 'success'); postToAnalytics('publish', originalUrl, ip, 'success');
res.status(200).json(claimsList); res.status(200).json(claimsList);
}) })
.catch(error => { .catch(error => {
postRequestAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to run a resolve request on the daemon // route to run a resolve request on the daemon
@ -28,12 +27,11 @@ module.exports = app => {
lbryApi lbryApi
.resolveUri(params.uri) .resolveUri(params.uri)
.then(resolvedUri => { .then(resolvedUri => {
postRequestAnalytics(originalUrl, ip, 'success'); postToAnalytics('publish', originalUrl, ip, 'success');
res.status(200).json(resolvedUri); res.status(200).json(resolvedUri);
}) })
.catch(error => { .catch(error => {
postRequestAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to run a publish request on the daemon // route to run a publish request on the daemon
@ -42,7 +40,7 @@ module.exports = app => {
// validate that a file was provided // validate that a file was provided
const file = files.speech || files.null; const file = files.speech || files.null;
if (!file) { if (!file) {
postPublishAnalytics(originalUrl, ip, 'Error: file'); postToAnalytics('publish', originalUrl, ip, 'Error: file');
res.status(400).send('Error: No file was submitted or the key used was incorrect. Files posted through this route must use a key of "speech" or null'); res.status(400).send('Error: No file was submitted or the key used was incorrect. Files posted through this route must use a key of "speech" or null');
return; return;
} }
@ -50,14 +48,14 @@ module.exports = app => {
const name = body.name || file.name.substring(0, file.name.indexOf('.')); const name = body.name || file.name.substring(0, file.name.indexOf('.'));
const invalidCharacters = /[^\w,-]/.exec(name); const invalidCharacters = /[^\w,-]/.exec(name);
if (invalidCharacters) { if (invalidCharacters) {
postPublishAnalytics(originalUrl, ip, 'Error: name'); postToAnalytics('publish', originalUrl, ip, 'Error: name');
res.status(400).send('Error: The name you provided is not allowed. Please use A-Z, a-z, 0-9, "_" and "-" only.'); res.status(400).send('Error: The name you provided is not allowed. Please use A-Z, a-z, 0-9, "_" and "-" only.');
return; return;
} }
// validate license // validate license
const license = body.license || 'No License Provided'; const license = body.license || 'No License Provided';
if ((license.indexOf('Public Domain') === -1) && (license.indexOf('Creative Commons') === -1)) { if ((license.indexOf('Public Domain') === -1) && (license.indexOf('Creative Commons') === -1)) {
postPublishAnalytics(originalUrl, ip, 'Error: license'); postToAnalytics('puplish', originalUrl, ip, 'Error: license');
res.status(400).send('Error: Only posts with a license of "Public Domain" or "Creative Commons" are eligible for publishing through spee.ch'); res.status(400).send('Error: Only posts with a license of "Public Domain" or "Creative Commons" are eligible for publishing through spee.ch');
return; return;
} }
@ -75,7 +73,7 @@ module.exports = app => {
case '1': case '1':
break; break;
default: default:
postPublishAnalytics(originalUrl, ip, 'Error: nsfw'); postToAnalytics('publish', originalUrl, ip, 'Error: nsfw');
res.status(400).send('Error: NSFW value was not accepted. NSFW must be set to either true, false, "on", or "off"'); res.status(400).send('Error: NSFW value was not accepted. NSFW must be set to either true, false, "on", or "off"');
return; return;
} }
@ -91,12 +89,11 @@ module.exports = app => {
publishController publishController
.publish(publishParams, fileName, fileType) .publish(publishParams, fileName, fileType)
.then(result => { .then(result => {
postPublishAnalytics(originalUrl, ip, 'success'); postToAnalytics('publish', originalUrl, ip, 'success');
res.status(200).json(result); res.status(200).json(result);
}) })
.catch(error => { .catch(error => {
postPublishAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
}; };

View file

@ -1,5 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const { postShowAnalytics } = require('../helpers/libraries/analytics'); const postToAnalytics = require('../helpers/libraries/analytics');
module.exports = app => { module.exports = app => {
// route for the home page // route for the home page
@ -10,7 +10,7 @@ module.exports = app => {
// a catch-all route if someone visits a page that does not exist // a catch-all route if someone visits a page that does not exist
app.use('*', ({ originalUrl, ip }, res) => { app.use('*', ({ originalUrl, ip }, res) => {
logger.error(`Get request on ${originalUrl} from ${ip} which was a 404`); logger.error(`Get request on ${originalUrl} from ${ip} which was a 404`);
postShowAnalytics(originalUrl, ip, 'Error: 404'); postToAnalytics('post', originalUrl, ip, 'Error: 404');
res.status(404).render('fourOhFour'); res.status(404).render('fourOhFour');
}); });
}; };

View file

@ -1,7 +1,7 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const serveController = require('../controllers/serveController.js'); const serveController = require('../controllers/serveController.js');
const logger = require('winston'); const logger = require('winston');
const { postRequestAnalytics } = require('../helpers/libraries/analytics'); const postToAnalytics = require('../helpers/libraries/analytics');
function serveFile ({ fileName, fileType, filePath }, res) { function serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName}`); logger.info(`serving file ${fileName}`);
@ -38,14 +38,13 @@ module.exports = (app) => {
logger.debug(`GET request on ${originalUrl} from ${ip}`); logger.debug(`GET request on ${originalUrl} from ${ip}`);
// begin image-serve processes // begin image-serve processes
serveController serveController
.getClaimByClaimId(params.name, params.claim_id) .getClaimByClaimId('serve', params.name, params.claim_id)
.then(fileInfo => { .then(fileInfo => {
postRequestAnalytics(originalUrl, ip, 'success'); postToAnalytics(originalUrl, ip, 'success');
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
.catch(error => { .catch(error => {
postRequestAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to fetch one free public claim // route to fetch one free public claim
@ -55,12 +54,11 @@ module.exports = (app) => {
serveController serveController
.getClaimByName(params.name) .getClaimByName(params.name)
.then(fileInfo => { .then(fileInfo => {
postRequestAnalytics(originalUrl, ip, 'success'); postToAnalytics('serve', originalUrl, ip, 'success');
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
.catch(error => { .catch(error => {
postRequestAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
}; };

View file

@ -1,7 +1,7 @@
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const showController = require('../controllers/showController.js'); const showController = require('../controllers/showController.js');
const logger = require('winston'); const logger = require('winston');
const { postShowAnalytics } = require('../helpers/libraries/analytics'); const postToAnalytics = require('../helpers/libraries/analytics');
module.exports = (app) => { module.exports = (app) => {
// route to fetch all free public claims // route to fetch all free public claims
@ -11,12 +11,11 @@ module.exports = (app) => {
showController showController
.getAllClaims('meme-fodder') .getAllClaims('meme-fodder')
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {
postShowAnalytics(originalUrl, ip, 'success'); postToAnalytics('show', originalUrl, ip, 'success');
res.status(200).render('memeFodder', { claims: orderedFreePublicClaims }); res.status(200).render('memeFodder', { claims: orderedFreePublicClaims });
}) })
.catch(error => { .catch(error => {
postShowAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to fetch all free public claims // route to fetch all free public claims
@ -26,12 +25,11 @@ module.exports = (app) => {
showController showController
.getAllClaims(params.name) .getAllClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {
postShowAnalytics(originalUrl, ip, 'success'); postToAnalytics('show', originalUrl, ip, 'success');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
}) })
.catch(error => { .catch(error => {
postShowAnalytics(originalUrl, ip, error); errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
errorHandlers.handleRequestError(error, res);
}); });
}); });
}; };

View file

@ -2,7 +2,7 @@ const logger = require('winston');
const publishController = require('../controllers/publishController.js'); const publishController = require('../controllers/publishController.js');
const publishHelpers = require('../helpers/libraries/publishHelpers.js'); const publishHelpers = require('../helpers/libraries/publishHelpers.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { postPublishAnalytics } = require('../helpers/libraries/analytics'); const postToAnalytics = require('../helpers/libraries/analytics');
module.exports = (app, siofu, hostedContentPath) => { module.exports = (app, siofu, hostedContentPath) => {
const http = require('http').Server(app); const http = require('http').Server(app);
@ -25,7 +25,7 @@ module.exports = (app, siofu, hostedContentPath) => {
// listener for when file upload encounters an error // listener for when file upload encounters an error
uploader.on('error', ({ error }) => { uploader.on('error', ({ error }) => {
logger.error('an error occured while uploading', error); logger.error('an error occured while uploading', error);
postPublishAnalytics('spee.ch/', null, error); postToAnalytics('publish', '/', null, error);
socket.emit('publish-status', error); socket.emit('publish-status', error);
}); });
// listener for when file has been uploaded // listener for when file has been uploaded
@ -39,18 +39,18 @@ module.exports = (app, siofu, hostedContentPath) => {
publishController publishController
.publish(publishParams, file.name, file.meta.type) .publish(publishParams, file.name, file.meta.type)
.then(result => { .then(result => {
postPublishAnalytics('spee.ch/', null, 'success'); postToAnalytics('publish', '/', null, 'success');
socket.emit('publish-complete', { name: publishParams.name, result }); socket.emit('publish-complete', { name: publishParams.name, result });
}) })
.catch(error => { .catch(error => {
error = errorHandlers.handlePublishError(error); error = errorHandlers.handlePublishError(error);
postPublishAnalytics('spee.ch/', null, error); postToAnalytics('publish', '/', null, error);
socket.emit('publish-failure', error); socket.emit('publish-failure', error);
}); });
} else { } else {
logger.error(`An error occurred in uploading the client's file`); logger.error(`An error occurred in uploading the client's file`);
socket.emit('publish-failure', 'file uploaded, but with errors'); socket.emit('publish-failure', 'file uploaded, but with errors');
postPublishAnalytics('spee.ch/', null, 'file uploaded, but with errors'); postToAnalytics('publish', '/', null, 'file uploaded, but with errors');
// to-do: remove the file if not done automatically // to-do: remove the file if not done automatically
} }
}); });