about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-10-13 13:09:27 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-10-13 13:09:27 +0200
commit2fa19a76b8f4ef72690f2d8ea0dd77ef2c5397da (patch)
tree8ac9d95c4077a552fcf7ad19231a56fe6edba93d /LibMatrix
parentFix alias thing? (diff)
downloadLibMatrix-2fa19a76b8f4ef72690f2d8ea0dd77ef2c5397da.tar.xz
Error handling
Diffstat (limited to '')
-rw-r--r--LibMatrix/Extensions/HttpClientExtensions.cs5
-rw-r--r--LibMatrix/Interfaces/EventContent.cs2
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs10
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}";