updated error handling

This commit is contained in:
bill bittner 2017-11-02 11:50:05 -07:00
parent 8d14f022bd
commit f9daf0f8be
4 changed files with 49 additions and 40 deletions

View file

@ -67,7 +67,7 @@ module.exports = {
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim; resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
}) })
.catch(error => { .catch(error => {
logger.error('publishController.publish, error', error); logger.error('publishController.publish error', error);
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
reject(error); reject(error);
}); });

View file

@ -2,38 +2,53 @@ const logger = require('winston');
const { postToStats } = require('../controllers/statsController.js'); const { postToStats } = require('../controllers/statsController.js');
module.exports = { module.exports = {
handleRequestError (action, originalUrl, ip, error, res) { returnErrorMessageAndStatus: function (error) {
logger.error(`Request Error: ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error)); let status;
postToStats(action, originalUrl, ip, null, null, error); let message;
if (error.response) {
res.status(error.response.status).render('requestError', {message: error.response.data.error.message, status: error.response.status});
} else if (error.code === 'ECONNREFUSED') {
res.status(503).render('requestError', {message: 'Connection refused. The daemon may not be running.', status: 503});
} else if (error.message) {
res.status(500).render('requestError', {message: error.message, status: 500});
} else {
res.status(400).render('requestError', {message: error, status: 400});
}
},
handlePublishError (error) {
logger.error('Publish Error:', module.exports.useObjectPropertiesIfNoKeys(error));
if (error.code === 'ECONNREFUSED') { if (error.code === 'ECONNREFUSED') {
return 'Connection refused. The daemon may not be running.'; status = 503;
message = 'Connection refused. The daemon may not be running.';
} else if (error.response) { } else if (error.response) {
status = error.response.status || 500;
if (error.response.data) { if (error.response.data) {
if (error.response.data.message) { if (error.response.data.message) {
return error.response.data.message; message = error.response.data.message;
} else if (error.response.data.error) { } else if (error.response.data.error) {
return error.response.data.error.message; message = error.response.data.error.message;
}
return error.response.data;
}
return error.response;
} else { } else {
return error; message = error.response.data;
} }
} else {
message = error.response;
}
} else {
message = error;
}
return [status, message];
}, },
useObjectPropertiesIfNoKeys (err) { handleRequestError: function (action, originalUrl, ip, error, res) {
logger.error(`Request Error on ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
postToStats(action, originalUrl, ip, null, null, error);
const errorStatusAndMessage = this.returnErrorMessageAndStatus(error);
res
.status(errorStatusAndMessage[0])
.render('requestError', {
status : errorStatusAndMessage[0],
message: errorStatusAndMessage[1],
});
},
handleApiError: function (action, originalUrl, ip, error, res) {
logger.error(`Api ${action} Error on ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
postToStats(action, originalUrl, ip, null, null, error);
const errorStatusAndMessage = this.returnErrorMessageAndStatus(error);
res
.status(errorStatusAndMessage[0])
.json({
success: false,
message: errorStatusAndMessage[1],
});
},
useObjectPropertiesIfNoKeys: function (err) {
if (Object.keys(err).length === 0) { if (Object.keys(err).length === 0) {
let newErrorObject = {}; let newErrorObject = {};
Object.getOwnPropertyNames(err).forEach((key) => { Object.getOwnPropertyNames(err).forEach((key) => {

View file

@ -21,7 +21,7 @@ module.exports = (app) => {
res.status(200).json(claimsList); res.status(200).json(claimsList);
}) })
.catch(error => { .catch(error => {
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res); errorHandlers.handleApiError('claim_list', originalUrl, ip, error, res);
}); });
}); });
// route to check whether spee.ch has published to a claim // route to check whether spee.ch has published to a claim
@ -67,15 +67,12 @@ module.exports = (app) => {
res.status(200).json(resolvedUri); res.status(200).json(resolvedUri);
}) })
.catch(error => { .catch(error => {
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res); errorHandlers.handleApiError('resolve', originalUrl, ip, error, res);
}); });
}); });
// route to run a publish request on the daemon // route to run a publish request on the daemon
app.post('/api/publish', multipartMiddleware, (req, res) => { app.post('/api/publish', multipartMiddleware, ({ body, files, ip, originalUrl }, res) => {
logger.debug('req:', req.body, req.files);
// validate that mandatory parts of the request are present // validate that mandatory parts of the request are present
const body = req.body;
const files = req.files;
try { try {
validateApiPublishRequest(body, files); validateApiPublishRequest(body, files);
} catch (error) { } catch (error) {
@ -138,8 +135,7 @@ module.exports = (app) => {
}); });
}) })
.catch(error => { .catch(error => {
logger.error('publish api error', error); errorHandlers.handleApiError('publish', originalUrl, ip, error, res);
res.status(400).json({success: false, message: error.message});
}); });
}); });
@ -157,7 +153,7 @@ module.exports = (app) => {
}); });
}); });
// route to get a short channel id from long channel Id // route to get a short channel id from long channel Id
app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => { app.get('/api/shortChannelId/:longId/:name', ({ ip, originalUrl, params }, res) => {
// serve content // serve content
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name) db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => { .then(shortId => {
@ -166,7 +162,7 @@ module.exports = (app) => {
}) })
.catch(error => { .catch(error => {
logger.error('api error getting short channel id', error); logger.error('api error getting short channel id', error);
res.status(400).json(error.message); errorHandlers.handleApiError('short channel id', originalUrl, ip, error, res);
}); });
}); });
}; };

View file

@ -24,7 +24,7 @@ module.exports = (app) => {
app.get('/trending', (req, res) => { app.get('/trending', (req, res) => {
res.status(301).redirect('/popular'); res.status(301).redirect('/popular');
}); });
app.get('/popular', (req, res) => { app.get('/popular', ({ ip, originalUrl }, res) => {
const startDate = new Date(); const startDate = new Date();
startDate.setDate(startDate.getDate() - 1); startDate.setDate(startDate.getDate() - 1);
const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' '); const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' ');
@ -36,20 +36,18 @@ module.exports = (app) => {
}); });
}) })
.catch(error => { .catch(error => {
errorHandlers.handleRequestError(null, null, null, error, res); errorHandlers.handleRequestError('popular', originalUrl, ip, error, res);
}); });
}); });
// route to display a list of the trending images // route to display a list of the trending images
app.get('/new', (req, res) => { app.get('/new', ({ ip, originalUrl }, res) => {
getRecentClaims() getRecentClaims()
.then(result => { .then(result => {
// logger.debug(result); // logger.debug(result);
res.status(200).render('new', { res.status(200).render('new', { newClaims: result });
newClaims: result,
});
}) })
.catch(error => { .catch(error => {
errorHandlers.handleRequestError(null, null, null, error, res); errorHandlers.handleRequestError('new', originalUrl, ip, error, res);
}); });
}); });
// route to send embedable video player (for twitter) // route to send embedable video player (for twitter)