From 0810967aaec5383eaa6733d7d8cce2adb0cd47cc Mon Sep 17 00:00:00 2001 From: Prateek Sunal Date: Tue, 7 May 2024 20:05:19 +0530 Subject: [PATCH] fix(auth): store error in Code --- auth/lib/models/code.dart | 22 +++++++++++++++++++++- auth/lib/store/code_store.dart | 28 ++++++++++++++-------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/auth/lib/models/code.dart b/auth/lib/models/code.dart index 9bf89c1a1..ba66e2465 100644 --- a/auth/lib/models/code.dart +++ b/auth/lib/models/code.dart @@ -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); } } } diff --git a/auth/lib/store/code_store.dart b/auth/lib/store/code_store.dart index 81c2d2dca..946bbeb7b 100644 --- a/auth/lib/store/code_store.dart +++ b/auth/lib/store/code_store.dart @@ -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