8000 Expose Retry-After value by KoditkarVedant · Pull Request #382 · notion-dotnet/notion-sdk-net · GitHub
[go: up one dir, main page]

Skip to content

Expose Retry-After value #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Src/Notion.Client/NotionApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Notion.Client
{
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public sealed class NotionApiException : Exception
public class NotionApiException : Exception
{
public NotionApiException(HttpStatusCode statusCode, NotionAPIErrorCode? notionAPIErrorCode, string message)
: this(statusCode, notionAPIErrorCode, message, null)
Expand All @@ -21,6 +21,11 @@ private NotionApiException(
NotionAPIErrorCode = notionAPIErrorCode;
StatusCode = statusCode;

InitializeData();
}

private void InitializeData()
{
Data.Add("StatusCode", StatusCode);
Data.Add("NotionApiErrorCode", NotionAPIErrorCode);
}
Expand Down
22 changes: 22 additions & 0 deletions Src/Notion.Client/NotionApiRateLimitException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Net;

namespace Notion.Client
{
public sealed class NotionApiRateLimitException : NotionApiException
{
public TimeSpan? RetryAfter { get; }

public NotionApiRateLimitException(
HttpStatusCode statusCode,
NotionAPIErrorCode? notionAPIErrorCode,
string message,
TimeSpan? retryAfter)
: base(statusCode, notionAPIErrorCode, message)
{
RetryAfter = retryAfter;

Data.Add("RetryAfter", retryAfter);
}
}
}
11 changes: 11 additions & 0 deletions Src/Notion.Client/RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ private static async Task<Exception> BuildException(HttpResponseMessage response
try
{
errorResponse = JsonConvert.DeserializeObject<NotionApiErrorResponse>(errorBody);

if (errorResponse.ErrorCode == NotionAPIErrorCode.RateLimited)
{
var retryAfter = response.Headers.RetryAfter.Delta;
return new NotionApiRateLimitException(
response.StatusCode,
errorResponse.ErrorCode,
errorResponse.Message,
retryAfter
);
}
}
catch (Exception ex)
{
Expand Down
0