web: handle errors when resolving upload urls

This commit is contained in:
Abdullah Atta
2024-06-07 10:30:36 +05:00
parent ef93ecc481
commit a6af92c330

View File

@@ -296,12 +296,15 @@ async function singlePartUploadFile(
console.log("Streaming file upload!"); console.log("Streaming file upload!");
const { url, headers, signal } = requestOptions; const { url, headers, signal } = requestOptions;
const uploadUrl = await fetch(url, { const uploadUrl: string | { error?: string } = await fetch(url, {
method: "PUT", method: "PUT",
headers, headers,
signal signal
}).then((res) => (res.ok ? res.text() : null)); }).then((res) => (res.ok ? res.text() : res.json()));
if (!uploadUrl) throw new Error("Unable to resolve attachment upload url."); if (typeof uploadUrl !== "string")
throw new Error(
uploadUrl.error || "Unable to resolve attachment upload url."
);
const response = await axios.request({ const response = await axios.request({
url: uploadUrl, url: uploadUrl,
@@ -354,6 +357,9 @@ async function multiPartUploadFile(
throw new WrappedError("Could not initiate multi-part upload.", e); throw new WrappedError("Could not initiate multi-part upload.", e);
}); });
if (initiateMultiPartUpload.data.error)
throw new Error(initiateMultiPartUpload.data.error);
uploadId = initiateMultiPartUpload.data.uploadId; uploadId = initiateMultiPartUpload.data.uploadId;
const { parts } = initiateMultiPartUpload.data; const { parts } = initiateMultiPartUpload.data;