Skip to content

Commit d130482

Browse files
Handle missing key case
1 parent d3383df commit d130482

File tree

1 file changed

+23
-23
lines changed
  • packages/utils/src/lib/server/cache

1 file changed

+23
-23
lines changed

packages/utils/src/lib/server/cache/s3.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,34 @@ export class S3Cache<V extends Uint8Array = Uint8Array> extends BaseCache<V> {
4545

4646
public async get(key: string): Promise<V | undefined> {
4747
const s3Key = this.buildKey(key);
48-
const head = await this.#client.send(
49-
new HeadObjectCommand({
50-
Bucket: this.#options.bucket,
51-
Key: s3Key,
52-
}),
53-
);
54-
55-
if (!head.Metadata) {
56-
return undefined;
57-
}
48+
try {
49+
const head = await this.#client.send(
50+
new HeadObjectCommand({
51+
Bucket: this.#options.bucket,
52+
Key: s3Key,
53+
}),
54+
);
5855

59-
const expiresAtStr = head.Metadata?.['expires-at'];
60-
if (expiresAtStr && Date.now() > Number.parseInt(expiresAtStr, 10)) {
61-
await this.delete(key);
62-
return undefined;
63-
}
56+
const expiresAtStr = head.Metadata?.['expires-at'];
57+
if (expiresAtStr && Date.now() > Number.parseInt(expiresAtStr, 10)) {
58+
await this.delete(key);
59+
return undefined;
60+
}
6461

65-
const res = await this.#client.send(
66-
new GetObjectCommand({
67-
Bucket: this.#options.bucket,
68-
Key: s3Key,
69-
}),
70-
);
62+
const res = await this.#client.send(
63+
new GetObjectCommand({
64+
Bucket: this.#options.bucket,
65+
Key: s3Key,
66+
}),
67+
);
7168

72-
if (!res.Body) {
69+
if (!res.Body) {
70+
return undefined;
71+
}
72+
return res.Body.transformToByteArray() as Promise<V>;
73+
} catch {
7374
return undefined;
7475
}
75-
return res.Body.transformToByteArray() as Promise<V>;
7676
}
7777

7878
public async set(

0 commit comments

Comments
 (0)