From 3615a6e87356a162a0ef45cce9dae3a9fa12e028 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Fri, 22 Nov 2024 04:56:19 +0200 Subject: [PATCH] fix publish --- src/background.ts | 27 +++++++++++++++++++++++++++ src/qortalRequests/get.ts | 18 +++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/background.ts b/src/background.ts index c8f33b0..b634fae 100644 --- a/src/background.ts +++ b/src/background.ts @@ -938,6 +938,33 @@ export async function getLTCBalance() { } else throw new Error("Onable to get LTC balance"); } +export async function parseErrorResponse(response, defaultMessage = "Request failed") { + let message = defaultMessage; + + try { + // Attempt to parse JSON + const json = await response.json(); + if (json?.message) { + message = json.message; + } else { + // If JSON exists but no `message` field, include full JSON string + message = JSON.stringify(json); + } + } catch (jsonError) { + try { + // Fallback to plain text + const text = await response.text(); + message = text || response.statusText || message; + } catch (textError) { + // Fallback to statusText or defaultMessage + message = response.statusText || message; + } + } + + return message; +} + + const processTransactionVersion2Chat = async (body: any, customApi) => { // const validApi = await findUsableApi(); const url = await createEndpoint( diff --git a/src/qortalRequests/get.ts b/src/qortalRequests/get.ts index 0ac2cac..766c548 100644 --- a/src/qortalRequests/get.ts +++ b/src/qortalRequests/get.ts @@ -15,6 +15,7 @@ import { isUsingLocal, createBuyOrderTx, performPowTask, + parseErrorResponse, } from "../background"; import { getNameInfo } from "../backgroundFunctions/encryption"; import { showSaveFilePicker } from "../components/Apps/useQortalMessageListener"; @@ -634,6 +635,9 @@ export const publishQDNResource = async ( if (data.identifier == null) { identifier = "default"; } + if (data.fileId) { + data64 = await getFileFromContentScript(data.fileId); + } if ( data.encrypt && (!data.publicKeys || @@ -644,9 +648,7 @@ export const publishQDNResource = async ( if (!data.encrypt && data.service.endsWith("_PRIVATE")) { throw new Error("Only encrypted data can go into private services"); } - if (data.fileId) { - data64 = await getFileFromContentScript(data.fileId); - } + if (data.encrypt) { try { const resKeyPair = await getKeyPair(); @@ -683,9 +685,7 @@ export const publishQDNResource = async ( ); const { accepted } = resPermission; if (accepted) { - if (data.fileId && !data.encrypt) { - data64 = await getFileFromContentScript(data.fileId); - } + try { const resPublish = await publishData({ registeredName: encodeURIComponent(name), @@ -971,7 +971,10 @@ export const voteOnPoll = async (data, isFromExtension) => { try { const url = await createEndpoint(`/polls/${encodeURIComponent(pollName)}`); const response = await fetch(url); - if (!response.ok) throw new Error("Failed to fetch poll"); + if (!response.ok){ + const errorMessage = await parseErrorResponse(response, "Failed to fetch poll"); + throw new Error(errorMessage); + } pollInfo = await response.json(); } catch (error) { @@ -979,6 +982,7 @@ export const voteOnPoll = async (data, isFromExtension) => { throw new Error(errorMsg); } if (!pollInfo || pollInfo.error) { + const errorMsg = (pollInfo && pollInfo.message) || "Poll not found"; throw new Error(errorMsg); }