Skip to content

Commit 675a121

Browse files
Parse error response and add to exception object
1 parent b476e81 commit 675a121

File tree

4 files changed

+98
-33
lines changed

4 files changed

+98
-33
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Notion.Client
4+
{
5+
public enum NotionAPIErrorCode
6+
{
7+
[EnumMember(Value = "invalid_json")]
8+
InvalidJSON,
9+
10+
[EnumMember(Value = "invalid_request_url")]
11+
InvalidRequestURL,
12+
13+
[EnumMember(Value = "invalid_request")]
14+
InvalidRequest,
15+
16+
[EnumMember(Value = "validation_error")]
17+
ValidationError,
18+
19+
[EnumMember(Value = "missing_version")]
20+
MissingVersion,
21+
22+
[EnumMember(Value = "unauthorized")]
23+
Unauthorized,
24+
25+
[EnumMember(Value = "restricted_resource")]
26+
RestrictedResource,
27+
28+
[EnumMember(Value = "object_not_found")]
29+
ObjectNotFound,
30+
31+
[EnumMember(Value = "conflict_error")]
32+
ConflictError,
33+
34+
[EnumMember(Value = "rate_limited")]
35+
RateLimited,
36+
37+
[EnumMember(Value = "internal_server_error")]
38+
InternalServerError,
39+
40+
[EnumMember(Value = "service_unavailable")]
41+
ServiceUnavailable,
42+
}
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Notion.Client
2+
{
3+
class NotionApiErrorResponse
4+
{
5+
public NotionAPIErrorCode ErrorCode { get; set; }
6+
public string Message { get; set; }
7+
}
8+
}

Src/Notion.Client/NotionApiException.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,32 @@
33

44
namespace Notion.Client
55
{
6-
class NotionApiException : Exception
6+
public class NotionApiException : Exception
77
{
8-
public NotionApiException(HttpStatusCode statusCode, string message) : this(statusCode, message, null)
8+
public NotionApiException(HttpStatusCode statusCode, NotionAPIErrorCode? notionAPIErrorCode, string message)
9+
: this(statusCode, notionAPIErrorCode, message, null)
910
{
1011
}
1112

12-
public NotionApiException(HttpStatusCode statusCode, string message, Exception innerException) : base(message, innerException)
13+
public NotionApiException(HttpStatusCode statusCode, string message)
14+
: this(statusCode, null, message, null)
1315
{
16+
}
17+
18+
public NotionApiException(
19+
HttpStatusCode statusCode,
20+
NotionAPIErrorCode? notionAPIErrorCode,
21+
string message,
22+
Exception innerException) : base(message, innerException)
23+
{
24+
NotionAPIErrorCode = notionAPIErrorCode;
25+
StatusCode = statusCode;
26+
1427
Data.Add("StatusCode", statusCode);
28+
Data.Add("NotionApiErrorCode", NotionAPIErrorCode);
1529
}
30+
31+
public NotionAPIErrorCode? NotionAPIErrorCode { get; }
32+
public HttpStatusCode StatusCode { get; }
1633
}
1734
}

Src/Notion.Client/RestClient/RestClient.cs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,26 @@ public async Task<T> GetAsync<T>(
5454

5555
var response = await SendAsync(requestUri, HttpMethod.Get, headers, cancellationToken: cancellationToken);
5656

57-
if (response.IsSuccessStatusCode)
57+
return await response.ParseStreamAsync<T>(serializerSettings);
58+
}
59+
60+
private static async Task<Exception> BuildException(HttpResponseMessage response)
61+
{
62+
var errorBody = await response.Content.ReadAsStringAsync();
63+
64+
NotionApiErrorResponse errorResponse = null;
65+
if (!string.IsNullOrWhiteSpace(errorBody))
5866
{
59-
return await response.ParseStreamAsync<T>(serializerSettings);
67+
try
68+
{
69+
errorResponse = JsonConvert.DeserializeObject<NotionApiErrorResponse>(errorBody);
70+
}
71+
catch
72+
{
73+
}
6074
}
6175

62-
var message = !string.IsNullOrWhiteSpace(response.ReasonPhrase)
63-
? response.ReasonPhrase
64-
: await response.Content.ReadAsStringAsync();
65-
66-
throw new NotionApiException(response.StatusCode, message);
76+
return new NotionApiException(response.StatusCode, errorResponse?.ErrorCode, errorResponse.Message);
6777
}
6878

6979
private async Task<HttpResponseMessage> SendAsync(
@@ -84,7 +94,14 @@ private async Task<HttpResponseMessage> SendAsync(
8494

8595
attachContent?.Invoke(httpRequest);
8696

87-
return await _httpClient.SendAsync(httpRequest, cancellationToken);
97+
var response = await _httpClient.SendAsync(httpRequest, cancellationToken);
98+
99+
if (!response.IsSuccessStatusCode)
100+
{
101+
throw await BuildException(response);
102+
}
103+
104+
return response;
88105
}
89106

90107
private static void AddHeaders(HttpRequestMessage request, IDictionary<string, string> headers)
@@ -114,16 +131,7 @@ void AttachContent(HttpRequestMessage httpRequest)
114131

115132
var response = await SendAsync(requestUri, HttpMethod.Post, headers, AttachContent, cancellationToken: cancellationToken);
116133

117-
if (response.IsSuccessStatusCode)
118-
{
119-
return await response.ParseStreamAsync<T>(serializerSettings);
120-
}
121-
122-
var message = !string.IsNullOrWhiteSpace(response.ReasonPhrase)
123-
? response.ReasonPhrase
124-
: await response.Content.ReadAsStringAsync();
125-
126-
throw new NotionApiException(response.StatusCode, message);
134+
return await response.ParseStreamAsync<T>(serializerSettings);
127135
}
128136

129137
public async Task<T> PatchAsync<T>(string uri, object body, IDictionary<string, string> queryParams = null, IDictionary<string, string> headers = null, JsonSerializerSettings serializerSettings = null, CancellationToken cancellationToken = default)
@@ -140,18 +148,7 @@ void AttachContent(HttpRequestMessage httpRequest)
140148

141149
var response = await SendAsync(requestUri, new HttpMethod("PATCH"), headers, AttachContent, cancellationToken: cancellationToken);
142150

143-
if (response.IsSuccessStatusCode)
144-
{
145-
return await response.ParseStreamAsync<T>(serializerSettings);
146-
}
147-
148-
var message = !string.IsNullOrWhiteSpace(response.ReasonPhrase)
149-
? response.ReasonPhrase
150-
: await response.Content.ReadAsStringAsync();
151-
152-
var errorMessage = await response.Content.ReadAsStringAsync();
153-
154-
throw new NotionApiException(response.StatusCode, message);
151+
return await response.ParseStreamAsync<T>(serializerSettings);
155152
}
156153

157154
private HttpClient EnsureHttpClient()

0 commit comments

Comments
 (0)