about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix')
-rw-r--r--LibMatrix/LibMatrixException.cs7
-rw-r--r--LibMatrix/Services/HomeserverResolverService.cs16
2 files changed, 17 insertions, 6 deletions
diff --git a/LibMatrix/LibMatrixException.cs b/LibMatrix/LibMatrixException.cs

index 27cfc2a..e066d6c 100644 --- a/LibMatrix/LibMatrixException.cs +++ b/LibMatrix/LibMatrixException.cs
@@ -1,6 +1,7 @@ using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; using ArcaneLibs.Extensions; + // ReSharper disable MemberCanBePrivate.Global namespace LibMatrix; @@ -12,13 +13,15 @@ public class LibMatrixException : Exception { [JsonPropertyName("error")] public required string Error { get; set; } - public object GetAsObject() => new { errcode = ErrorCode, error = Error }; public string GetAsJson() => GetAsObject().ToJson(ignoreNull: true); public override string Message => $"{ErrorCode}: {ErrorCode switch { + "M_NOT_FOUND" => "The specified entity could not be found", "M_UNSUPPORTED" => "The requested feature is not supported", + "RLM_NO_CLIENT_URL" => "Could not resolve client URL", + "RLM_NO_SERVER_URL" => "Could not resolve server URL", _ => $"Unknown error: {GetAsObject().ToJson(ignoreNull: true)}" }}\nError: {Error}"; @@ -26,5 +29,7 @@ public class LibMatrixException : Exception { public static class ErrorCodes { public const string M_NOT_FOUND = "M_NOT_FOUND"; public const string M_UNSUPPORTED = "M_UNSUPPORTED"; + public const string RLM_NO_CLIENT_URL = "RLM_NO_CLIENT_URL"; + public const string RLM_NO_SERVER_URL = "RLM_NO_SERVER_URL"; } } \ No newline at end of file diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs
index ed1d2e3..700cfbb 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs
@@ -37,16 +37,22 @@ public class HomeserverResolverService { var res = new WellKnownUris(); if (client != null) - res.Client = (await client)?.TrimEnd('/') ?? throw new Exception($"Could not resolve client URL for {homeserver}."); + res.Client = (await client)?.TrimEnd('/') ?? throw new LibMatrixException() { + ErrorCode = LibMatrixException.ErrorCodes.RLM_NO_CLIENT_URL, + Error = $"Could not resolve client URL for {homeserver}." + }; if (server != null) - res.Server = (await server)?.TrimEnd('/') ?? throw new Exception($"Could not resolve server URL for {homeserver}."); + res.Server = (await server)?.TrimEnd('/') ?? throw new LibMatrixException() { + ErrorCode = LibMatrixException.ErrorCodes.RLM_NO_SERVER_URL, + Error = $"Could not resolve server URL for {homeserver}." + }; _logger.LogInformation("Resolved well-knowns for {hs}: {json}", homeserver, res.ToJson(indent: false)); return res; }); } - + private async Task<T?> GetFromJsonAsync<T>(string url) { try { return await _httpClient.GetFromJsonAsync<T>(url); @@ -56,7 +62,7 @@ public class HomeserverResolverService { return default; } } - + private async Task<string?> _tryResolveClientEndpoint(string homeserver) { ArgumentNullException.ThrowIfNull(homeserver); _logger.LogTrace("Resolving client well-known: {homeserver}", homeserver); @@ -71,7 +77,7 @@ public class HomeserverResolverService { } else if (homeserver.StartsWith("http://")) { clientWellKnown = await GetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); - + if (clientWellKnown is null && await MatrixHttpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) return homeserver; }