From 0ad12d2374fcbafc7498aed484c79feebbcb3fa6 Mon Sep 17 00:00:00 2001 From: dab246 Date: Wed, 10 Jun 2026 14:36:43 +0700 Subject: [PATCH] fix: map HandshakeException to ConnectionError in DioNoResponseErrorHandler to avoid silent failure --- .../dio_no_response_error_handler.dart | 8 ++ .../dio_no_response_error_handler_test.dart | 90 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/main/exceptions/thrower/dio_no_response_error_handler_test.dart diff --git a/lib/main/exceptions/thrower/dio_no_response_error_handler.dart b/lib/main/exceptions/thrower/dio_no_response_error_handler.dart index b421e184f..47b1ee7c9 100644 --- a/lib/main/exceptions/thrower/dio_no_response_error_handler.dart +++ b/lib/main/exceptions/thrower/dio_no_response_error_handler.dart @@ -27,6 +27,14 @@ class DioNoResponseErrorHandler { logWarning('RemoteExceptionThrower: socket error'); throw const SocketError(); } + if (underlyingError is HandshakeException) { + logError( + 'RemoteExceptionThrower: TLS handshake error — will_logout=false | ${underlyingError.message}', + exception: underlyingError, + stackTrace: stackTrace, + ); + throw ConnectionError(message: underlyingError.message); + } if (underlyingError is OAuthAuthorizationError) { throw underlyingError; } diff --git a/test/main/exceptions/thrower/dio_no_response_error_handler_test.dart b/test/main/exceptions/thrower/dio_no_response_error_handler_test.dart new file mode 100644 index 000000000..ce30c0d2d --- /dev/null +++ b/test/main/exceptions/thrower/dio_no_response_error_handler_test.dart @@ -0,0 +1,90 @@ +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:tmail_ui_user/main/exceptions/remote/network_exception.dart'; +import 'package:tmail_ui_user/main/exceptions/thrower/dio_no_response_error_handler.dart'; + +void main() { + late DioNoResponseErrorHandler handler; + + setUp(() { + handler = DioNoResponseErrorHandler(); + }); + + group('DioNoResponseErrorHandler', () { + test( + 'WHEN DioException type=connectionError\n' + 'THEN throws ConnectionError', + () { + final error = DioException( + requestOptions: RequestOptions(path: '/jmap'), + type: DioExceptionType.connectionError, + message: 'Connection refused', + ); + + expect( + () => handler.handle(error), + throwsA(isA()), + ); + }, + ); + + test( + 'WHEN DioException type=connectionTimeout\n' + 'THEN throws ConnectionTimeout', + () { + final error = DioException( + requestOptions: RequestOptions(path: '/jmap'), + type: DioExceptionType.connectionTimeout, + ); + + expect( + () => handler.handle(error), + throwsA(isA()), + ); + }, + ); + + test( + 'WHEN DioException type=unknown with SocketException\n' + 'THEN throws SocketError', + () { + final error = DioException( + requestOptions: RequestOptions(path: '/jmap'), + type: DioExceptionType.unknown, + error: const SocketException('Connection refused'), + ); + + expect( + () => handler.handle(error), + throwsA(isA()), + ); + }, + ); + + test( + 'WHEN DioException type=unknown with HandshakeException\n' + 'THEN throws ConnectionError (not logout, not Sentry error)\n' + 'AND OIDC session is preserved (no forced logout path)', + () { + final error = DioException( + requestOptions: RequestOptions(path: '/jmap'), + type: DioExceptionType.unknown, + error: const HandshakeException('Connection terminated during handshake'), + ); + + expect( + () => handler.handle(error), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('Connection terminated during handshake'), + ), + ), + ); + }, + ); + }); +}