Skip to content

armgit / ente

Public Imported from GitHub
0

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

Conv wip

Manav Rathi8e756215c0ee
diff --git a/web/packages/gallery/services/upload/remote.ts b/web/packages/gallery/services/upload/remote.ts
index af78b9f7c9..29ad5ae854 100644
--- a/web/packages/gallery/services/upload/remote.ts
+++ b/web/packages/gallery/services/upload/remote.ts
@@ -21,6 +21,11 @@ import { handleUploadError } from "ente-shared/error";
import HTTPService from "ente-shared/network/HTTPService";
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
@@ -60,10 +65,31 @@ 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.
*/
@@ -129,6 +155,7 @@ 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 190e7d5a83..4a2dade203 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -44,11 +44,7 @@ import {
import { FileType, type FileTypeInfo } from "ente-media/file-type";
import { encodeLivePhoto } from "ente-media/live-photo";
import { addToCollection } from "ente-new/photos/services/collection";
-import {
- CustomError,
- CustomErrorMessage,
- handleUploadError,
-} from "ente-shared/error";
+import { CustomError, handleUploadError } from "ente-shared/error";
import { mergeUint8Arrays } from "ente-utils/array";
import { ensureInteger, ensureNumber } from "ente-utils/ensure";
import type { UploadableUploadItem, UploadItem, UploadPathPrefix } from ".";
@@ -571,6 +567,49 @@ export const uploadCancelledErrorMessage = "Upload cancelled";
export const isUploadCancelledError = (e: unknown) =>
e instanceof Error && e.message == uploadCancelledErrorMessage;
+/**
+ * The message of the {@link Error} that is thrown when the upload fails because
+ * the user's current session has expired (e.g. maybe they logged this client
+ * out from another session), and that they need to login again.
+ */
+export const sessionExpiredErrorMessage = "Session expired";
+
+/**
+ * The message of the {@link Error} that is thrown when the upload fails because
+ * the user's subscription has expired.
+ */
+export const subscriptionExpiredErrorMessage = "Subscription expired";
+
+/**
+ * The message of the {@link Error} that is thrown when the upload fails because
+ * the user's storage space has been exhausted.
+ */
+export const storageLimitExceededErrorMessage = "Storage limit exceeded";
+
+/**
+ * The message of the {@link Error} that is thrown when the PUT request for the
+ * upload of a part of file (as part of an overall multipart upload) fails
+ * because the response did not have the etag error.
+ *
+ * This usually happens because some browser extension is blocking access to the
+ * ETag header (even when it is present in the remote S3 response). In self
+ * hosted scenarios, this can also happen if the remote S3 bucket does not have
+ * the appropriate CORS rules to allow access to the etag header.
+ */
+const eTagMissingErrorMessage = "ETag header not present in response";
+
+/**
+ * The message of the {@link Error} that is thrown when the size of the file
+ * being uploaded exceeds the maximum allowed file size.
+ *
+ * The client already checks for the size of the file being uploaded, and aborts
+ * the request if the client side limit is exceeded. An error with this message
+ * is thrown if we remote side validation fails.
+ *
+ * The UI outcome is the same in both cases.
+ */
+export const fileTooLargeErrorMessage = "File too large";
+
/**
* Some state and callbacks used during upload that are not tied to a specific
* file being uploaded.
@@ -759,7 +798,7 @@ export const upload = async (
log.error(`Upload failed for ${fileName}`, e);
const error = handleUploadError(e);
switch (error.message) {
- case CustomErrorMessage.eTagMissing:
+ case eTagMissingErrorMessage:
return { uploadResult: "blocked" };
case CustomError.FILE_TOO_LARGE:
return { uploadResult: "largerThanAvailableStorage" };
@@ -1697,7 +1736,7 @@ const uploadStreamUsingMultipart = async (
requestRetrier,
)
: await putFilePart(partUploadURL, partData, requestRetrier);
- if (!eTag) throw new Error(CustomErrorMessage.eTagMissing);
+ if (!eTag) throw new Error(eTagMissingErrorMessage);
updateUploadProgress(fileLocalID, percentPerPart * partNumber);
completedParts.push({ partNumber, eTag });
diff --git a/web/packages/shared/error/index.ts b/web/packages/shared/error/index.ts
index 014af48a68..76578adf18 100644
--- a/web/packages/shared/error/index.ts
+++ b/web/packages/shared/error/index.ts
@@ -22,14 +22,6 @@ export function isApiErrorResponse(object: any): object is ApiErrorResponse {
return object && "code" in object && "message" in object;
}
-/**
- * Constant string values used mark custom errors that we need to subsequently
- * catch and identify to deal with in a particular manner.
- */
-export const CustomErrorMessage = {
- eTagMissing: "ETag header not present in response",
-};
-
export const CustomError = {
ETAG_MISSING: "no header/etag present in response body",
FILE_TOO_LARGE: "file too large",