Skip to content

armgit / ente

Public Imported from GitHub
0

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

Shorten

Manav Rathi53a8de9ac432
diff --git a/web/packages/new/photos/services/ml/embedding.ts b/web/packages/new/photos/services/ml/embedding.ts
index c04a2e0e8e..f4c290893c 100644
--- a/web/packages/new/photos/services/ml/embedding.ts
+++ b/web/packages/new/photos/services/ml/embedding.ts
@@ -7,7 +7,7 @@ import {
getLocalTrashedFiles,
} from "@/new/photos/services/files";
import type { EnteFile } from "@/new/photos/types/file";
-import { HTTPError, authenticatedRequestHeaders } from "@/next/http";
+import { authenticatedRequestHeaders, ensure2xx } from "@/next/http";
import { getKV, setKV } from "@/next/kv";
import log from "@/next/log";
import { apiURL } from "@/next/origins";
@@ -243,8 +243,7 @@ const getEmbeddingsDiff = async (
const res = await fetch(`${url}?${params.toString()}`, {
headers: await authenticatedRequestHeaders(),
});
- if (!res.ok) throw new HTTPError(url, res);
-
+ ensure2xx(res);
return z.object({ diff: z.array(RemoteEmbedding) }).parse(await res.json())
.diff;
};
@@ -272,8 +271,7 @@ export const putEmbedding = async (
const { encryptedMetadataB64, decryptionHeaderB64 } =
await encryptFileMetadata(embedding, enteFile.key);
- const url = await apiURL("/embeddings");
- const res = await fetch(url, {
+ const res = await fetch(await apiURL("/embeddings"), {
method: "PUT",
headers: await authenticatedRequestHeaders(),
body: JSON.stringify({
@@ -283,7 +281,7 @@ export const putEmbedding = async (
model,
}),
});
- if (!res.ok) throw new HTTPError(url, res);
+ ensure2xx(res);
};
// MARK: - Face
diff --git a/web/packages/next/http.ts b/web/packages/next/http.ts
index 4cfb5cfdff..65917e8d90 100644
--- a/web/packages/next/http.ts
+++ b/web/packages/next/http.ts
@@ -27,8 +27,10 @@ export const clientPackageHeader = () => ({
export class HTTPError extends Error {
res: Response;
- constructor(url: string, res: Response) {
- super(`Failed to fetch ${url}: HTTP ${res.status}`);
+ constructor(res: Response) {
+ // Nb: res.url is URL obtained after any redirects, and thus is not
+ // necessarily the same as the request's URL.
+ super(`Fetch failed: ${res.url}: HTTP ${res.status} ${res.statusText}`);
// Cargo culted from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
@@ -40,6 +42,14 @@ export class HTTPError extends Error {
}
}
+/**
+ * A convenience method that throws an {@link HTTPError} if the given
+ * {@link Response} does not have a HTTP 2xx status.
+ */
+export const ensure2xx = (res: Response) => {
+ if (!res.ok) throw new HTTPError(res);
+};
+
/**
* Return true if this is a HTTP "client" error.
*