add FoxchatError type
This commit is contained in:
parent
f6629fbb33
commit
d34c41a126
7 changed files with 42 additions and 20 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Net.Http.Headers;
|
||||
using Foxchat.Core.Models;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
|
@ -9,9 +10,6 @@ public partial class RequestSigningService
|
|||
public const string USER_AGENT_HEADER = "User-Agent";
|
||||
public const string USER_AGENT = "Foxchat.NET";
|
||||
public const string DATE_HEADER = "Date";
|
||||
public const string CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
public const string CONTENT_TYPE_HEADER = "Content-Type";
|
||||
public const string CONTENT_TYPE = "application/json; charset=utf-8";
|
||||
|
||||
public const string SERVER_HEADER = "X-Foxchat-Server";
|
||||
public const string SIGNATURE_HEADER = "X-Foxchat-Signature";
|
||||
|
@ -28,19 +26,16 @@ public partial class RequestSigningService
|
|||
public async Task<T> RequestAsync<T>(HttpMethod method, string domain, string requestPath, string? userId = null, object? body = null)
|
||||
{
|
||||
var request = BuildHttpRequest(method, domain, requestPath, userId, body);
|
||||
_logger.Debug("Content length in header: '{ContentLength}'", request.Headers.Where(c => c.Key == "Content-Length"));
|
||||
var resp = await _httpClient.SendAsync(request);
|
||||
if (!resp.IsSuccessStatusCode)
|
||||
{
|
||||
var error = await resp.Content.ReadAsStringAsync();
|
||||
_logger.Error("Received {Status}, body: {Error}", resp.StatusCode, error);
|
||||
// TODO: replace this with specific exception type
|
||||
throw new Exception("oh no a request error");
|
||||
throw new FoxchatError.OutgoingFederationError($"Request to {domain}/{requestPath} returned an error", DeserializeObject<ApiError>(error));
|
||||
}
|
||||
|
||||
var bodyString = await resp.Content.ReadAsStringAsync();
|
||||
// TODO: replace this with specific exception type
|
||||
return DeserializeObject<T>(bodyString) ?? throw new Exception("oh no invalid json");
|
||||
return DeserializeObject<T>(bodyString)
|
||||
?? throw new FoxchatError.OutgoingFederationError($"Request to {domain}/{requestPath} returned invalid response body");
|
||||
}
|
||||
|
||||
private HttpRequestMessage BuildHttpRequest(HttpMethod method, string domain, string requestPath, string? userId = null, object? bodyData = null)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue