feat: Implement error handling for server installation

Now user gets notified when connection error occurs
This commit is contained in:
NaiJi 2022-11-29 19:21:36 +04:00
parent 1dfd2180d2
commit 65f5d987e2
6 changed files with 71 additions and 22 deletions

View file

@ -113,7 +113,7 @@ class CloudflareApi extends DnsProviderApi {
}
@override
Future<void> removeSimilarRecords({
Future<APIGenericResult<void>> removeSimilarRecords({
required final ServerDomain domain,
final String? ip4,
}) async {
@ -139,9 +139,16 @@ class CloudflareApi extends DnsProviderApi {
await Future.wait(allDeleteFutures);
} catch (e) {
print(e);
return APIGenericResult(
success: false,
data: null,
message: e.toString(),
);
} finally {
close(client);
}
return APIGenericResult(success: true, data: null);
}
@override
@ -183,7 +190,7 @@ class CloudflareApi extends DnsProviderApi {
}
@override
Future<void> createMultipleDnsRecords({
Future<APIGenericResult<void>> createMultipleDnsRecords({
required final ServerDomain domain,
final String? ip4,
}) async {
@ -206,9 +213,18 @@ class CloudflareApi extends DnsProviderApi {
} on DioError catch (e) {
print(e.message);
rethrow;
} catch (e) {
print(e);
return APIGenericResult(
success: false,
data: null,
message: e.toString(),
);
} finally {
close(client);
}
return APIGenericResult(success: true, data: null);
}
List<DnsRecord> projectDnsRecords(

View file

@ -14,11 +14,11 @@ abstract class DnsProviderApi extends ApiMap {
Future<List<DnsRecord>> getDnsRecords({
required final ServerDomain domain,
});
Future<void> removeSimilarRecords({
Future<APIGenericResult<void>> removeSimilarRecords({
required final ServerDomain domain,
final String? ip4,
});
Future<void> createMultipleDnsRecords({
Future<APIGenericResult<void>> createMultipleDnsRecords({
required final ServerDomain domain,
final String? ip4,
});

View file

@ -790,11 +790,13 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi {
}
@override
Future<void> createReverseDns({
Future<APIGenericResult<void>> createReverseDns({
required final ServerHostingDetails serverDetails,
required final ServerDomain domain,
}) async {
/// TODO remove from provider interface
const bool success = true;
return APIGenericResult(success: success, data: null);
}
@override

View file

@ -788,7 +788,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
}
@override
Future<void> createReverseDns({
Future<APIGenericResult<void>> createReverseDns({
required final ServerHostingDetails serverDetails,
required final ServerDomain domain,
}) async {
@ -803,8 +803,15 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
);
} catch (e) {
print(e);
return APIGenericResult(
success: false,
data: null,
message: e.toString(),
);
} finally {
close(client);
}
return APIGenericResult(success: true, data: null);
}
}

View file

@ -38,7 +38,7 @@ abstract class ServerProviderApi extends ApiMap {
required final String domainName,
required final String serverType,
});
Future<void> createReverseDns({
Future<APIGenericResult<void>> createReverseDns({
required final ServerHostingDetails serverDetails,
required final ServerDomain domain,
});

View file

@ -334,21 +334,9 @@ class ServerInstallationRepository {
final ServerProviderApi serverApi =
ApiController.currentServerProviderApiFactory!.getServerProvider();
await dnsProviderApi.removeSimilarRecords(
ip4: serverDetails.ip4,
domain: domain,
);
try {
await dnsProviderApi.createMultipleDnsRecords(
ip4: serverDetails.ip4,
domain: domain,
);
} on DioError catch (e) {
void showDomainErrorPopUp(final String error) {
showPopUpAlert(
alertTitle: e.response!.data['errors'][0]['code'] == 1038
? 'modals.you_cant_use_this_api'.tr()
: 'domain.error'.tr(),
alertTitle: error,
description: 'modals.delete_server_volume'.tr(),
cancelButtonOnPressed: onCancel,
actionButtonTitle: 'basis.delete'.tr(),
@ -359,14 +347,50 @@ class ServerInstallationRepository {
onCancel();
},
);
}
final APIGenericResult removingResult =
await dnsProviderApi.removeSimilarRecords(
ip4: serverDetails.ip4,
domain: domain,
);
if (!removingResult.success) {
showDomainErrorPopUp('domain.error'.tr());
return false;
}
await serverApi.createReverseDns(
bool createdSuccessfully = false;
String errorMessage = 'domain.error'.tr();
try {
final APIGenericResult createResult =
await dnsProviderApi.createMultipleDnsRecords(
ip4: serverDetails.ip4,
domain: domain,
);
createdSuccessfully = createResult.success;
} on DioError catch (e) {
if (e.response!.data['errors'][0]['code'] == 1038) {
errorMessage = 'modals.you_cant_use_this_api'.tr();
}
}
if (!createdSuccessfully) {
showDomainErrorPopUp(errorMessage);
return false;
}
final APIGenericResult createReverseResult =
await serverApi.createReverseDns(
serverDetails: serverDetails,
domain: domain,
);
if (!createReverseResult.success) {
showDomainErrorPopUp(errorMessage);
return false;
}
return true;
}