11import 'package:bb_mobile/core/mempool/application/dtos/requests/set_custom_mempool_server_request.dart' ;
22import 'package:bb_mobile/core/mempool/domain/entities/mempool_server.dart' ;
3+ import 'package:bb_mobile/core/mempool/domain/errors/mempool_server_exception.dart' ;
34import 'package:bb_mobile/core/mempool/domain/ports/mempool_server_validator_port.dart' ;
45import 'package:bb_mobile/core/mempool/domain/repositories/mempool_server_repository.dart' ;
56import 'package:bb_mobile/core/mempool/domain/value_objects/mempool_server_network.dart' ;
@@ -8,20 +9,36 @@ import 'package:bb_mobile/core/mempool/domain/ports/environment_port.dart';
89import 'package:bb_mobile/core/settings/domain/settings_entity.dart' ;
910import 'package:bb_mobile/core/utils/logger.dart' ;
1011
12+ enum SetCustomMempoolServerError {
13+ sameAsDefault,
14+ validationFailed,
15+ saveFailed,
16+ unexpected,
17+ }
18+
1119class SetCustomMempoolServerResult {
1220 final bool isValid;
13- final String ? errorMessage;
21+ final SetCustomMempoolServerError ? errorType;
22+ final MempoolValidationErrorType ? validationErrorType;
1423
15- SetCustomMempoolServerResult ({required this .isValid, this .errorMessage});
24+ SetCustomMempoolServerResult ({
25+ required this .isValid,
26+ this .errorType,
27+ this .validationErrorType,
28+ });
1629
1730 factory SetCustomMempoolServerResult .success () {
1831 return SetCustomMempoolServerResult (isValid: true );
1932 }
2033
21- factory SetCustomMempoolServerResult .failure (String errorMessage) {
34+ factory SetCustomMempoolServerResult .failure (
35+ SetCustomMempoolServerError errorType, {
36+ MempoolValidationErrorType ? validationErrorType,
37+ }) {
2238 return SetCustomMempoolServerResult (
2339 isValid: false ,
24- errorMessage: errorMessage,
40+ errorType: errorType,
41+ validationErrorType: validationErrorType,
2542 );
2643 }
2744}
@@ -51,14 +68,20 @@ class SetCustomMempoolServerUsecase {
5168 isLiquid: request.isLiquid,
5269 );
5370
54- final customUrl = NormalizedMempoolUrl (request.url);
71+ final customUrl = NormalizedMempoolUrl (
72+ request.url,
73+ enableSsl: request.enableSsl,
74+ );
5575
5676 final defaultServerResult = await _fetchDefaultServerSafely (network);
5777 if (defaultServerResult != null ) {
58- final defaultUrl = NormalizedMempoolUrl (defaultServerResult.url);
78+ final defaultUrl = NormalizedMempoolUrl (
79+ defaultServerResult.url,
80+ enableSsl: defaultServerResult.enableSsl,
81+ );
5982 if (customUrl == defaultUrl) {
6083 return SetCustomMempoolServerResult .failure (
61- 'This URL is the same as the default server. Please use a different URL.' ,
84+ SetCustomMempoolServerError .sameAsDefault ,
6285 );
6386 }
6487 }
@@ -68,16 +91,22 @@ class SetCustomMempoolServerUsecase {
6891 final isValid = await _validator.validateServer (
6992 url: request.url,
7093 network: network,
94+ enableSsl: request.enableSsl,
7195 );
7296
7397 if (! isValid) {
7498 return SetCustomMempoolServerResult .failure (
75- 'Server validation failed: Unable to connect or invalid response' ,
99+ SetCustomMempoolServerError .validationFailed ,
76100 );
77101 }
102+ } on MempoolServerValidationException catch (e) {
103+ return SetCustomMempoolServerResult .failure (
104+ SetCustomMempoolServerError .validationFailed,
105+ validationErrorType: e.errorType,
106+ );
78107 } catch (e) {
79108 return SetCustomMempoolServerResult .failure (
80- 'Validation error: ${ e . toString ()}' ,
109+ SetCustomMempoolServerError .unexpected ,
81110 );
82111 }
83112 }
@@ -86,14 +115,16 @@ class SetCustomMempoolServerUsecase {
86115 final server = MempoolServer .createCustom (
87116 url: request.url,
88117 network: network,
118+ enableSsl: request.enableSsl,
89119 );
90120
91121 await _serverRepository.save (server);
92122
93123 return SetCustomMempoolServerResult .success ();
94124 } catch (e) {
125+ log.warning ('Failed to save mempool server: $e ' );
95126 return SetCustomMempoolServerResult .failure (
96- 'Failed to save server: ${ e . toString ()}' ,
127+ SetCustomMempoolServerError .saveFailed ,
97128 );
98129 }
99130 }
@@ -105,9 +136,7 @@ class SetCustomMempoolServerUsecase {
105136 return await _serverRepository.fetchDefaultServer (network);
106137 } catch (e) {
107138 // log the error for debugging but don't throw.
108- log.warning (
109- 'Could not fetch default mempool server for comparison: $e ' ,
110- );
139+ log.warning ('Could not fetch default mempool server for comparison: $e ' );
111140 return null ;
112141 }
113142 }
0 commit comments