Basic Webuntis error handling

This commit is contained in:
2023-03-31 21:24:38 +02:00
parent 4b9994a7ef
commit 9b6a307212
10 changed files with 148 additions and 153 deletions

View File

@ -1,6 +1,8 @@
import 'dart:convert';
import 'dart:developer';
import 'package:localstore/localstore.dart';
import 'package:marianum_mobile/api/webuntis/webuntisError.dart';
abstract class RequestCache<T> {
static const int cacheNothing = 0;
@ -10,9 +12,12 @@ abstract class RequestCache<T> {
int maxCacheTime;
Function(T) onUpdate;
Function(Exception) onError;
bool? renew;
RequestCache(this.maxCacheTime, this.onUpdate, {this.renew = false});
RequestCache(this.maxCacheTime, this.onUpdate, {this.onError = ignore, this.renew = false});
static void ignore(Exception e) {}
void start(String file, String document) async {
Map<String, dynamic>? tableData = await Localstore.instance.collection(file).doc(document).get();
@ -24,13 +29,18 @@ abstract class RequestCache<T> {
if(renew == null || !renew!) return;
}
T newValue = await onLoad();
onUpdate(newValue);
try {
T newValue = await onLoad();
onUpdate(newValue);
Localstore.instance.collection(file).doc(document).set({
"json": jsonEncode(newValue),
"lastupdate": DateTime.now().millisecondsSinceEpoch
});
Localstore.instance.collection(file).doc(document).set({
"json": jsonEncode(newValue),
"lastupdate": DateTime.now().millisecondsSinceEpoch
});
} on WebuntisError catch(e) {
log("ERROR CATCHED");
onError(e);
}
}
T onLocalData(String json);