forked from kookxiang/jellyfin-plugin-bangumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBangumiApi.Exception.cs
53 lines (45 loc) · 1.47 KB
/
BangumiApi.Exception.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
namespace Jellyfin.Plugin.Bangumi;
public partial class BangumiApi
{
private class ServerException : Exception
{
private static readonly JsonSerializerOptions Options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public readonly HttpStatusCode StatusCode;
private ServerException(HttpStatusCode status, string message) : base(message)
{
StatusCode = status;
}
public static async Task ThrowFrom(HttpResponseInfo response)
{
var content = "<empty>";
var exception = new Exception($"unknown response from bangumi server: {content}");
try
{
using var stream = new StreamReader(response.Content);
content = await stream.ReadToEndAsync();
var result = JsonSerializer.Deserialize<Response>(content, Options);
if (result?.Title != null)
exception = new ServerException(response.StatusCode, $"{result.Title}: {result.Description}");
}
catch (Exception)
{
// ignored
}
throw exception;
}
}
private class Response
{
public string Title { get; set; } = "";
public string Description { get; set; } = "";
}
}