From 6ab4a7ca707941cd9d354e3d1d9b774fb4bfdde0 Mon Sep 17 00:00:00 2001 From: Srie Teja Date: Mon, 4 Dec 2023 19:05:42 +0530 Subject: [PATCH 1/4] fix: update exception handling in cacheable_secondary_address_finder.findSecondary() --- .../cacheable_secondary_address_finder.dart | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart b/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart index c2ce09de..38454e33 100644 --- a/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart +++ b/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart @@ -150,35 +150,37 @@ class SecondaryUrlFinder { // then the secondary domain name will be deemed to be the portion of rootDomain after 'proxy:' // and the secondary port will be deemed to be the rootPort return '${_rootDomain.substring("proxy:".length)}:$_rootPort'; - } else { - String? address; - for (int i = 0; i <= retryDelaysMillis.length; i++) { - try { - address = await _findSecondary(atSign); - return address; - } catch (e) { - if (i == retryDelaysMillis.length) { - _logger.severe('AtLookup.findSecondary $atSign failed with $e' - ' : ${retryDelaysMillis.length + 1} failures, giving up'); - rethrow; - } else { - _logger.info('AtLookup.findSecondary $atSign failed with $e' - ' : will retry in ${retryDelaysMillis[i]} milliseconds'); - await Future.delayed(Duration(milliseconds: retryDelaysMillis[i])); - } + } + String? address; + for (int i = 0; i <= retryDelaysMillis.length; i++) { + try { + address = await _findSecondary(atSign); + return address; + } catch (e) { + if (i < retryDelaysMillis.length) { + _logger.info('AtLookup.findSecondary for $atSign failed with $e' + ' : will retry in ${retryDelaysMillis[i]} milliseconds'); + await Future.delayed(Duration(milliseconds: retryDelaysMillis[i])); + continue; + } + _logger.severe('AtLookup.findSecondary for $atSign failed with $e' + ' : ${retryDelaysMillis.length + 1} failures, giving up'); + if (e is RootServerConnectivityException) { + throw AtConnectException( + 'Unable to establish connection with root server.' + ' Please check your internet connection and try again'); } } - throw AtConnectException( - 'CacheableSecondaryAddressFinder.SecondaryUrlFinder.findSecondaryUrl' - ' : ${retryDelaysMillis.length + 1} failures, giving up'); } + throw AtConnectException('Could not fetch secondary address for $atSign :' + ' ${retryDelaysMillis.length + 1} failures, giving up'); } Future _findSecondary(String atsign) async { String? response; SecureSocket? socket; try { - _logger.finer('AtLookup.findSecondary received atsign: $atsign'); + _logger.finer('findSecondaryUrl: received atsign: $atsign'); if (atsign.startsWith('@')) atsign = atsign.replaceFirst('@', ''); var answer = ''; String? secondary; @@ -188,7 +190,7 @@ class SecondaryUrlFinder { socket = await _socketFactory.createSocket( _rootDomain, '$_rootPort', SecureSocketConfig()); - + _logger.finer('findSecondaryUrl: connection to root server established'); // listen to the received data event stream socket.listen((List event) async { _logger.finest('root socket listener received: $event'); @@ -221,7 +223,7 @@ class SecondaryUrlFinder { await socket.flush(); socket.destroy(); _logger.finer( - 'AtLookup.findSecondary got answer: $secondary and closing connection'); + 'findSecondaryUrl got answer: $secondary and closing connection'); return response; } } @@ -230,8 +232,10 @@ class SecondaryUrlFinder { socket.destroy(); throw AtTimeoutException('AtLookup.findSecondary timed out'); } on SocketException catch (se) { + _logger.severe( + '_findSecondary caught exception [$se] while connecting to root server url'); throw RootServerConnectivityException( - '_findSecondary caught exception [$se] while connecting to root server url $_rootDomain on port $_rootPort'); + 'Could not connect to Root Server at $_rootDomain:$_rootPort'); } on Exception catch (exception) { _logger.severe('AtLookup.findSecondary connection to ' + _rootDomain + @@ -246,7 +250,7 @@ class SecondaryUrlFinder { exception.toString()); } catch (error, stackTrace) { _logger.severe( - 'AtLookup.findSecondary connection to root server failed with error: $error'); + 'findSecondaryUrl: connection to root server failed with error: $error'); _logger.severe(stackTrace); if (socket != null) { socket.destroy(); From 4037ccde7595ea0c17b90fcfdd12b7c9e21b632e Mon Sep 17 00:00:00 2001 From: Srie Teja Date: Mon, 4 Dec 2023 19:06:00 +0530 Subject: [PATCH 2/4] fix: update exception handling in at_auth_impl --- packages/at_auth/lib/src/at_auth_impl.dart | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/at_auth/lib/src/at_auth_impl.dart b/packages/at_auth/lib/src/at_auth_impl.dart index b6486564..8698e88e 100644 --- a/packages/at_auth/lib/src/at_auth_impl.dart +++ b/packages/at_auth/lib/src/at_auth_impl.dart @@ -58,7 +58,8 @@ class AtAuthImpl implements AtAuth { } if (atAuthKeys == null) { throw AtAuthenticationException( - 'keys either were not provided in the AtAuthRequest, or could not be read from provided keys file'); + 'keys either were not provided in the AtAuthRequest,' + ' or could not be read from provided keys file'); } enrollmentIdFromRequest ??= atAuthKeys.enrollmentId; var pkamPrivateKey = atAuthKeys.apkamPrivateKey; @@ -81,10 +82,12 @@ class AtAuthImpl implements AtAuth { var pkamResponse = (await pkamAuthenticator! .authenticate(enrollmentId: enrollmentIdFromRequest)); isPkamAuthenticated = pkamResponse.isSuccessful; - } on Exception catch (e) { - _logger.severe('Caught exception: $e'); + } on AtException catch (e) { + _logger.severe('Caught $e'); throw AtAuthenticationException( - 'Unable to authenticate- ${e.toString()}'); + 'Unable to authenticate | Cause: ${e.message}'); + } on Exception catch (e) { + throw AtAuthenticationException('Unable to authenticate | Cause: $e'); } _logger.finer( 'PKAM auth result: ${isPkamAuthenticated ? 'success' : 'failed'}'); @@ -108,7 +111,7 @@ class AtAuthImpl implements AtAuth { if (!cramAuthResult.isSuccessful) { throw AtAuthenticationException( 'Cram authentication failed. Please check the cram key' - ' and try again \n(or) contact support@atsign.com'); + ' and try again (or) contact support@atsign.com'); } //2. generate key pairs var atAuthKeys = _generateKeyPairs(atOnboardingRequest.authMode, @@ -122,7 +125,8 @@ class AtAuthImpl implements AtAuth { //3. update pkam public key through enrollment or manually based on app preference String? enrollmentIdFromServer; if (atOnboardingRequest.enableEnrollment) { - // server will update the apkam public key during enrollment.So don't have to manually update in this scenario. + // server will update the apkam public key during enrollment. + // So don't have to manually update in this scenario. enrollmentIdFromServer = await _sendOnboardingEnrollment( atOnboardingRequest, atAuthKeys, atLookUp!); atAuthKeys.enrollmentId = enrollmentIdFromServer; @@ -137,20 +141,20 @@ class AtAuthImpl implements AtAuth { _logger.finer('PkamPublicKey update result: $pkamUpdateResult'); } - //3. Close connection to server + //4. Close connection to server try { await (atLookUp as AtLookupImpl).close(); } on Exception catch (e) { _logger.severe('error while closing connection to server: $e'); } - //4. Init _atLookUp again and attempt pkam auth + //5. Init _atLookUp again and attempt pkam auth // atLookUp = AtLookupImpl(atOnboardingRequest.atSign, // atOnboardingRequest.rootDomain, atOnboardingRequest.rootPort); atLookUp!.atChops = atChops; var isPkamAuthenticated = false; - //5. Do pkam auth + //6. Do pkam auth pkamAuthenticator ??= PkamAuthenticator(atOnboardingRequest.atSign, atLookUp!); try { @@ -164,7 +168,8 @@ class AtAuthImpl implements AtAuth { throw AtAuthenticationException('Pkam auth returned false'); } - //5. If Pkam auth is success, update encryption public key to secondary and delete cram key from server + //7. If Pkam auth is success, update encryption public key to secondary + // and delete cram key from server final encryptionPublicKey = atAuthKeys.defaultEncryptionPublicKey; UpdateVerbBuilder updateBuilder = UpdateVerbBuilder() ..atKey = 'publickey' @@ -173,7 +178,8 @@ class AtAuthImpl implements AtAuth { ..sharedBy = atOnboardingRequest.atSign; String? encryptKeyUpdateResult = await atLookUp!.executeVerb(updateBuilder); _logger.info('Encryption public key update result $encryptKeyUpdateResult'); - // deleting cram secret from the keystore as cram auth is complete + + //8. Delete cram secret from the keystore as cram auth is complete DeleteVerbBuilder deleteBuilder = DeleteVerbBuilder() ..atKey = AtConstants.atCramSecret; String? deleteResponse = await atLookUp!.executeVerb(deleteBuilder); From eba6bb279fad04e8f4fe5763f9b8a0510970d759 Mon Sep 17 00:00:00 2001 From: Srie Teja Date: Mon, 4 Dec 2023 19:09:31 +0530 Subject: [PATCH 3/4] docs: pubspec and changelog for at_lookup --- packages/at_lookup/CHANGELOG.md | 2 ++ packages/at_lookup/pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/at_lookup/CHANGELOG.md b/packages/at_lookup/CHANGELOG.md index 58c6b83b..b8a36f6a 100644 --- a/packages/at_lookup/CHANGELOG.md +++ b/packages/at_lookup/CHANGELOG.md @@ -1,3 +1,5 @@ +## 3.0.42 +- fix: more informative exception messages ## 3.0.41 - feat: introduce methods cramAuthenticate and close into the AtLookup interface - deprecate: authenticate_cram() from AtLookupImpl. [cramAuthenticate should be used instead] diff --git a/packages/at_lookup/pubspec.yaml b/packages/at_lookup/pubspec.yaml index 7245a04b..93615a49 100644 --- a/packages/at_lookup/pubspec.yaml +++ b/packages/at_lookup/pubspec.yaml @@ -1,6 +1,6 @@ name: at_lookup description: A Dart library that contains the core commands that can be used with a secondary server (scan, update, lookup, llookup, plookup, etc.) -version: 3.0.38 +version: 3.0.42 repository: https://github.com/atsign-foundation/at_libraries homepage: https://atsign.com documentation: https://docs.atsign.com/ From 073e4b08d28318cef003dda103844b9c4e2028c1 Mon Sep 17 00:00:00 2001 From: Srie Teja Date: Mon, 4 Dec 2023 19:30:49 +0530 Subject: [PATCH 4/4] fix: revert wrapping of RootServerConnectivityException in AtConnectivityException --- .../lib/src/cache/cacheable_secondary_address_finder.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart b/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart index 38454e33..dc74cef9 100644 --- a/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart +++ b/packages/at_lookup/lib/src/cache/cacheable_secondary_address_finder.dart @@ -166,7 +166,7 @@ class SecondaryUrlFinder { _logger.severe('AtLookup.findSecondary for $atSign failed with $e' ' : ${retryDelaysMillis.length + 1} failures, giving up'); if (e is RootServerConnectivityException) { - throw AtConnectException( + throw RootServerConnectivityException( 'Unable to establish connection with root server.' ' Please check your internet connection and try again'); }