From 7b10703821e5f82c57ff1f6d49881e8c83b9f505 Mon Sep 17 00:00:00 2001 From: "@wwwjim" Date: Tue, 22 Sep 2020 00:01:48 -0700 Subject: [PATCH] buckets: elegant failure modes for bucket getOrCreate failures --- node_common/managers/viewer.js | 4 ++++ node_common/upload-fs.js | 38 +++++++++++++++++++------------ node_common/upload.js | 32 ++++++++++++++++++++------ node_common/utilities.js | 4 +++- pages/api/data/archive.js | 7 ++++++ pages/api/data/get.js | 6 +++++ pages/api/data/remove-multiple.js | 7 ++++++ pages/api/data/remove.js | 7 ++++++ pages/api/users/create.js | 6 +++++ 9 files changed, 89 insertions(+), 22 deletions(-) diff --git a/node_common/managers/viewer.js b/node_common/managers/viewer.js index 825daafb..7162fe51 100644 --- a/node_common/managers/viewer.js +++ b/node_common/managers/viewer.js @@ -122,6 +122,10 @@ export const getTextileById = async ({ id }) => { bucketRoot, } = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api, user); + if (!buckets) { + return null; + } + const { power, powerInfo, diff --git a/node_common/upload-fs.js b/node_common/upload-fs.js index 9f2f1426..78ff82a4 100644 --- a/node_common/upload-fs.js +++ b/node_common/upload-fs.js @@ -54,15 +54,20 @@ export const formMultipart = (req, res, { user }) => const data = LibraryManager.createLocalDataIncomplete(target); // TODO(jim): Put this call into a file for all Textile related calls. + let { buckets, bucketKey } = await Utilities.getBucketAPIFromUserToken( + user.data.tokens.api, + user + ); + + if (!buckets) { + return reject({ + decorator: "SERVER_BUCKETS_INIT_ISSUE", + error: true, + }); + } + let push; try { - const { - buckets, - bucketKey, - } = await Utilities.getBucketAPIFromUserToken( - user.data.tokens.api, - user - ); push = await buckets.pushPath(bucketKey, data.name, readStream); } catch (e) { await FS.unlinkSync(tempPath); @@ -77,18 +82,23 @@ export const formMultipart = (req, res, { user }) => // Delete temporary local file, await FS.unlinkSync(tempPath); + let { buckets, bucketKey } = await Utilities.getBucketAPIFromUserToken( + user.data.tokens.api, + user + ); + + if (!buckets) { + return reject({ + decorator: "SERVER_BUCKETS_INIT_ISSUE", + error: true, + }); + } + // NOTE(jim) // Get remote file size from bucket. // TODO(jim): Put this call into a file for all Textile related calls. let ipfs = push.path.path; try { - const { - buckets, - bucketKey, - } = await Utilities.getBucketAPIFromUserToken( - user.data.tokens.api, - user - ); const newUpload = await buckets.listIpfsPath(ipfs); data.size = newUpload.size; } catch (e) { diff --git a/node_common/upload.js b/node_common/upload.js index c5aea3cb..a45ee97c 100644 --- a/node_common/upload.js +++ b/node_common/upload.js @@ -28,14 +28,24 @@ export const formMultipart = async (req, res, { user }) => { type: mime, }); + const token = user.data.tokens.api; + const { + buckets, + bucketKey, + } = await Utilities.getBucketAPIFromUserToken(token, user); + + if (!buckets) { + return reject({ + decorator: "SERVER_BUCKET_INIT_FAILURE", + error: true, + }); + } + let push; try { - const token = user.data.tokens.api; - const { - buckets, - bucketKey, - } = await Utilities.getBucketAPIFromUserToken(token, user); + console.log("[upload] pushing to textile"); push = await buckets.pushPath(bucketKey, data.id, stream); + console.log("[upload] finished pushing to textile"); } catch (e) { Social.sendTextileSlackMessage({ file: "/node_common/upload.js", @@ -84,9 +94,17 @@ export const formMultipart = async (req, res, { user }) => { } // TODO(jim): Put this call into a file for all Textile related calls. + const token = user.data.tokens.api; + const { buckets } = await Utilities.getBucketAPIFromUserToken(token, user); + + if (!buckets) { + return { + decorator: "SERVER_BUCKET_INIT_FAILURE", + error: true, + }; + } + try { - const token = user.data.tokens.api; - const { buckets } = await Utilities.getBucketAPIFromUserToken(token, user); const newUpload = await buckets.listIpfsPath(response.data); data.size = newUpload.size; } catch (e) { diff --git a/node_common/utilities.js b/node_common/utilities.js index 54a8e72b..31ea532f 100644 --- a/node_common/utilities.js +++ b/node_common/utilities.js @@ -130,6 +130,7 @@ export const getBucketAPIFromUserToken = async (token, user) => { // TODO(jim): Put this call into a file for all Textile related calls. let target; + console.log(`[buckets] getOrCreate`); try { target = await buckets.getOrCreate(BUCKET_NAME); } catch (e) { @@ -141,8 +142,9 @@ export const getBucketAPIFromUserToken = async (token, user) => { functionName: `buckets.getOrCreate`, }); - return null; + return { buckets: null, bucketKey: null, bucketRoot: null }; } + console.log(`[buckets] getOrCreate succes!`); return { buckets, diff --git a/pages/api/data/archive.js b/pages/api/data/archive.js index 726b48f6..475d2ca4 100644 --- a/pages/api/data/archive.js +++ b/pages/api/data/archive.js @@ -35,6 +35,13 @@ export default async (req, res) => { bucketRoot, } = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api, user); + if (!buckets) { + return res.status(500).send({ + decorator: "SERVER_BUCKET_INIT_FAILURE", + error: true, + }); + } + // bucketRoot.root.key // bucketRoot.root.path diff --git a/pages/api/data/get.js b/pages/api/data/get.js index 31704061..3aee745e 100644 --- a/pages/api/data/get.js +++ b/pages/api/data/get.js @@ -26,6 +26,12 @@ export default async (req, res) => { bucketName, } = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api, user); + if (!buckets) { + return res + .status(500) + .send({ decorator: "SERVER_GET_BUCKET_INIT", error: true }); + } + // TODO(jim): Put this call into a file for all Textile related calls. let r = null; try { diff --git a/pages/api/data/remove-multiple.js b/pages/api/data/remove-multiple.js index 4b417e76..744f9f94 100644 --- a/pages/api/data/remove-multiple.js +++ b/pages/api/data/remove-multiple.js @@ -54,6 +54,13 @@ export default async (req, res) => { bucketName, } = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api, user); + if (!buckets) { + return res.status(500).send({ + decorator: "SERVER_BUCKET_INIT_FAILURE", + error: true, + }); + } + // TODO(jim): Put this call into a file for all Textile related calls. let r = null; try { diff --git a/pages/api/data/remove.js b/pages/api/data/remove.js index e2935cb1..7e246ab5 100644 --- a/pages/api/data/remove.js +++ b/pages/api/data/remove.js @@ -51,6 +51,13 @@ export default async (req, res) => { bucketName, } = await Utilities.getBucketAPIFromUserToken(user.data.tokens.api, user); + if (!buckets) { + return res.status(500).send({ + decorator: "SERVER_BUCKET_INIT_FAILURE", + error: true, + }); + } + // TODO(jim): Put this call into a file for all Textile related calls. let r = null; try { diff --git a/pages/api/users/create.js b/pages/api/users/create.js index 807c2efa..54008e54 100644 --- a/pages/api/users/create.js +++ b/pages/api/users/create.js @@ -54,6 +54,12 @@ export default async (req, res) => { username: newUsername, }); + if (!buckets) { + return res + .status(500) + .send({ decorator: "SERVER_BUCKET_INIT_FAILURE", error: true }); + } + const user = await Data.createUser({ password: hash, salt,