Add debug logs

This commit is contained in:
Christian Pauly 2019-06-12 13:43:14 +02:00
parent b3bb4e6531
commit 502623ad31
2 changed files with 22 additions and 14 deletions

View File

@ -38,7 +38,7 @@ class Client {
/// Optional persistent store for all data.
Store store;
Client(this.clientName) {
Client(this.clientName, {this.debug = false}) {
connection = Connection(this);
if (this.clientName != "testclient") store = Store(this);
@ -47,6 +47,9 @@ class Client {
});
}
/// Whether debug prints should be displayed.
final bool debug;
/// The required name for this client.
final String clientName;

View File

@ -65,7 +65,7 @@ class Connection {
/// Outside of rooms there are account updates like account_data or presences.
final StreamController<UserUpdate> onUserEvent =
new StreamController.broadcast();
new StreamController.broadcast();
/// Called when the login state e.g. user gets logged out.
final StreamController<LoginState> onLoginStateChanged =
@ -187,6 +187,8 @@ class Connection {
if (client.isLogged())
headers["Authorization"] = "Bearer ${client.accessToken}";
if (client.debug) print("[REQUEST $type] Action: $action, Data: $data");
http.Response resp;
try {
switch (type) {
@ -212,12 +214,15 @@ class Connection {
break;
}
} on TimeoutException catch (_) {
return ErrorResponse(
error: "No connection possible...", errcode: "TIMEOUT", request: resp?.request);
error: "No connection possible...",
errcode: "TIMEOUT",
request: resp?.request);
} catch (e) {
return ErrorResponse(
error: "No connection possible...", errcode: "NO_CONNECTION", request: resp.request);
error: "No connection possible...",
errcode: "NO_CONNECTION",
request: resp.request);
}
Map<String, dynamic> jsonResp;
@ -225,13 +230,17 @@ class Connection {
jsonResp = jsonDecode(resp.body) as Map<String, dynamic>;
} catch (e) {
return ErrorResponse(
error: "No connection possible...", errcode: "MALFORMED", request: resp.request);
error: "No connection possible...",
errcode: "MALFORMED",
request: resp.request);
}
if (jsonResp.containsKey("errcode") && jsonResp["errcode"] is String) {
if (jsonResp["errcode"] == "M_UNKNOWN_TOKEN") clear();
return ErrorResponse.fromJson(jsonResp, resp.request);
}
if (client.debug) print("[RESPONSE] ${jsonResp.toString()}");
return jsonResp;
}
@ -339,8 +348,7 @@ class Connection {
if (room["invite_state"] is Map<String, dynamic> &&
room["invite_state"]["events"] is List<dynamic>)
_handleRoomEvents(
id, room["invite_state"]["events"], "invite_state");
_handleRoomEvents(id, room["invite_state"]["events"], "invite_state");
if (room["timeline"] is Map<String, dynamic> &&
room["timeline"]["events"] is List<dynamic>)
@ -352,8 +360,7 @@ class Connection {
if (room["account_data"] is Map<String, dynamic> &&
room["account_data"]["events"] is List<dynamic>)
_handleRoomEvents(
id, room["account_data"]["events"], "account_data");
_handleRoomEvents(id, room["account_data"]["events"], "account_data");
});
}
@ -391,8 +398,7 @@ class Connection {
}
}
void _handleRoomEvents(
String chat_id, List<dynamic> events, String type) {
void _handleRoomEvents(String chat_id, List<dynamic> events, String type) {
for (num i = 0; i < events.length; i++) {
_handleEvent(events[i], chat_id, type);
}
@ -411,8 +417,7 @@ class Connection {
}
}
void _handleEvent(
Map<String, dynamic> event, String roomID, String type) {
void _handleEvent(Map<String, dynamic> event, String roomID, String type) {
if (event["type"] is String && event["content"] is dynamic) {
EventUpdate update = EventUpdate(
eventType: event["type"],