File tree Expand file tree Collapse file tree 2 files changed +19
-10
lines changed Expand file tree Collapse file tree 2 files changed +19
-10
lines changed Original file line number Diff line number Diff line change 1
1
import type { CacheMetadata } from './common' ;
2
2
3
+ export function logKey ( key ?: string ) {
4
+ return key ? `for ${ key } ` : '' ;
5
+ }
6
+
3
7
export function assertCacheEntry (
4
8
entry : unknown ,
5
9
key ?: string ,
@@ -9,9 +13,9 @@ export function assertCacheEntry(
9
13
} {
10
14
if ( ! isRecord ( entry ) ) {
11
15
throw new Error (
12
- `Cache entry ${
13
- key ? `for ${ key } ` : ''
14
- } is not a cache entry object, it's a ${ typeof entry } `,
16
+ `Cache entry ${ logKey (
17
+ key ,
18
+ ) } is not a cache entry object, it's a ${ typeof entry } `,
15
19
) ;
16
20
}
17
21
if (
@@ -21,17 +25,13 @@ export function assertCacheEntry(
21
25
( entry . metadata . swr != null && typeof entry . metadata . swr !== 'number' )
22
26
) {
23
27
throw new Error (
24
- `Cache entry ${
25
- key ? `for ${ key } ` : ''
26
- } does not have valid metadata property`,
28
+ `Cache entry ${ logKey ( key ) } does not have valid metadata property` ,
27
29
) ;
28
30
}
29
31
30
32
if ( ! ( 'value' in entry ) ) {
31
33
throw new Error (
32
- `Cache entry for ${
33
- key ? `for ${ key } ` : ''
34
- } does not have a value property`,
34
+ `Cache entry for ${ logKey ( key ) } does not have a value property` ,
35
35
) ;
36
36
}
37
37
}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import {
18
18
totalTtl ,
19
19
} from './index' ;
20
20
import { Deferred } from './createBatch' ;
21
+ import { logKey } from './assertCacheEntry' ;
21
22
22
23
jest . mock ( './index' , ( ) => {
23
24
if ( process . version . startsWith ( 'v18' ) ) {
@@ -1249,7 +1250,9 @@ describe('cachified', () => {
1249
1250
( cb as Function ) ( new Error ( 'Nope2' ) , null ) ;
1250
1251
return false ;
1251
1252
} ) ;
1252
- expect ( cache . delete ( 'test-0' ) ) . rejects . toThrowErrorMatchingInlineSnapshot ( `"Nope2"` ) ;
1253
+ expect ( cache . delete ( 'test-0' ) ) . rejects . toThrowErrorMatchingInlineSnapshot (
1254
+ `"Nope2"` ,
1255
+ ) ;
1253
1256
1254
1257
// handle corrupt cache
1255
1258
await new Promise ( ( res ) => redis . set ( 'test-3' , '{{{' , res ) ) ;
@@ -1589,3 +1592,9 @@ describe('totalTtl helper', () => {
1589
1592
expect ( totalTtl ( { createdTime : 0 , swr : 5 } ) ) . toBe ( 5 ) ;
1590
1593
} ) ;
1591
1594
} ) ;
1595
+
1596
+ describe ( 'internal logKey helper' , ( ) => {
1597
+ it ( 'falls back to empty string, when no key given' , ( ) => {
1598
+ expect ( logKey ( ) ) . toBe ( '' ) ;
1599
+ } ) ;
1600
+ } ) ;
You can’t perform that action at this time.
0 commit comments