-
Notifications
You must be signed in to change notification settings - Fork 1
API Client のエラー処理の改善 #91
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| using BuffettCodeCommon.Exception; | ||
| using System.Collections.Immutable; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
|
|
||
| namespace BuffettCodeAPIClient | ||
| { | ||
| enum GetHttpStatusErrorCode | ||
|
akiomik marked this conversation as resolved.
|
||
| { | ||
| Forbidden = HttpStatusCode.Forbidden, | ||
| NotFound = HttpStatusCode.NotFound, | ||
| BadRequest = HttpStatusCode.BadRequest, | ||
| TooManyRequests = 429, | ||
| } | ||
|
|
||
| static class TestApiTokenErrorMessage | ||
| { | ||
| const string ExceedingTestingApiTickerLimist = @"{""message"":""Testing Apikey is only allowed to ticker ending with \""01\""""}"; | ||
| const string ExceedingTraialApiTickerLimit = "{\"message\":\"Trial request only supports ticker ending '01'\"}"; | ||
| const string TestingApiKeyIsNotAllowed = "{\"message\":\"Testing apikey is not allowed\"}"; | ||
| public static ImmutableHashSet<string> KnownErrorMessages = ImmutableHashSet.Create<string>(new string[] { ExceedingTestingApiTickerLimist, ExceedingTraialApiTickerLimit, TestingApiKeyIsNotAllowed }); | ||
| } | ||
|
|
||
| static class InvalidAPIKeyErrorMessage | ||
| { | ||
| public const string ApiGatewayDefault = "{\"message\":\"Forbidden\"}"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 名前にまよったけど、一番現実に即した名前を付けてあります |
||
|
|
||
| } | ||
|
|
||
|
|
||
| public class GetRequestErrorHandler | ||
| { | ||
| public static void Handle(ApiGetRequest request, HttpResponseMessage errorResponse) | ||
| { | ||
| switch ((int)errorResponse.StatusCode) | ||
| { | ||
| case (int)GetHttpStatusErrorCode.Forbidden: | ||
| throw CreateExceptionForForbidden(request, errorResponse); | ||
| case (int)GetHttpStatusErrorCode.NotFound: | ||
| throw new ResourceNotFoundException($"request={request}"); | ||
| case (int)GetHttpStatusErrorCode.TooManyRequests: | ||
| throw new DailyQuotaException($"request={request}"); | ||
| case (int)GetHttpStatusErrorCode.BadRequest: | ||
| throw CreateExceptionforBadRequest(request, | ||
| errorResponse); | ||
| default: | ||
| throw new BuffettCodeApiClientException($"request={request}"); | ||
| } | ||
| } | ||
|
|
||
| private static BuffettCodeApiClientException CreateExceptionForForbidden(ApiGetRequest request, HttpResponseMessage errorResponse) | ||
| { | ||
| switch (errorResponse.Content.ReadAsStringAsync().Result) | ||
| { | ||
| case InvalidAPIKeyErrorMessage.ApiGatewayDefault: | ||
| return new InvalidAPIKeyException($"request={request}"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ネストしてるやつは
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ですです |
||
| default: | ||
| return new ApiMonthlyLimitExceededException($"request={request}"); | ||
| } | ||
| } | ||
|
|
||
| private static BuffettCodeApiClientException CreateExceptionforBadRequest(ApiGetRequest request, HttpResponseMessage errorResponse) | ||
|
akiomik marked this conversation as resolved.
|
||
| { | ||
| if (TestApiTokenErrorMessage.KnownErrorMessages.Contains(errorResponse.Content.ReadAsStringAsync().Result)) | ||
| { | ||
| return new TestAPIConstraintException($"request={request}"); | ||
| } | ||
| else | ||
| { | ||
| return new BuffettCodeApiClientException($"request={request}"); | ||
|
Comment on lines
+66
to
+70
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こっちも return するようにしてあります |
||
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using BuffettCodeCommon.Exception; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.Collections.Generic; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
|
|
||
| namespace BuffettCodeAPIClient.Tests | ||
| { | ||
| [TestClass()] | ||
| public class GetRequestErrorHandlerTests | ||
| { | ||
|
|
||
| private readonly ApiGetRequest Request = new ApiGetRequest("dummy", new Dictionary<string, string>()); | ||
|
|
||
| private static HttpResponseMessage CreateResponseMessage(HttpStatusCode statusCode, string content) | ||
| { | ||
| var response = new HttpResponseMessage(statusCode); | ||
| response.Content = new StringContent(content); | ||
| return response; | ||
| } | ||
|
|
||
| [TestMethod()] | ||
| public void HandleInvalidApiKeyError() | ||
| { | ||
| var response = CreateResponseMessage(HttpStatusCode.Forbidden, "{\"message\":\"Forbidden\"}"); | ||
| Assert.ThrowsException<InvalidAPIKeyException>(() => GetRequestErrorHandler.Handle(Request, response)); | ||
| } | ||
|
|
||
| [TestMethod()] | ||
| public void HandleInvalidApiMonthlyLimitExceededException() | ||
| { | ||
| var response = CreateResponseMessage(HttpStatusCode.Forbidden, "dummy"); | ||
| Assert.ThrowsException<ApiMonthlyLimitExceededException>(() => GetRequestErrorHandler.Handle(Request, response)); | ||
| } | ||
|
|
||
| [TestMethod()] | ||
| public void HandleResourceNotFound() | ||
| { | ||
| var response = CreateResponseMessage(HttpStatusCode.NotFound, ""); | ||
| Assert.ThrowsException<ResourceNotFoundException>(() => GetRequestErrorHandler.Handle(Request, response)); | ||
| } | ||
|
|
||
|
|
||
| [TestMethod()] | ||
| public void HandleTestAPIConstraintException() | ||
| { | ||
| var response = CreateResponseMessage(HttpStatusCode.BadRequest, "{\"message\":\"Testing apikey is not allowed\"}"); | ||
| Assert.ThrowsException<TestAPIConstraintException>(() => GetRequestErrorHandler.Handle(Request, response)); | ||
| } | ||
|
|
||
| [TestMethod()] | ||
| public void HandleTestBadRequest() | ||
| { | ||
| var response = CreateResponseMessage(HttpStatusCode.BadRequest, "unknown error"); | ||
| Assert.ThrowsException<BuffettCodeApiClientException>(() => GetRequestErrorHandler.Handle(Request, response)); | ||
| } | ||
|
|
||
| [TestMethod()] | ||
| public void HandleTestDefaultException() | ||
| { | ||
| var response = CreateResponseMessage(HttpStatusCode.InternalServerError, "unknown error"); | ||
| Assert.ThrowsException<BuffettCodeApiClientException>(() => GetRequestErrorHandler.Handle(Request, response)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,13 @@ public InvalidAPIKeyException(string message) : base(message) { } | |
| public InvalidAPIKeyException(string message, Exception inner) : base(message, inner) { } | ||
| } | ||
|
|
||
| public class QuotaException : BuffettCodeApiClientException | ||
| public class DailyQuotaException : BuffettCodeApiClientException | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AWS Gateway の daily quota によるものなので、 |
||
| { | ||
| public QuotaException() : base() { } | ||
| public DailyQuotaException() : base() { } | ||
|
|
||
| public QuotaException(string message) : base(message) { } | ||
| public DailyQuotaException(string message) : base(message) { } | ||
|
|
||
| public QuotaException(string message, Exception inner) : base(message, inner) { } | ||
| public DailyQuotaException(string message, Exception inner) : base(message, inner) { } | ||
|
|
||
| } | ||
|
|
||
|
|
@@ -50,4 +50,14 @@ public ResourceNotFoundException(string message, Exception inner) : base(message | |
|
|
||
| } | ||
|
|
||
| public class ApiMonthlyLimitExceededException : BuffettCodeApiClientException | ||
| { | ||
| public ApiMonthlyLimitExceededException() : base() { } | ||
|
|
||
| public ApiMonthlyLimitExceededException(string message) : base(message) { } | ||
|
|
||
| public ApiMonthlyLimitExceededException(string message, Exception inner) : base(message, inner) { } | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build が不安定だったので(成果物がないって言われる)、
Setup系の build は各テストが終わってからにしますThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
見てる感じ、これでだいぶ安定した(なんでや・・・