Skip to content

Commit 4da621e

Browse files
fix(cache-interceptor): use try catch in getCache method
Signed-off-by: Taranjeet Singh <[email protected]>
1 parent b7f962a commit 4da621e

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

lib/src/interceptors/cache_interceptor.dart

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,49 +37,54 @@ abstract class ApiCacheInterceptor extends Interceptor {
3737
}
3838

3939
Future<Response?> _getCacheResponse(RequestOptions options) async {
40-
final _now = DateTime.now();
41-
final _midnightTime =
42-
DateTime(_now.year, _now.month, _now.day + 1).subtract(
43-
Duration(
44-
seconds: 1,
45-
),
46-
);
47-
48-
final _apiDataKey = _formStringFromRequestHeaders(options);
49-
final _storeRef = StoreRef.main();
50-
final keyData = await sembastAppDb.get(_storeRef.record(_apiDataKey))
51-
as Map<String, dynamic>?;
52-
53-
if (keyData != null) {
54-
final isValid = isCacheValid(
55-
DateTime.parse(
56-
keyData['appSpecificHeaders']?['expirationTime'] ?? _midnightTime,
57-
),
58-
DateTime.parse(
59-
keyData['appSpecificHeaders']?['cachedTime'],
40+
try {
41+
final _now = DateTime.now();
42+
final _midnightTime =
43+
DateTime(_now.year, _now.month, _now.day + 1).subtract(
44+
Duration(
45+
seconds: 1,
6046
),
6147
);
62-
if (!isValid) {
63-
if (options.headers['appSpecificHeaders']?['ignoreAutoRefresh'] ??
64-
false) {
65-
return null;
48+
49+
final _apiDataKey = _formStringFromRequestHeaders(options);
50+
final _storeRef = StoreRef.main();
51+
final keyData = await sembastAppDb.get(_storeRef.record(_apiDataKey))
52+
as Map<String, dynamic>?;
53+
54+
if (keyData != null) {
55+
final isValid = isCacheValid(
56+
DateTime.parse(
57+
keyData['appSpecificHeaders']?['expirationTime'] ?? _midnightTime,
58+
),
59+
DateTime.parse(
60+
keyData['appSpecificHeaders']?['cachedTime'],
61+
),
62+
);
63+
if (!isValid) {
64+
if (options.headers['appSpecificHeaders']?['ignoreAutoRefresh'] ??
65+
false) {
66+
return null;
67+
}
68+
refreshCache(options);
6669
}
67-
refreshCache(options);
70+
71+
logger.v("************** 🔥 VALIDATING CACHE 🔥 ***********");
72+
return Response(
73+
requestOptions: options,
74+
data: jsonDecode(keyData['appSpecificHeaders']?['data'] ?? ''),
75+
statusCode: cache_response_status,
76+
redirects: [],
77+
extra: {},
78+
isRedirect: false,
79+
statusMessage: 'Cached Data',
80+
);
6881
}
6982

70-
logger.v("************** 🔥 VALIDATING CACHE 🔥 ***********");
71-
return Response(
72-
requestOptions: options,
73-
data: jsonDecode(keyData['appSpecificHeaders']?['data'] ?? ''),
74-
statusCode: cache_response_status,
75-
redirects: [],
76-
extra: {},
77-
isRedirect: false,
78-
statusMessage: 'Cached Data',
79-
);
83+
return null;
84+
} catch (e, stk) {
85+
logger.e(e, stk);
86+
return null;
8087
}
81-
82-
return null;
8388
}
8489

8590
void refreshCache(RequestOptions option);

0 commit comments

Comments
 (0)