about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-03-31 09:30:02 +0200
committerRory& <root@rory.gay>2025-03-31 09:30:22 +0200
commitfee33ee3ff27c3c89ff8a27701242b62334f8e58 (patch)
tree0b63d1beea6f7102e830487f937cdc376d305dfc
parentSplit out invite context, add empty filter constants (diff)
downloadLibMatrix-fee33ee3ff27c3c89ff8a27701242b62334f8e58.tar.xz
Propagate more HTTP errors, sync error handling callback
-rw-r--r--LibMatrix/Extensions/MatrixHttpClient.Single.cs15
-rw-r--r--LibMatrix/Helpers/SyncHelper.cs8
-rw-r--r--LibMatrix/Helpers/SyncStateResolver.cs6
3 files changed, 18 insertions, 11 deletions
diff --git a/LibMatrix/Extensions/MatrixHttpClient.Single.cs b/LibMatrix/Extensions/MatrixHttpClient.Single.cs

index afbe52f..bfc3f3b 100644 --- a/LibMatrix/Extensions/MatrixHttpClient.Single.cs +++ b/LibMatrix/Extensions/MatrixHttpClient.Single.cs
@@ -148,10 +148,10 @@ public class MatrixHttpClient { if (responseMessage.IsSuccessStatusCode) return responseMessage; //retry on gateway timeout - if (responseMessage.StatusCode == HttpStatusCode.GatewayTimeout) { - request.ResetSendStatus(); - return await SendAsync(request, cancellationToken); - } + // if (responseMessage.StatusCode == HttpStatusCode.GatewayTimeout) { + // request.ResetSendStatus(); + // return await SendAsync(request, cancellationToken); + // } //error handling var content = await responseMessage.Content.ReadAsStringAsync(cancellationToken); @@ -160,7 +160,12 @@ public class MatrixHttpClient { ErrorCode = "M_UNKNOWN", Error = "Unknown error, server returned no content" }; - if (!content.StartsWith('{')) throw new InvalidDataException("Encountered invalid data:\n" + content); + + // if (!content.StartsWith('{')) throw new InvalidDataException("Encountered invalid data:\n" + content); + if (!content.TrimStart().StartsWith('{')) { + responseMessage.EnsureSuccessStatusCode(); + throw new InvalidDataException("Encountered invalid data:\n" + content); + } //we have a matrix error MatrixException? ex; diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs
index babdd6f..9b1b921 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs
@@ -149,6 +149,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg catch (Exception e) { Console.WriteLine(e); logger?.LogError(e, "Failed to sync!\n{}", e.ToString()); + await Task.WhenAll(ExceptionHandlers.Select(x => x.Invoke(e)).ToList()); } return null; @@ -257,8 +258,13 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg /// </summary> public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; } = new(); + /// <summary> + /// Event fired when an exception is thrown + /// </summary> + public List<Func<Exception, Task>> ExceptionHandlers { get; } = new(); + private void Log(string message) { if (logger is null) Console.WriteLine(message); else logger.LogInformation(message); } -} +} \ No newline at end of file diff --git a/LibMatrix/Helpers/SyncStateResolver.cs b/LibMatrix/Helpers/SyncStateResolver.cs
index 35360d2..5d25561 100644 --- a/LibMatrix/Helpers/SyncStateResolver.cs +++ b/LibMatrix/Helpers/SyncStateResolver.cs
@@ -51,7 +51,7 @@ public class SyncStateResolver(AuthenticatedHomeserverGeneric homeserver, ILogge var keys = (await storageProvider.GetAllKeysAsync()).Where(x => !x.StartsWith("old/")).ToFrozenSet(); var count = keys.Count - 1; int total = count; - Console.WriteLine($"Found {count} entries to optimise."); + Console.WriteLine($"Found {count} entries to optimise in {totalSw.Elapsed}."); var merged = await initLoadTask; if (merged is null) return; @@ -110,10 +110,6 @@ public class SyncStateResolver(AuthenticatedHomeserverGeneric homeserver, ILogge #endif } - // var traceString = string.Join("\n", traces.Select(x => $"{x.Key}\t{x.Value.ToJson(indent: false)}")); - // var ms = new MemoryStream(Encoding.UTF8.GetBytes(traceString)); - // await storageProvider.SaveStreamAsync($"traces/{oldPath}", ms); - await storageProvider.SaveObjectAsync("init", merged); await Task.WhenAll(moveTasks);