refactor: Connection error handling
This commit is contained in:
parent
955fb747c2
commit
f9f18641e3
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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<String, dynamic>; // 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;
|
||||
|
|
|
|||
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
class MatrixConnectionException implements Exception {
|
||||
final dynamic original;
|
||||
final StackTrace stackTrace;
|
||||
MatrixConnectionException(this.original, this.stackTrace);
|
||||
|
||||
@override
|
||||
String toString() => original.toString();
|
||||
}
|
||||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Reference in New Issue