feat(plugin): handle errors nicely

This commit is contained in:
Anna 2023-10-05 01:30:47 -04:00
parent 4d7f0599f3
commit 58eec899b4
Signed by: anna
GPG Key ID: D0943384CD9F87D1
1 changed files with 16 additions and 11 deletions

View File

@ -82,11 +82,11 @@ public class Plugin : IDalamudPlugin {
});
Task.Run(async () => {
var mem = new MemoryStream();
var gz = new GZipStream(mem, CompressionLevel.Optimal);
await gz.WriteAsync(Encoding.UTF8.GetBytes(json));
await gz.FlushAsync();
await gz.DisposeAsync();
using var mem = new MemoryStream();
await using (var gz = new GZipStream(mem, CompressionLevel.Optimal)) {
await gz.WriteAsync(Encoding.UTF8.GetBytes(json));
await gz.FlushAsync();
}
var req = new HttpRequestMessage(HttpMethod.Post, "http://localhost:30888/upload") {
Content = new ByteArrayContent(mem.ToArray()) {
@ -95,14 +95,19 @@ public class Plugin : IDalamudPlugin {
ContentEncoding = { "gzip" },
},
},
// Content = new StringContent(json) {
// Headers = { ContentType = new MediaTypeHeaderValue("application/json") },
// },
};
var resp = await this.Http.SendAsync(req);
var output = await resp.Content.ReadAsStringAsync();
Log.Info(output);
try {
var resp = await this.Http.SendAsync(req);
if (resp.IsSuccessStatusCode) {
Log.Verbose("uploaded successfully");
} else {
var output = await resp.Content.ReadAsStringAsync();
Log.Warning($"could not upload data ({resp.StatusCode}): {output}");
}
} catch (Exception ex) {
Log.Warning(ex, "could not upload data");
}
});
}
}