diff --git a/lib/matrix_api.dart b/lib/matrix_api.dart index e918827b..b6e19ec9 100644 --- a/lib/matrix_api.dart +++ b/lib/matrix_api.dart @@ -30,6 +30,7 @@ export 'matrix_api/model/filter.dart'; export 'matrix_api/model/keys_query_response.dart'; export 'matrix_api/model/login_response.dart'; export 'matrix_api/model/login_types.dart'; +export 'matrix_api/model/matrix_connection_exception.dart'; export 'matrix_api/model/matrix_event.dart'; export 'matrix_api/model/matrix_exception.dart'; export 'matrix_api/model/matrix_keys.dart'; diff --git a/lib/matrix_api/matrix_api.dart b/lib/matrix_api/matrix_api.dart index 4e372e1f..c3dd5f04 100644 --- a/lib/matrix_api/matrix_api.dart +++ b/lib/matrix_api/matrix_api.dart @@ -30,6 +30,7 @@ import 'model/filter.dart'; import 'model/keys_query_response.dart'; import 'model/login_response.dart'; import 'model/login_types.dart'; +import 'model/matrix_connection_exception.dart'; import 'model/matrix_event.dart'; import 'model/matrix_exception.dart'; import 'model/matrix_keys.dart'; @@ -190,6 +191,9 @@ class MatrixApi { } catch (_) { // No-OP } + if (resp.statusCode >= 500 && resp.statusCode < 600) { + throw Exception(respBody); + } var jsonString = String.fromCharCodes(respBody.runes); if (jsonString.startsWith('[') && jsonString.endsWith(']')) { jsonString = '\{"chunk":$jsonString\}'; @@ -197,18 +201,15 @@ class MatrixApi { jsonResp = jsonDecode(jsonString) as Map; // May throw FormatException - if (resp.statusCode >= 400 && resp.statusCode < 500) { - // The server has responsed with an matrix related error. - var exception = MatrixException(resp); - - throw exception; - } _timeoutFactor = 1; - } on TimeoutException catch (_) { + } on TimeoutException catch (e, s) { _timeoutFactor *= 2; - rethrow; - } catch (_) { - rethrow; + throw MatrixConnectionException(e, s); + } catch (e, s) { + throw MatrixConnectionException(e, s); + } + if (resp.statusCode >= 400 && resp.statusCode < 500) { + throw MatrixException(resp); } return jsonResp; diff --git a/lib/matrix_api/model/matrix_connection_exception.dart b/lib/matrix_api/model/matrix_connection_exception.dart new file mode 100644 index 00000000..b45192e0 --- /dev/null +++ b/lib/matrix_api/model/matrix_connection_exception.dart @@ -0,0 +1,26 @@ +/* + * Famedly Matrix SDK + * Copyright (C) 2019, 2020 Famedly GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +class MatrixConnectionException implements Exception { + final dynamic original; + final StackTrace stackTrace; + MatrixConnectionException(this.original, this.stackTrace); + + @override + String toString() => original.toString(); +} diff --git a/lib/src/client.dart b/lib/src/client.dart index 7d8c0c97..f16073df 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -769,9 +769,12 @@ class Client extends MatrixApi { Logs.warning('The user has been logged out!'); clear(); } + } on MatrixConnectionException catch (e, s) { + Logs.warning('Synchronization connection failed: ${e.toString()}'); + onSyncError.add(SdkError(exception: e, stackTrace: s)); } catch (e, s) { if (!isLogged() || _disposed) return; - Logs.error('Error during processing events: ' + e.toString(), s); + Logs.error('Error during processing events: ${e.toString()}', s); onSyncError.add(SdkError( exception: e is Exception ? e : Exception(e), stackTrace: s)); } diff --git a/test/client_test.dart b/test/client_test.dart index 1f125c9d..b554f774 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -78,7 +78,7 @@ void main() { try { await matrix.checkHomeserver('https://fakeserver.wrongaddress'); - } on FormatException catch (exception) { + } on MatrixConnectionException catch (exception) { expect(exception != null, true); } await matrix.checkHomeserver('https://fakeserver.notexisting');