Error handling
3 files changed, 15 insertions, 2 deletions
diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/HttpClientExtensions.cs
index 2faf0d5..62e9a98 100644
--- a/LibMatrix/Extensions/HttpClientExtensions.cs
+++ b/LibMatrix/Extensions/HttpClientExtensions.cs
@@ -48,6 +48,11 @@ public class MatrixHttpClient : HttpClient {
//error handling
var content = await a.Content.ReadAsStringAsync(cancellationToken);
+ if (content.Length == 0)
+ throw new MatrixException() {
+ ErrorCode = "M_UNKNOWN",
+ Error = "Unknown error, server returned no content"
+ };
if (!content.StartsWith('{')) throw new InvalidDataException("Encountered invalid data:\n" + content);
//we have a matrix error
var ex = JsonSerializer.Deserialize<MatrixException>(content);
diff --git a/LibMatrix/Interfaces/EventContent.cs b/LibMatrix/Interfaces/EventContent.cs
index b21cfc7..37d316b 100644
--- a/LibMatrix/Interfaces/EventContent.cs
+++ b/LibMatrix/Interfaces/EventContent.cs
@@ -13,8 +13,6 @@ public abstract class EventContent {
[JsonPropertyName("m.in_reply_to")]
public EventInReplyTo? InReplyTo { get; set; }
-
-
public abstract class EventInReplyTo {
[JsonPropertyName("event_id")]
public string EventId { get; set; }
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs
index 1c0633c..106b2f6 100644
--- a/LibMatrix/RoomTypes/GenericRoom.cs
+++ b/LibMatrix/RoomTypes/GenericRoom.cs
@@ -66,6 +66,16 @@ public class GenericRoom {
}
}
+ public async Task<T?> GetStateOrNullAsync<T>(string type, string stateKey = "") {
+ try {
+ return await GetStateAsync<T>(type, stateKey);
+ }
+ catch (MatrixException e) {
+ if (e.ErrorCode == "M_NOT_FOUND") return default;
+ throw;
+ }
+ }
+
public async Task<MessagesResponse> GetMessagesAsync(string from = "", int limit = 10, string dir = "b",
string filter = "") {
var url = $"/_matrix/client/v3/rooms/{RoomId}/messages?from={from}&limit={limit}&dir={dir}";
|