Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Handle in same file

Manav Rathi63597b3bc4bd
diff --git a/web/packages/gallery/services/upload/remote.ts b/web/packages/gallery/services/upload/remote.ts
index 9bfa182d83..8e2952435c 100644
--- a/web/packages/gallery/services/upload/remote.ts
+++ b/web/packages/gallery/services/upload/remote.ts
@@ -14,11 +14,6 @@ import {
} from "ente-media/file";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
-import {
- sessionExpiredErrorMessage,
- storageLimitExceededErrorMessage,
- subscriptionExpiredErrorMessage,
-} from "./upload-service";
/**
* A pre-signed URL alongwith the associated object key that is later used to
@@ -58,31 +53,10 @@ export const fetchUploadURLs = async (countHint: number) => {
const res = await fetch(`${url}?${params.toString()}`, {
headers: await authenticatedRequestHeaders(),
});
- handleURLFetchErrors(res);
ensureOk(res);
return ObjectUploadURLResponse.parse(await res.json()).urls;
};
-/**
- * Throw, if needed, custom errors corresponding to specific HTTP responses for
- * the API calls to fetch new upload URLs:
- *
- * - {@link fetchUploadURLs}
- *
- * - {@link fetchMultipartUploadURLs}
- *
- */
-const handleURLFetchErrors = (res: Response) => {
- switch (res.status) {
- case 401:
- throw new Error(sessionExpiredErrorMessage);
- case 402:
- throw new Error(subscriptionExpiredErrorMessage);
- case 426:
- throw new Error(storageLimitExceededErrorMessage);
- }
-};
-
/**
* Sibling of {@link fetchUploadURLs} for public albums.
*/
@@ -148,7 +122,6 @@ export const fetchMultipartUploadURLs = async (uploadPartCount: number) => {
const res = await fetch(`${url}?${params.toString()}`, {
headers: await authenticatedRequestHeaders(),
});
- handleURLFetchErrors(res);
ensureOk(res);
return MultipartUploadURLsResponse.parse(await res.json()).urls;
};
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index 7ba7764e74..d89ed71b8c 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -188,18 +188,28 @@ class UploadService {
this.publicAlbumsCredentials,
);
} else {
- urls = await fetchUploadURLs(this.pendingUploadCount);
+ try {
+ urls = await fetchUploadURLs(this.pendingUploadCount);
+ } catch (e) {
+ throw translateURLFetchErrorIfNeeded(e);
+ }
}
urls.forEach((u) => this.uploadURLs.push(u));
}
async fetchMultipartUploadURLs(uploadPartCount: number) {
- return this.publicAlbumsCredentials
- ? fetchPublicAlbumsMultipartUploadURLs(
- uploadPartCount,
- this.publicAlbumsCredentials,
- )
- : fetchMultipartUploadURLs(uploadPartCount);
+ if (this.publicAlbumsCredentials) {
+ return fetchPublicAlbumsMultipartUploadURLs(
+ uploadPartCount,
+ this.publicAlbumsCredentials,
+ );
+ } else {
+ try {
+ return await fetchMultipartUploadURLs(uploadPartCount);
+ } catch (e) {
+ throw translateURLFetchErrorIfNeeded(e);
+ }
+ }
}
}
@@ -804,6 +814,30 @@ export const upload = async (
}
};
+/**
+ * Convert specific HTTP errors during an API call to remote endpoints for
+ * fetching new upload URLs into error with known messages (if applicable).
+ *
+ * Can be used with the following functions:
+ *
+ * - {@link fetchUploadURLs}
+ * - {@link fetchMultipartUploadURLs}
+ *
+ */
+const translateURLFetchErrorIfNeeded = (e: unknown) => {
+ if (e instanceof HTTPError) {
+ switch (e.res.status) {
+ case 401:
+ return new Error(sessionExpiredErrorMessage);
+ case 402:
+ return new Error(subscriptionExpiredErrorMessage);
+ case 426:
+ return new Error(storageLimitExceededErrorMessage);
+ }
+ }
+ return e;
+};
+
/**
* Read the given file or path or zip item into an in-memory representation.
*