Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

refactor: Remove MatrixConnectionException #144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/matrix_api_lite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export 'src/model/events/room_key_request_content.dart';
export 'src/model/events/secret_storage_default_key_content.dart';
export 'src/model/events/secret_storage_key_content.dart';
export 'src/model/events/tombstone_content.dart';
export 'src/model/matrix_connection_exception.dart';
export 'src/model/matrix_event.dart';
export 'src/model/matrix_exception.dart';
export 'src/model/matrix_keys.dart';
Expand Down
60 changes: 29 additions & 31 deletions lib/src/matrix_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,39 +109,37 @@ class MatrixApi extends Api {

late http.Response resp;
Map<String, Object?>? jsonResp = <String, Object?>{};

switch (type) {
case RequestType.GET:
resp = await httpClient.get(url, headers: headers);
break;
case RequestType.POST:
resp = await httpClient.post(url, body: json, headers: headers);
break;
case RequestType.PUT:
resp = await httpClient.put(url, body: json, headers: headers);
break;
case RequestType.DELETE:
resp = await httpClient.delete(url, headers: headers);
break;
}
var respBody = resp.body;
try {
switch (type) {
case RequestType.GET:
resp = await httpClient.get(url, headers: headers);
break;
case RequestType.POST:
resp = await httpClient.post(url, body: json, headers: headers);
break;
case RequestType.PUT:
resp = await httpClient.put(url, body: json, headers: headers);
break;
case RequestType.DELETE:
resp = await httpClient.delete(url, headers: headers);
break;
}
var respBody = resp.body;
try {
respBody = utf8.decode(resp.bodyBytes);
} 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}';
}
jsonResp = jsonDecode(jsonString)
as Map<String, Object?>?; // May throw FormatException
} catch (e, s) {
throw MatrixConnectionException(e, s);
respBody = utf8.decode(resp.bodyBytes);
} 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}';
}
jsonResp = jsonDecode(jsonString)
as Map<String, Object?>?; // May throw FormatException

if (resp.statusCode >= 400 && resp.statusCode < 500) {
throw MatrixException(resp);
}
Expand Down
32 changes: 0 additions & 32 deletions lib/src/model/matrix_connection_exception.dart

This file was deleted.