Skip to content

armgit / ente

Public Imported from GitHub
0

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

Replace message-based error checks

Manav Rathi82e7029272ed
diff --git a/infra/staff/src/App.tsx b/infra/staff/src/App.tsx
index 5e8f61ef9f..986fccdb27 100644
--- a/infra/staff/src/App.tsx
+++ b/infra/staff/src/App.tsx
@@ -80,15 +80,7 @@ export const App: React.FC = () => {
const userResult = await getUser(
{ token: authToken },
userSearchInput,
- ).catch((error: unknown) => {
- if (
- error instanceof Error &&
- error.message === "User not found"
- ) {
- return undefined;
- }
- throw error;
- });
+ );
if (requestID !== searchRequestID.current) return;
if (userResult) {
const userDetailsData = buildUserDetailsData(
diff --git a/infra/staff/src/services/admin-user.ts b/infra/staff/src/services/admin-user.ts
index bd0b6ece62..fea34cea65 100644
--- a/infra/staff/src/services/admin-user.ts
+++ b/infra/staff/src/services/admin-user.ts
@@ -168,9 +168,7 @@ export const getUser = async (
if (response.status === 403) {
throw new Error("Insufficient permissions");
}
- if (response.status === 404) {
- throw new Error("User not found");
- }
+ if (response.status === 404) return undefined;
await ensureOk(response, "Network response was not ok");
return UserResponse.parse(await response.json());
diff --git a/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx b/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx
index 9f1d431f8d..e97295a408 100644
--- a/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx
+++ b/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx
@@ -1,7 +1,7 @@
import {
+ castDeviceNotFoundErrorName,
publishCastPayload,
revokeAllCastTokens,
- unknownDeviceCodeErrorMessage,
} from "@/services/cast";
import { loadCast } from "@/utils/chromecast-sender";
import { Link, Stack, Typography } from "@mui/material";
@@ -66,7 +66,7 @@ const AlbumCastDialogContents: React.FC<AlbumCastDialogProps> = ({
log.error("Failed to cast", e);
if (
e instanceof Error &&
- e.message == unknownDeviceCodeErrorMessage
+ e.name == castDeviceNotFoundErrorName
) {
setFieldError(t("tv_not_found"));
} else {
diff --git a/web/apps/photos/src/services/cast.ts b/web/apps/photos/src/services/cast.ts
index b24c57da33..bcf0f2ffd4 100644
--- a/web/apps/photos/src/services/cast.ts
+++ b/web/apps/photos/src/services/cast.ts
@@ -22,15 +22,18 @@ const publicKeysForPairingCode = async (code: string) => {
.parse(await res.json());
};
-// AlbumCastDialog matches this exact message.
-export const unknownDeviceCodeErrorMessage = "Unknown device code";
+export const castDeviceNotFoundErrorName = "cast_device_not_found";
export const publishCastPayload = async (
deviceCode: string,
collection: Collection,
) => {
const publicKeys = await publicKeysForPairingCode(deviceCode);
- if (!publicKeys) throw new Error(unknownDeviceCodeErrorMessage);
+ if (!publicKeys) {
+ const error = new Error("Unknown device code");
+ error.name = castDeviceNotFoundErrorName;
+ throw error;
+ }
const { preparePayload } = await import("ente-cast-wasm");
const { castToken, encryptedPayload } = preparePayload(
diff --git a/web/packages/gallery/utils/detect-type.ts b/web/packages/gallery/utils/detect-type.ts
index 7dfd5268e1..07dfa698b7 100644
--- a/web/packages/gallery/utils/detect-type.ts
+++ b/web/packages/gallery/utils/detect-type.ts
@@ -7,6 +7,8 @@ import {
} from "ente-media/file-type";
import { fileTypeFromBuffer } from "file-type";
+const fileTypeNotSupportedErrorName = "file_type_not_supported";
+
export const detectFileTypeInfo = async (file: File): Promise<FileTypeInfo> =>
detectFileTypeInfoFromChunk(() => readInitialChunkOfFile(file), file.name);
@@ -27,8 +29,9 @@ export const detectFileTypeInfoFromChunk = async (
} else if (mimeType.startsWith("video/")) {
fileType = FileType.video;
} else {
- // Keep this prefix in sync with isFileTypeNotSupportedError.
- throw new Error(`Unsupported file format (MIME type ${mimeType})`);
+ throw fileTypeNotSupportedError(
+ `Unsupported file format (MIME type ${mimeType})`,
+ );
}
return { fileType, extension: typeResult.ext, mimeType };
@@ -38,8 +41,7 @@ export const detectFileTypeInfoFromChunk = async (
if (known) return known;
if (extension && KnownNonMediaFileExtensions.includes(extension)) {
- // Keep this prefix in sync with isFileTypeNotSupportedError.
- throw new Error(
+ throw fileTypeNotSupportedError(
`Unsupported file format (extension ${extension})`,
{ cause: e },
);
@@ -50,7 +52,16 @@ export const detectFileTypeInfoFromChunk = async (
};
export const isFileTypeNotSupportedError = (e: unknown) =>
- e instanceof Error && e.message.startsWith("Unsupported file format");
+ e instanceof Error && e.name == fileTypeNotSupportedErrorName;
+
+const fileTypeNotSupportedError = (
+ message: string,
+ options?: ErrorOptions,
+) => {
+ const error = new Error(message, options);
+ error.name = fileTypeNotSupportedErrorName;
+ return error;
+};
const readInitialChunkOfFile = async (file: File) => {
const chunkSizeForTypeDetection = 4100;