From b69e4ad7ff165d15eaaa7e8ed02391cfda037ea7 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 7 Aug 2024 22:08:04 +0400 Subject: [PATCH] fix: Implement better error messaging for providers token check - Resolves #508 --- .../cloudflare/cloudflare_api.dart | 7 +++-- .../dns_providers/desec/desec_api.dart | 7 +++-- .../digital_ocean_dns_api.dart | 7 +++-- .../digital_ocean/digital_ocean_api.dart | 7 +++-- .../server_providers/hetzner/hetzner_api.dart | 7 +++-- .../initializing/dns_provider_form_cubit.dart | 2 +- .../server_installation_cubit.dart | 26 ++++++++++++++----- .../server_installation_repository.dart | 17 ++++++++---- .../initializing/dns_provider_picker.dart | 3 +-- .../initializing/server_provider_picker.dart | 3 +-- 10 files changed, 60 insertions(+), 26 deletions(-) diff --git a/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart b/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart index 433ccd02..4a5a450c 100644 --- a/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart +++ b/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart @@ -49,7 +49,7 @@ class CloudflareApi extends RestApiMap { Future> isApiTokenValid(final String token) async { bool isValid = false; Response? response; - String message = ''; + String? message; final Dio client = await getClient(); try { response = await client.get( @@ -77,9 +77,12 @@ class CloudflareApi extends RestApiMap { ); } + message = response.statusMessage; + if (response.statusCode == HttpStatus.ok) { isValid = true; } else if (response.statusCode == HttpStatus.unauthorized) { + message = 'initializing.provider_bad_key_error'; isValid = false; } else { throw Exception('code: ${response.statusCode}'); @@ -88,7 +91,7 @@ class CloudflareApi extends RestApiMap { return GenericResult( data: isValid, success: true, - message: response.statusMessage, + message: message, ); } diff --git a/lib/logic/api_maps/rest_maps/dns_providers/desec/desec_api.dart b/lib/logic/api_maps/rest_maps/dns_providers/desec/desec_api.dart index 7e069678..82a00e91 100644 --- a/lib/logic/api_maps/rest_maps/dns_providers/desec/desec_api.dart +++ b/lib/logic/api_maps/rest_maps/dns_providers/desec/desec_api.dart @@ -49,7 +49,7 @@ class DesecApi extends RestApiMap { Future> isApiTokenValid(final String token) async { bool isValid = false; Response? response; - String message = ''; + String? message; final Dio client = await getClient(); try { response = await client.get( @@ -78,9 +78,12 @@ class DesecApi extends RestApiMap { ); } + message = response.statusMessage; + if (response.statusCode == HttpStatus.ok) { isValid = true; } else if (response.statusCode == HttpStatus.unauthorized) { + message = 'initializing.provider_bad_key_error'; isValid = false; } else { throw Exception('code: ${response.statusCode}'); @@ -89,7 +92,7 @@ class DesecApi extends RestApiMap { return GenericResult( data: isValid, success: true, - message: response.statusMessage, + message: message, ); } diff --git a/lib/logic/api_maps/rest_maps/dns_providers/digital_ocean_dns/digital_ocean_dns_api.dart b/lib/logic/api_maps/rest_maps/dns_providers/digital_ocean_dns/digital_ocean_dns_api.dart index 090ea5f7..07276cb9 100644 --- a/lib/logic/api_maps/rest_maps/dns_providers/digital_ocean_dns/digital_ocean_dns_api.dart +++ b/lib/logic/api_maps/rest_maps/dns_providers/digital_ocean_dns/digital_ocean_dns_api.dart @@ -49,7 +49,7 @@ class DigitalOceanDnsApi extends RestApiMap { Future> isApiTokenValid(final String token) async { bool isValid = false; Response? response; - String message = ''; + String? message; final Dio client = await getClient(); try { response = await client.get( @@ -77,9 +77,12 @@ class DigitalOceanDnsApi extends RestApiMap { ); } + message = response.statusMessage; + if (response.statusCode == HttpStatus.ok) { isValid = true; } else if (response.statusCode == HttpStatus.unauthorized) { + message = 'initializing.provider_bad_key_error'; isValid = false; } else { throw Exception('code: ${response.statusCode}'); @@ -88,7 +91,7 @@ class DigitalOceanDnsApi extends RestApiMap { return GenericResult( data: isValid, success: true, - message: response.statusMessage, + message: message, ); } diff --git a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart index 224900a2..4fdbbb08 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart @@ -147,7 +147,7 @@ class DigitalOceanApi extends RestApiMap { Future> isApiTokenValid(final String token) async { bool isValid = false; Response? response; - String message = ''; + String? message; final Dio client = await getClient(); try { response = await client.get( @@ -175,9 +175,12 @@ class DigitalOceanApi extends RestApiMap { ); } + message = response.statusMessage; + if (response.statusCode == HttpStatus.ok) { isValid = true; } else if (response.statusCode == HttpStatus.unauthorized) { + message = 'initializing.provider_bad_key_error'; isValid = false; } else { throw Exception('code: ${response.statusCode}'); @@ -186,7 +189,7 @@ class DigitalOceanApi extends RestApiMap { return GenericResult( data: isValid, success: true, - message: response.statusMessage, + message: message, ); } diff --git a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner_api.dart b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner_api.dart index 4c1b146a..514e8402 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner_api.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner_api.dart @@ -195,7 +195,7 @@ class HetznerApi extends RestApiMap { Future> isApiTokenValid(final String token) async { bool isValid = false; Response? response; - String message = ''; + String? message; final Dio client = await getClient(); try { response = await client.get( @@ -223,9 +223,12 @@ class HetznerApi extends RestApiMap { ); } + message = response.statusMessage; + if (response.statusCode == HttpStatus.ok) { isValid = true; } else if (response.statusCode == HttpStatus.unauthorized) { + message = 'initializing.provider_bad_key_error'; isValid = false; } else { throw Exception('code: ${response.statusCode}'); @@ -234,7 +237,7 @@ class HetznerApi extends RestApiMap { return GenericResult( data: isValid, success: true, - message: response.statusMessage, + message: message, ); } diff --git a/lib/logic/cubit/forms/setup/initializing/dns_provider_form_cubit.dart b/lib/logic/cubit/forms/setup/initializing/dns_provider_form_cubit.dart index 084adb83..d2b2a59f 100644 --- a/lib/logic/cubit/forms/setup/initializing/dns_provider_form_cubit.dart +++ b/lib/logic/cubit/forms/setup/initializing/dns_provider_form_cubit.dart @@ -41,7 +41,7 @@ class DnsProviderFormCubit extends FormCubit { } if (!isKeyValid) { - apiKey.setError('initializing.dns_provider_bad_key_error'.tr()); + apiKey.setError('initializing.provider_bad_key_error'.tr()); } return isKeyValid; diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index ce4dea94..1b3da96c 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -92,11 +92,17 @@ class ServerInstallationCubit extends Cubit { if (!apiResponse.success) { getIt().showSnackBar( - 'initializing.could_not_connect'.tr(), + apiResponse.message ?? 'initializing.could_not_connect'.tr(), ); return null; } + if (!apiResponse.data) { + getIt().showSnackBar( + (apiResponse.message ?? 'initializing.provider_bad_key_error').tr(), + ); + } + return apiResponse.data; } @@ -110,11 +116,17 @@ class ServerInstallationCubit extends Cubit { if (!apiResponse.success) { getIt().showSnackBar( - 'initializing.could_not_connect'.tr(), + apiResponse.message ?? 'initializing.could_not_connect'.tr(), ); return null; } + if (!apiResponse.data) { + getIt().showSnackBar( + (apiResponse.message ?? 'initializing.provider_bad_key_error').tr(), + ); + } + return apiResponse.data; } @@ -800,11 +812,13 @@ class ServerInstallationCubit extends Cubit { if (serverDomain == null) { return; } - final isTokenValid = + final isTokenValidResult = await repository.validateDnsToken(token, serverDomain.domainName); - if (!isTokenValid) { - getIt() - .showSnackBar('recovering.domain_not_available_on_token'.tr()); + if (!isTokenValidResult.success) { + getIt().showSnackBar( + isTokenValidResult.message ?? + 'recovering.domain_not_available_on_token'.tr(), + ); return; } final dnsProviderType = await ServerApi( diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index cd586f7a..e0fdecfa 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -192,27 +192,34 @@ class ServerInstallationRepository { return server; } - Future validateDnsToken( + Future> validateDnsToken( final String token, final String domain, ) async { final result = await ProvidersController.currentDnsProvider!.tryInitApiByToken(token); if (!result.success) { - return false; + return result; } await setDnsApiToken(token); final domainResult = await ProvidersController.currentDnsProvider!.domainList(); if (!domainResult.success || domainResult.data.isEmpty) { - return false; + return GenericResult( + success: false, + data: false, + message: domainResult.message, + ); } await getIt().removeDnsProviderToken( getIt().dnsProviderCredentials.first, ); - return domainResult.data.any( - (final serverDomain) => serverDomain.domainName == domain, + return GenericResult( + success: true, + data: domainResult.data.any( + (final serverDomain) => serverDomain.domainName == domain, + ), ); } diff --git a/lib/ui/pages/setup/initializing/dns_provider_picker.dart b/lib/ui/pages/setup/initializing/dns_provider_picker.dart index efe3aeaf..93cae1d7 100644 --- a/lib/ui/pages/setup/initializing/dns_provider_picker.dart +++ b/lib/ui/pages/setup/initializing/dns_provider_picker.dart @@ -110,9 +110,8 @@ class ProviderInputDataPage extends StatelessWidget { CubitFormTextField( autofocus: true, formFieldCubit: providerCubit.apiKey, - textAlign: TextAlign.center, - scrollPadding: const EdgeInsets.only(bottom: 70), decoration: const InputDecoration( + border: OutlineInputBorder(), hintText: 'Provider API Token', ), ), diff --git a/lib/ui/pages/setup/initializing/server_provider_picker.dart b/lib/ui/pages/setup/initializing/server_provider_picker.dart index 5a842be1..e7ff49e1 100644 --- a/lib/ui/pages/setup/initializing/server_provider_picker.dart +++ b/lib/ui/pages/setup/initializing/server_provider_picker.dart @@ -118,9 +118,8 @@ class ProviderInputDataPage extends StatelessWidget { CubitFormTextField( autofocus: true, formFieldCubit: providerCubit.apiKey, - textAlign: TextAlign.center, - scrollPadding: const EdgeInsets.only(bottom: 70), decoration: const InputDecoration( + border: OutlineInputBorder(), hintText: 'Provider API Token', ), ),