add error handler middleware
This commit is contained in:
parent
41e4dda7b4
commit
7a0247b551
13 changed files with 177 additions and 46 deletions
|
@ -1,5 +1,4 @@
|
|||
using System.Net.Http.Headers;
|
||||
using Foxchat.Core.Models;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
|
@ -15,14 +14,6 @@ public partial class RequestSigningService
|
|||
public const string SIGNATURE_HEADER = "X-Foxchat-Signature";
|
||||
public const string USER_HEADER = "X-Foxchat-User";
|
||||
|
||||
private static readonly JsonSerializerSettings _jsonSerializerSettings = new()
|
||||
{
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
@ -30,17 +21,17 @@ public partial class RequestSigningService
|
|||
if (!resp.IsSuccessStatusCode)
|
||||
{
|
||||
var error = await resp.Content.ReadAsStringAsync();
|
||||
throw new ApiError.OutgoingFederationError($"Request to {domain}{requestPath} returned an error", DeserializeObject<Models.ApiError>(error));
|
||||
throw new ApiError.OutgoingFederationError($"Request to {domain}{requestPath} returned an error", JsonConvert.DeserializeObject<Models.Http.ApiError>(error));
|
||||
}
|
||||
|
||||
var bodyString = await resp.Content.ReadAsStringAsync();
|
||||
return DeserializeObject<T>(bodyString)
|
||||
return JsonConvert.DeserializeObject<T>(bodyString)
|
||||
?? throw new ApiError.OutgoingFederationError($"Request to {domain}{requestPath} returned invalid response body");
|
||||
}
|
||||
|
||||
private HttpRequestMessage BuildHttpRequest(HttpMethod method, string domain, string requestPath, string? userId = null, object? bodyData = null)
|
||||
{
|
||||
var body = bodyData != null ? SerializeObject(bodyData) : null;
|
||||
var body = bodyData != null ? JsonConvert.SerializeObject(bodyData) : null;
|
||||
|
||||
var now = _clock.GetCurrentInstant();
|
||||
var url = $"https://{domain}{requestPath}";
|
||||
|
@ -59,7 +50,4 @@ public partial class RequestSigningService
|
|||
|
||||
return request;
|
||||
}
|
||||
|
||||
public static string SerializeObject(object data) => JsonConvert.SerializeObject(data, _jsonSerializerSettings);
|
||||
public static T? DeserializeObject<T>(string data) => JsonConvert.DeserializeObject<T>(data, _jsonSerializerSettings);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue