From f53ca8d95821c21be6b0f059a7f71b33f98d84e3 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 14 Sep 2017 11:34:13 -0700 Subject: [PATCH] update error handler to convert props to keys --- helpers/errorHandlers.js | 15 +++++++++++++-- helpers/lbryApi.js | 2 +- models/index.js | 10 +++++----- package.json | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/helpers/errorHandlers.js b/helpers/errorHandlers.js index ae889dfa..d1ac23cc 100644 --- a/helpers/errorHandlers.js +++ b/helpers/errorHandlers.js @@ -1,9 +1,20 @@ const logger = require('winston'); const { postToStats } = require('../controllers/statsController.js'); +function useObjectPropertiesIfNoKeys (err) { + if (Object.keys(err).length === 0) { + let newErrorObject = {}; + Object.getOwnPropertyNames(err).forEach((key) => { + newErrorObject[key] = err[key]; + }); + return newErrorObject; + } + return err; +} + module.exports = { handleRequestError (action, originalUrl, ip, error, res) { - logger.error('Request Error:', error.message); + logger.error('Request Error:', useObjectPropertiesIfNoKeys(error)); postToStats(action, originalUrl, ip, null, null, error); if (error.response) { res.status(error.response.status).send(error.response.data.error.message); @@ -16,7 +27,7 @@ module.exports = { } }, handlePublishError (error) { - logger.error('Publish Error:', error.message); + logger.error('Publish Error:', useObjectPropertiesIfNoKeys(error)); if (error.code === 'ECONNREFUSED') { return 'Connection refused. The daemon may not be running.'; } else if (error.response.data.error) { diff --git a/helpers/lbryApi.js b/helpers/lbryApi.js index 6b7b3b6e..3777c593 100644 --- a/helpers/lbryApi.js +++ b/helpers/lbryApi.js @@ -115,7 +115,7 @@ module.exports = { } }) .catch(error => { - logger.error('Lbrynet Error:', error.message); + logger.error('Lbrynet Error:', error); resolve('/home/lbry/Downloads/'); }); }); diff --git a/models/index.js b/models/index.js index 96a4d288..e4e870c5 100644 --- a/models/index.js +++ b/models/index.js @@ -52,7 +52,7 @@ function getLongClaimIdFromShortClaimId (name, shortId) { .then(result => { switch (result.length) { case 0: - return reject(new Error('That is an invalid Short Claim Id')); + throw new Error('That is an invalid Short Claim Id'); default: // note results must be sorted return resolve(result[0].claimId); } @@ -179,7 +179,7 @@ db['getShortClaimIdFromLongClaimId'] = (claimId, claimName) => { .then(result => { switch (result.length) { case 0: - return reject(new Error('That is an invalid claim name')); + throw new Error('That is an invalid claim name'); default: return resolve(sortResult(result, claimId)); } @@ -198,7 +198,7 @@ db['getShortChannelIdFromLongChannelId'] = (channelName, longChannelId) => { .then(result => { switch (result.length) { case 0: - return reject(new Error('That is an invalid channel name')); + throw new Error('That is an invalid channel name'); default: return resolve(sortResult(result, longChannelId)); } @@ -238,7 +238,7 @@ db['resolveClaim'] = (name, claimId) => { case 1: return resolve(result[0]); default: - return new Error('more than one entry matches that name and claimID'); + throw new Error('more than one entry matches that name and claimID'); } }) .catch(error => { @@ -255,7 +255,7 @@ db['getClaimIdByLongChannelId'] = (channelId, claimName) => { .then(result => { switch (result.length) { case 0: - return reject(new Error('There is no such claim for that channel')); + throw new Error('There is no such claim for that channel'); default: return resolve(result[0].claimId); } diff --git a/package.json b/package.json index ba075575..22f9872b 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "socketio-file-upload": "^0.6.0", "universal-analytics": "^0.4.13", "winston": "^2.3.1", - "winston-slack-webhook": "^0.1.5" + "winston-slack-webhook": "billbitt/winston-slack-webhook" }, "devDependencies": { "eslint": "3.19.0",