Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

API Client のエラー処理の改善#91

Merged
shoe116 merged 1 commit intomasterfrom
improve_error_handling
Jul 17, 2022
Merged

API Client のエラー処理の改善#91
shoe116 merged 1 commit intomasterfrom
improve_error_handling

Conversation

@shoe116
Copy link
Copy Markdown
Contributor

@shoe116 shoe116 commented Jul 17, 2022

to close #51

Copy link
Copy Markdown
Contributor Author

@shoe116 shoe116 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akiomik お願いします

Comment thread BuffettCodeAPIClient/ApiClientCore.cs Outdated
Comment on lines +52 to +51
{
case (int)HttpStatusCode.Forbidden:
throw new InvalidAPIKeyException($"request={request}");
case (int)HttpStatusCode.NotFound:
throw new ResourceNotFoundException($"request={request}");
case 429: // Quota Error
throw new QuotaException($"request={request}");
default:
throw new BuffettCodeApiClientException($"request={request}");
}
GetErrorResponseHandler.Handle(request, response);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getのエラーレスポンスの処理はこいつに任せます

Copy link
Copy Markdown
Contributor

@akiomik akiomik Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ちょっと名前が直感的じゃなく感じた。たぶん Get がどこにかかっているのかわからないから、「エラーレスポンスのハンドラ」を取得するように見える

「GETリクエストのエラー時のハンドラ」なら、 GetReqeustErrorHandler がわかりやすいかなと思うんだけどどうだろう (同じ問題はあるけど、 GetRequest という言葉はあるからまだましかなと)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

わかる GetRequestError にするわ

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename 07c27bb

Forbidden = HttpStatusCode.Forbidden,
NotFound = HttpStatusCode.NotFound,
BadRequest = HttpStatusCode.BadRequest,
ExceedingApiGatewayQuota = 429,
Copy link
Copy Markdown
Contributor Author

@shoe116 shoe116 Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こいつは今使ってる HttpStatusCode に未定義でした、新しいやつには入ってるっぽいので .NET あげたら対応します
https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=net-6.0

Copy link
Copy Markdown
Contributor

@akiomik akiomik Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どのみち新しい.netframework入れたら TooManyRequests にするんだろうから、素直に TooManyRequests という名前にした方がいいと思う。もし ExceedingApiGatewayQuota を使いたいなら、 ForbiddenBadRequest をapplication specificな名前に変更した方がいいと思う

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ああ、たしかに。

Comment on lines +36 to +37
case "{\"message\":\"Forbidden\"}":
throw new InvalidAPIKeyException($"request={request}");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

403 かつこのメッセージの時のみ InvalidAPIKeyException

Comment on lines +47 to +48
if (TestApiTokenErrorMessage.KnownErrorMessages.Contains(errorResponse.Content.ReadAsStringAsync().Result))
{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メッセージ完全一致にしました

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSむずかしすぎる… ReadAsStringAsync() ってどうasyncなの?
普通のプログラミング言語なら、コールバックを使うなり await するなりでまたなきゃいけないと思うけど、呼び出しを見ると同期してるように見える

Copy link
Copy Markdown
Contributor Author

@shoe116 shoe116 Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これまじで・・・もちろん await しないと使えないんだけど、 Result すると Task じゃなくて string がそのまま返ってくるのよね(詳細は追ってない

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど

}
else
{
throw new BuffettCodeApiClientException($"request={request}");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

知らないメッセージの 400 は BuffettCodeApiClientException

Comment thread BuffettCodeAPIClient/GetErrorResponseHandler.cs Outdated
Comment thread BuffettCodeAPIClient/GetErrorResponseHandler.cs Outdated
Comment thread BuffettCodeAPIClient/GetRequestErrorHandler.cs
@shoe116 shoe116 changed the title Mod error handlings API Client のエラー処理の改善 Jul 17, 2022
Comment thread BuffettCodeAPIClient/GetRequestErrorHandler.cs Outdated
switch (errorResponse.Content.ReadAsStringAsync().Result)
{
case "{\"message\":\"Forbidden\"}":
return new InvalidAPIKeyException($"request={request}");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ネストしてるやつは return するようにしたのね

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ですです

Comment thread BuffettCodeAPIClient/GetRequestErrorHandler.cs
Comment thread BuffettCodeAPIClient/GetRequestErrorHandler.cs Outdated
Comment thread BuffettCodeAddinRibbon/CsvDownload/CsvDownloadExceptionHandler.cs
Comment thread BuffettCodeExcelFunctions/BCodeFunctionErrorHandler.cs
Comment thread BuffettCode.sln
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuffettCodeAddinRibbonTests", "BuffettCodeAddinRibbonTests\BuffettCodeAddinRibbonTests.csproj", "{00A6A593-A87D-4DD2-8B06-8A36CFC6C3AA}"
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "SetupAddinRibbon32", "SetupAddinRibbon32\SetupAddinRibbon32.wixproj", "{E1016C59-0448-4BFA-B89D-BB3C960F1F16}"
ProjectSection(ProjectDependencies) = postProject
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build が不安定だったので(成果物がないって言われる)、 Setup 系の build は各テストが終わってからにします

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見てる感じ、これでだいぶ安定した(なんでや・・・

switch (errorResponse.Content.ReadAsStringAsync().Result)
{
case "{\"message\":\"Forbidden\"}":
return new InvalidAPIKeyException($"request={request}");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ですです

Comment on lines +66 to +70
return new TestAPIConstraintException($"request={request}");
}
else
{
return new BuffettCodeApiClientException($"request={request}");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こっちも return するようにしてあります


static class InvalidAPIKeyErrorMessage
{
public const string ApiGatewayDefault = "{\"message\":\"Forbidden\"}";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名前にまよったけど、一番現実に即した名前を付けてあります

Comment thread BuffettCodeAddinRibbon/CsvDownload/CsvDownloadExceptionHandler.cs
Comment thread BuffettCodeAddinRibbon/CsvDownload/CsvDownloadExceptionHandler.cs
Comment thread BuffettCodeAddinRibbon/CsvDownload/CsvDownloadExceptionHandler.cs Outdated
}

public class QuotaException : BuffettCodeApiClientException
public class DailyQuotaException : BuffettCodeApiClientException
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS Gateway の daily quota によるものなので、 DailyQuotaException に rename しました

else if (bce is DailyQuotaException)
{
message = "APIの実行回数が上限に達しました";
message = "今日のAPIの実行回数が上限に達しました";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「今日の」をつけた 

@shoe116 shoe116 force-pushed the improve_error_handling branch from e9ea367 to 97b7b3a Compare July 17, 2022 07:00
Copy link
Copy Markdown
Contributor

@akiomik akiomik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

色々対応ありがとうございました 🙏

@shoe116 shoe116 merged commit e946ea7 into master Jul 17, 2022
@shoe116 shoe116 deleted the improve_error_handling branch July 17, 2022 07:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

エラーハンドリングの改善

2 participants