Skip to content

Commit

Permalink
fix(cache): don't catch errors (#10449)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Jun 17, 2021
1 parent d4a22c7 commit 55d23ad
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions lib/util/cache/package/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import * as packageCache from '.';

type Handler<T> = (parameters: DecoratorParameters<T>) => Promise<unknown>;
Expand Down Expand Up @@ -90,37 +89,32 @@ export function cache<T>({
ttlMinutes = 30,
}: CacheParameters): Decorator<T> {
return decorate(async ({ args, instance, callback }) => {
try {
let finalNamespace: string;
if (is.string(namespace)) {
finalNamespace = namespace;
} else if (is.function_(namespace)) {
finalNamespace = namespace.apply(instance, args);
}

let finalKey: string;
if (is.string(key)) {
finalKey = key;
} else if (is.function_(key)) {
finalKey = key.apply(instance, args);
}

const cachedResult = await packageCache.get<unknown>(
finalNamespace,
finalKey
);

if (cachedResult !== undefined) {
return cachedResult;
}

const result = await callback();

await packageCache.set(finalNamespace, finalKey, result, ttlMinutes);
return result;
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, 'cache decorate error');
throw err;
let finalNamespace: string;
if (is.string(namespace)) {
finalNamespace = namespace;
} else if (is.function_(namespace)) {
finalNamespace = namespace.apply(instance, args);
}

let finalKey: string;
if (is.string(key)) {
finalKey = key;
} else if (is.function_(key)) {
finalKey = key.apply(instance, args);
}

const cachedResult = await packageCache.get<unknown>(
finalNamespace,
finalKey
);

if (cachedResult !== undefined) {
return cachedResult;
}

const result = await callback();

await packageCache.set(finalNamespace, finalKey, result, ttlMinutes);
return result;
});
}

0 comments on commit 55d23ad

Please sign in to comment.