Skip to content

Commit 0882a76

Browse files
NotionApiRateLimitException to expose Retry-After value
1 parent 4a2bdb5 commit 0882a76

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Src/Notion.Client/NotionApiException.cs

Lines changed: 16 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)
@@ -29,4 +29,19 @@ private NotionApiException(
2929

3030
public HttpStatusCode StatusCode { get; }
3131
}
32+
33+
public sealed class NotionApiRateLimitException : NotionApiException
34+
{
35+
public TimeSpan? RetryAfter { get; }
36+
37+
public NotionApiRateLimitException(
38+
HttpStatusCode statusCode,
39+
NotionAPIErrorCode? notionAPIErrorCode,
40+
string message,
41+
TimeSpan? retryAfter)
42+
: base(statusCode, notionAPIErrorCode, message)
43+
{
44+
RetryAfter = retryAfter;
45+
}
46+
}
3247
}

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)