Skip to content

Commit 37b74dd

Browse files
Merge pull request #382 from notion-dotnet/253-expose-retry-after-value
Expose Retry-After value
2 parents bec3f62 + ae9ec8c commit 37b74dd

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

Src/Notion.Client/NotionApiException.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Notion.Client
66
{
77
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
8-
public sealed class NotionApiException : Exception
8+
public class NotionApiException : Exception
99
{
1010
public NotionApiException(HttpStatusCode statusCode, NotionAPIErrorCode? notionAPIErrorCode, string message)
1111
: this(statusCode, notionAPIErrorCode, message, null)
@@ -21,6 +21,11 @@ private NotionApiException(
2121
NotionAPIErrorCode = notionAPIErrorCode;
2222
StatusCode = statusCode;
2323

24+
InitializeData();
25+
}
26+
27+
private void InitializeData()
28+
{
2429
Data.Add("StatusCode", StatusCode);
2530
Data.Add("NotionApiErrorCode", NotionAPIErrorCode);
2631
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Net;
3+
4+
namespace Notion.Client
5+
{
6+
public sealed class NotionApiRateLimitException : NotionApiException
7+
{
8+
public TimeSpan? RetryAfter { get; }
9+
10+
public NotionApiRateLimitException(
11+
HttpStatusCode statusCode,
12+
NotionAPIErrorCode? notionAPIErrorCode,
13+
string message,
14+
TimeSpan? retryAfter)
15+
: base(statusCode, notionAPIErrorCode, message)
16+
{
17+
RetryAfter = retryAfter;
18+
19+
Data.Add("RetryAfter", retryAfter);
20+
}
21+
}
22+
}

Src/Notion.Client/RestClient/RestClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ private static async Task<Exception> BuildException(HttpResponseMessage response
112112
try
113113
{
114114
errorResponse = JsonConvert.DeserializeObject<NotionApiErrorResponse>(errorBody);
115+
116+
if (errorResponse.ErrorCode == NotionAPIErrorCode.RateLimited)
117+
{
118+
var retryAfter = response.Headers.RetryAfter.Delta;
119+
return new NotionApiRateLimitException(
120+
response.StatusCode,
121+
errorResponse.ErrorCode,
122+
errorResponse.Message,
123+
retryAfter
124+
);
125+
}
115126
}
116127
catch (Exception ex)
117128
{

0 commit comments

Comments
 (0)