fix(auth): store error in Code

This commit is contained in:
Prateek Sunal 2024-05-07 20:05:19 +05:30
parent e1239a6490
commit 0810967aae
2 changed files with 35 additions and 15 deletions

View file

@ -26,6 +26,9 @@ class Code {
bool get isPinned => display.pinned;
final Object? err;
bool get hasError => err != null;
Code(
this.account,
this.issuer,
@ -38,8 +41,25 @@ class Code {
this.rawData, {
this.generatedID,
required this.display,
this.err,
});
factory Code.withError(Object error) {
return Code(
"",
"",
0,
0,
"",
Algorithm.sha1,
Type.totp,
0,
"",
err: error,
display: CodeDisplay(),
);
}
Code copyWith({
String? account,
String? issuer,
@ -123,7 +143,7 @@ class Code {
if (rawData.contains("#")) {
return Code.fromOTPAuthUrl(rawData.replaceAll("#", '%23'));
} else {
rethrow;
return Code.withError(e);
}
}
}

View file

@ -31,22 +31,22 @@ class CodeStore {
bool hasError = false;
for (final entity in entities) {
try {
final decodeJson = jsonDecode(entity.rawData);
final decodeJson = jsonDecode(entity.rawData);
late Code code;
if (decodeJson is String && decodeJson.startsWith('otpauth://')) {
code = Code.fromOTPAuthUrl(decodeJson);
} else {
code = Code.fromExportJson(decodeJson);
}
code.generatedID = entity.generatedID;
code.hasSynced = entity.hasSynced;
codes.add(code);
} catch (e) {
hasError = true;
_logger.severe("Could not parse code", e);
late Code code;
if (decodeJson is String && decodeJson.startsWith('otpauth://')) {
code = Code.fromOTPAuthUrl(decodeJson);
} else {
code = Code.fromExportJson(decodeJson);
}
if (code.hasError) {
hasError = true;
_logger.severe("Could not parse code", code.err);
continue;
}
code.generatedID = entity.generatedID;
code.hasSynced = entity.hasSynced;
codes.add(code);
}
// sort codes by issuer,account