From 9bb35692413b59dce3438926a2b5b377c3e44573 Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Fri, 25 Nov 2022 17:31:47 -0500 Subject: [PATCH] feat(resiliance): automatically recover from malformed cache files; just warn and move on --- src/cache.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index ef7c988..d75f90a 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -217,11 +217,29 @@ export const createCache = ({ key, }); if (cacheContentSerialized === undefined) return undefined; // if not in cache, then undefined - const cacheContent = JSON.parse(cacheContentSerialized); - if (isRecordExpired(cacheContent)) return undefined; // if already expired, then undefined - if (cacheContent.deserializedForObservability) - return JSON.stringify(cacheContent.value); // if it had been deserialized for observability, reserialize it - return cacheContent.value as string; // otherwise, its in the cache and not expired, so return the value + try { + const cacheContent = JSON.parse(cacheContentSerialized); + if (isRecordExpired(cacheContent)) return undefined; // if already expired, then undefined + if (cacheContent.deserializedForObservability) + return JSON.stringify(cacheContent.value); // if it had been deserialized for observability, reserialize it + return cacheContent.value as string; // otherwise, its in the cache and not expired, so return the value + } catch (error) { + // if it was a json parsing error, warn about it and treat the key as invalid + if ( + error instanceof Error && + error.message.includes('Unexpected string in JSON at position') + ) { + // eslint-disable-next-line no-console + console.warn( + 'simple-on-disk-cache: detected unparseable cache file. treating the result as invalid. this should not have occured', + { key }, + ); + return undefined; + } + + // otherwise, propagate the error, we dont know how to handle it + throw error; + } }; /**