Skip to content

Commit 15da503

Browse files
committed
feat(cache): make dummy cache error ignorable
All "dummy" cache methods throw by design. This PR makes the corresponding errors ignorable so that the logs are less verbose and always use the DEBUG level.
1 parent 4a500ee commit 15da503

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

packages/open-next/src/adapters/cache.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IgnorableError, isIgnorableError } from "utils/error";
12
import { isBinaryContentType } from "./binary";
23
import { debug, error, warn } from "./logger";
34

@@ -192,7 +193,11 @@ export default class S3Cache {
192193
} as CacheHandlerValue;
193194
} catch (e) {
194195
// We can usually ignore errors here as they are usually due to cache not being found
195-
debug("Failed to get fetch cache", e);
196+
if (isIgnorableError(e)) {
197+
debug("Failed to get fetch cache", e.message);
198+
} else {
199+
debug("Failed to get fetch cache", e);
200+
}
196201
return null;
197202
}
198203
}
@@ -271,7 +276,11 @@ export default class S3Cache {
271276
return null;
272277
} catch (e) {
273278
// We can usually ignore errors here as they are usually due to cache not being found
274-
debug("Failed to get body cache", e);
279+
if (isIgnorableError(e)) {
280+
debug("Failed to get body cache", e.message);
281+
} else {
282+
debug("Failed to get body cache", e);
283+
}
275284
return null;
276285
}
277286
}
@@ -409,7 +418,11 @@ export default class S3Cache {
409418
}
410419
debug("Finished setting cache");
411420
} catch (e) {
412-
error("Failed to set cache", e);
421+
if (isIgnorableError(e)) {
422+
debug("Failed to set cache", e.message);
423+
} else {
424+
error("Failed to set cache", e);
425+
}
413426
} finally {
414427
// We need to resolve the promise even if there was an error
415428
detachedPromise?.resolve();
@@ -456,7 +469,11 @@ export default class S3Cache {
456469
await globalThis.tagCache.writeTags(toInsert);
457470
}
458471
} catch (e) {
459-
error("Failed to revalidate tag", e);
472+
if (isIgnorableError(e)) {
473+
debug("Failed to revalidate tag", e.message);
474+
} else {
475+
error("Failed to revalidate tag", e);
476+
}
460477
}
461478
}
462479
}

packages/open-next/src/overrides/incrementalCache/dummy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { IncrementalCache } from "types/overrides";
2+
import { IgnorableError } from "utils/error";
23

34
const dummyIncrementalCache: IncrementalCache = {
45
name: "dummy",
56
get: async () => {
6-
throw new Error("Dummy cache is not implemented");
7+
throw new IgnorableError('"Dummy" cache does not cache anything');
78
},
89
set: async () => {
9-
throw new Error("Dummy cache is not implemented");
10+
throw new IgnorableError('"Dummy" cache does not cache anything');
1011
},
1112
delete: async () => {
12-
throw new Error("Dummy cache is not implemented");
13+
throw new IgnorableError('"Dummy" cache does not cache anything');
1314
},
1415
};
1516

packages/open-next/src/utils/error.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface BaseOpenNextError {
55
readonly logLevel: 0 | 1 | 2;
66
}
77

8+
const IGNORABLE_ERROR_NAME = "IgnorableError";
9+
810
// This is an error that can be totally ignored
911
// It don't even need to be logged, or only in debug mode
1012
export class IgnorableError extends Error implements BaseOpenNextError {
@@ -13,10 +15,17 @@ export class IgnorableError extends Error implements BaseOpenNextError {
1315
readonly logLevel = 0;
1416
constructor(message: string) {
1517
super(message);
16-
this.name = "IgnorableError";
18+
this.name = IGNORABLE_ERROR_NAME;
1719
}
1820
}
1921

22+
// Note that `e instanceof IgnorableError` might not work across bundles
23+
// embedding a distinct implementation of `IgnorableError`.
24+
export function isIgnorableError(error: unknown): error is IgnorableError {
25+
const anyError = error as any;
26+
return anyError.__openNextInternal && anyError.name === IGNORABLE_ERROR_NAME;
27+
}
28+
2029
// This is an error that can be recovered from
2130
// It should be logged but the process can continue
2231
export class RecoverableError extends Error implements BaseOpenNextError {

0 commit comments

Comments
 (0)