Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache-interceptor): use try catch in getCache method #45

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions lib/src/interceptors/cache_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,54 @@ abstract class ApiCacheInterceptor extends Interceptor {
}

Future<Response?> _getCacheResponse(RequestOptions options) async {
final _now = DateTime.now();
final _midnightTime =
DateTime(_now.year, _now.month, _now.day + 1).subtract(
Duration(
seconds: 1,
),
);

final _apiDataKey = _formStringFromRequestHeaders(options);
final _storeRef = StoreRef.main();
final keyData = await sembastAppDb.get(_storeRef.record(_apiDataKey))
as Map<String, dynamic>?;

if (keyData != null) {
final isValid = isCacheValid(
DateTime.parse(
keyData['appSpecificHeaders']?['expirationTime'] ?? _midnightTime,
),
DateTime.parse(
keyData['appSpecificHeaders']?['cachedTime'],
try {
final _now = DateTime.now();
final _midnightTime =
DateTime(_now.year, _now.month, _now.day + 1).subtract(
Duration(
seconds: 1,
),
);
if (!isValid) {
if (options.headers['appSpecificHeaders']?['ignoreAutoRefresh'] ??
false) {
return null;

final _apiDataKey = _formStringFromRequestHeaders(options);
final _storeRef = StoreRef.main();
final keyData = await sembastAppDb.get(_storeRef.record(_apiDataKey))
as Map<String, dynamic>?;

if (keyData != null) {
final isValid = isCacheValid(
DateTime.parse(
keyData['appSpecificHeaders']?['expirationTime'] ?? _midnightTime,
),
DateTime.parse(
keyData['appSpecificHeaders']?['cachedTime'],
),
);
if (!isValid) {
if (options.headers['appSpecificHeaders']?['ignoreAutoRefresh'] ??
false) {
return null;
}
refreshCache(options);
}
refreshCache(options);

logger.v("************** 🔥 VALIDATING CACHE 🔥 ***********");
return Response(
requestOptions: options,
data: jsonDecode(keyData['appSpecificHeaders']?['data'] ?? ''),
statusCode: cache_response_status,
redirects: [],
extra: {},
isRedirect: false,
statusMessage: 'Cached Data',
);
}

logger.v("************** 🔥 VALIDATING CACHE 🔥 ***********");
return Response(
requestOptions: options,
data: jsonDecode(keyData['appSpecificHeaders']?['data'] ?? ''),
statusCode: cache_response_status,
redirects: [],
extra: {},
isRedirect: false,
statusMessage: 'Cached Data',
);
return null;
} catch (e, stk) {
logger.e(e, stk);
return null;
}

return null;
}

void refreshCache(RequestOptions option);
Expand Down
Loading