From 2fa19a76b8f4ef72690f2d8ea0dd77ef2c5397da Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 13 Oct 2023 13:09:27 +0200 Subject: Error handling --- LibMatrix/Extensions/HttpClientExtensions.cs | 5 +++++ LibMatrix/Interfaces/EventContent.cs | 2 -- LibMatrix/RoomTypes/GenericRoom.cs | 10 ++++++++++ 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(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 GetStateOrNullAsync(string type, string stateKey = "") { + try { + return await GetStateAsync(type, stateKey); + } + catch (MatrixException e) { + if (e.ErrorCode == "M_NOT_FOUND") return default; + throw; + } + } + public async Task 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}"; -- cgit 1.4.1