From 593d748bc0b39c836b8f8f16fd2eff76938ac99f Mon Sep 17 00:00:00 2001 From: Travis Eden Date: Thu, 4 Oct 2018 10:26:42 -0400 Subject: [PATCH] improve error handling; pass filePath to publish.js explicitly --- client/src/pages/HomePage/index.js | 7 ++----- client/src/sagas/publish.js | 7 ++++++- server/controllers/api/claim/publish/index.js | 2 +- .../controllers/api/claim/publish/publish.js | 18 +++++++++++++----- server/controllers/api/claim/update/index.js | 10 +++++++++- server/lbrynet/index.js | 9 ++++++++- server/models/utils/createFileRecordData.js | 2 +- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/client/src/pages/HomePage/index.js b/client/src/pages/HomePage/index.js index 5a00f4f1..cd32268b 100644 --- a/client/src/pages/HomePage/index.js +++ b/client/src/pages/HomePage/index.js @@ -2,17 +2,14 @@ import { connect } from 'react-redux'; import { onHandleShowHomepage } from '../../actions/show'; import View from './view'; -<<<<<<< HEAD -const mapStateToProps = ({ show, site, channel }) => { +const mapStateToProps = ({ show, site, channel, publish }) => { return { error : show.request.error, requestType: show.request.type, homeChannel: site.publishOnlyApproved && !channel.loggedInChannel.name ? `${site.approvedChannels[0].name}:${site.approvedChannels[0].longId}` : null, + isUpdate : publish.isUpdate, }; }; -======= -const mapStateToProps = props => ({ isUpdate: props.publish.isUpdate }); ->>>>>>> clear publish and edit pages based on publish.isUpdate const mapDispatchToProps = { onHandleShowHomepage, diff --git a/client/src/sagas/publish.js b/client/src/sagas/publish.js index 1a9bd46f..fbc45338 100644 --- a/client/src/sagas/publish.js +++ b/client/src/sagas/publish.js @@ -72,7 +72,12 @@ function * publishFile (action) { }, }); } - return history.push(`/${success.data.claimId}/${success.data.name}`); + if (success.data.claimId) { + return history.push(`/${success.data.claimId}/${success.data.name}`); + } else { + // this returns to the homepage, needs work + return yield put(updatePublishStatus(publishStates.FAILED, 'ERROR')); + } } if (loadStart) { yield put(updatePublishStatus(publishStates.LOAD_START, null)); diff --git a/server/controllers/api/claim/publish/index.js b/server/controllers/api/claim/publish/index.js index 530d03c8..a849cfa7 100644 --- a/server/controllers/api/claim/publish/index.js +++ b/server/controllers/api/claim/publish/index.js @@ -83,7 +83,7 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res) publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType); } // publish the asset - return publish(publishParams, fileName, fileType); + return publish(publishParams, fileName, fileType, filePath); }) .then(claimData => { logger.debug('Publish success >', claimData); diff --git a/server/controllers/api/claim/publish/publish.js b/server/controllers/api/claim/publish/publish.js index 298f07f4..7af9beae 100644 --- a/server/controllers/api/claim/publish/publish.js +++ b/server/controllers/api/claim/publish/publish.js @@ -5,11 +5,10 @@ const { createFileRecordDataAfterPublish } = require('../../../../models/utils/c const { createClaimRecordDataAfterPublish } = require('../../../../models/utils/createClaimRecordData.js'); const deleteFile = require('./deleteFile.js'); -const publish = async (publishParams, fileName, fileType) => { +const publish = async (publishParams, fileName, fileType, filePath) => { let publishResults; let channel; let fileRecord; - let filePath = publishParams.file_path; let newFile = Boolean(filePath); try { @@ -53,9 +52,18 @@ const publish = async (publishParams, fileName, fileType) => { return claimRecord; } catch (err) { - logger.error('PUBLISH ERROR', err); - await deleteFile(filePath); - return err; + // parse daemon response when err is a string + // this needs work + logger.info('publish/publish err:', err); + const error = typeof err === 'string' ? JSON.parse(err) : err; + if (filePath) { + await deleteFile(filePath); + } + const message = error.error && error.error.message ? error.error.message : 'Unknown publish error'; + return { + error: true, + message, + }; } }; diff --git a/server/controllers/api/claim/update/index.js b/server/controllers/api/claim/update/index.js index 2c1c5231..803a5611 100644 --- a/server/controllers/api/claim/update/index.js +++ b/server/controllers/api/claim/update/index.js @@ -123,9 +123,17 @@ const claimUpdate = ({ body, files, headers, ip, originalUrl, user, tor }, res) publishParams['thumbnail'] = `${details.host}/${channelName}:${channelId}/${name}-thumb.jpg`; } - return publish(publishParams, fileName, fileType); + const fp = files && files.file && files.file.path ? files.file.path : undefined; + return publish(publishParams, fileName, fileType, fp); }) .then(claimData => { + // this may need to happen in ../publish/index.js as well + if (claimData.error) { + res.status(400).json({ + success: false, + message: claimData.message, + }); + } const {claimId} = claimData; res.status(200).json({ success: true, diff --git a/server/lbrynet/index.js b/server/lbrynet/index.js index c77edc59..3b4eafcd 100644 --- a/server/lbrynet/index.js +++ b/server/lbrynet/index.js @@ -2,6 +2,7 @@ const axios = require('axios'); const logger = require('winston'); const { apiHost, apiPort, getTimeout } = require('@config/lbryConfig'); const lbrynetUri = 'http://' + apiHost + ':' + apiPort; +const db = require('../models'); const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('../utils/googleAnalytics.js'); const handleLbrynetResponse = require('./utils/handleLbrynetResponse.js'); const { publishing } = require('@config/siteConfig'); @@ -90,7 +91,13 @@ module.exports = { }) .then(({ data }) => { sendGATimingEvent('lbrynet', 'resolveUri', 'RESOLVE', gaStartTime, Date.now()); - if (data.result[uri].error) { // check for errors + if (Object.keys(data.result).length === 0 && data.result.constructor === Object) { + // workaround for daemon returning empty result object + // https://github.com/lbryio/lbry/issues/1485 + db.Claim.findOne({ where: { claimId: uri.split('#')[1] } }) + .then(() => reject('This claim has not yet been confirmed on the LBRY blockchain')) + .catch(() => reject(`Claim ${uri} does not exist`)); + } else if (data.result[uri].error) { // check for errors reject(data.result[uri].error); } else { // if no errors, resolve resolve(data.result[uri]); diff --git a/server/models/utils/createFileRecordData.js b/server/models/utils/createFileRecordData.js index aa0802cf..fdcb9ce0 100644 --- a/server/models/utils/createFileRecordData.js +++ b/server/models/utils/createFileRecordData.js @@ -28,7 +28,7 @@ async function createFileRecordDataAfterGet (resolveResult, getResult) { filePath, fileType, }; -}; +} async function createFileRecordDataAfterPublish (fileName, fileType, publishParams, publishResults) { const {