From db4a9ee88035ad3e91104e77fc2d12d1329cf756 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 21 Apr 2023 19:50:01 +0100 Subject: [PATCH] Return "Resource does not exist" error if requesting a non-existent resource via GET_QDN_RESOURCE_URL. --- Q-Apps.md | 3 +++ src/main/resources/q-apps/q-apps.js | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Q-Apps.md b/Q-Apps.md index 936ebd30..0f52c086 100644 --- a/Q-Apps.md +++ b/Q-Apps.md @@ -653,6 +653,7 @@ let res = await qortalRequest({ ``` ### Get URL to load a QDN resource +Note: this returns a "Resource does not exist" error if a non-existent resource is requested. ``` let url = await qortalRequest({ action: "GET_QDN_RESOURCE_URL", @@ -664,6 +665,7 @@ let url = await qortalRequest({ ``` ### Get URL to load a QDN website +Note: this returns a "Resource does not exist" error if a non-existent resource is requested. ``` let url = await qortalRequest({ action: "GET_QDN_RESOURCE_URL", @@ -673,6 +675,7 @@ let url = await qortalRequest({ ``` ### Get URL to load a specific file from a QDN website +Note: this returns a "Resource does not exist" error if a non-existent resource is requested. ``` let url = await qortalRequest({ action: "GET_QDN_RESOURCE_URL", diff --git a/src/main/resources/q-apps/q-apps.js b/src/main/resources/q-apps/q-apps.js index 262d19a8..ba3ee39b 100644 --- a/src/main/resources/q-apps/q-apps.js +++ b/src/main/resources/q-apps/q-apps.js @@ -46,6 +46,18 @@ function handleResponse(event, response) { responseObj = response; } + // GET_QDN_RESOURCE_URL has custom handling + const data = event.data; + if (data.action == "GET_QDN_RESOURCE_URL") { + if (responseObj == null || responseObj.status == null || responseObj.status == "NOT_PUBLISHED") { + responseObj = {}; + responseObj.error = "Resource does not exist"; + } + else { + responseObj = buildResourceUrl(data.service, data.name, data.identifier, data.path, false); + } + } + // Respond to app if (responseObj.error != null) { event.ports[0].postMessage({ @@ -173,9 +185,10 @@ window.addEventListener("message", (event) => { return httpGetAsyncWithEvent(event, "/names/" + data.name); case "GET_QDN_RESOURCE_URL": - const response = buildResourceUrl(data.service, data.name, data.identifier, data.path, false); - handleResponse(event, response); - return; + // Check status first; URL is built ant returned automatically after status check + url = "/arbitrary/resource/status/" + data.service + "/" + data.name; + if (data.identifier != null) url = url.concat("/" + data.identifier); + return httpGetAsyncWithEvent(event, url); case "LINK_TO_QDN_RESOURCE": if (data.service == null) data.service = "WEBSITE"; // Default to WEBSITE