OSDN Git Service

UserByScreenNameで空のユーザー情報が返ってきた場合にエラーとして扱う
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 6 Jan 2024 16:29:33 +0000 (01:29 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 6 Jan 2024 16:31:24 +0000 (01:31 +0900)
CHANGELOG.txt
OpenTween.Tests/Api/GraphQL/UserByScreenNameRequestTest.cs
OpenTween.Tests/Resources/Responses/UserByScreenName_Empty.json [new file with mode: 0644]
OpenTween/Api/GraphQL/UserByScreenNameRequest.cs

index 6306a2d..31c14d3 100644 (file)
@@ -3,6 +3,7 @@
 ==== Unreleased
  * NEW: Cookie使用時の関連発言表示に対応
  * FIX: APIリクエストのタイムアウト時に接続が切断されない場合がある不具合を修正
+ * FIX: 存在しないユーザーのプロフィールを取得しようとした場合のエラーが適切に処理されない不具合を修正
 
 ==== Ver 3.10.1(2023/12/23)
  * FIX: OAuth 1.0a によるAPIアクセスに失敗する不具合を修正
index 9c4e287..292fbad 100644 (file)
@@ -81,5 +81,30 @@ namespace OpenTween.Api.GraphQL
 
             mock.VerifyAll();
         }
+
+        [Fact]
+        public async Task Send_EmptyTest()
+        {
+            // ユーザーが存在しない場合にエラー情報を含まない空のオブジェクトが返されることがある
+            using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/UserByScreenName_Empty.json");
+
+            var mock = new Mock<IApiConnection>();
+            mock.Setup(x =>
+                    x.SendAsync(It.IsAny<IHttpRequest>())
+                )
+                .ReturnsAsync(apiResponse);
+
+            var request = new UserByScreenNameRequest
+            {
+                ScreenName = "==INVALID==",
+            };
+
+            var ex = await Assert.ThrowsAsync<WebApiException>(
+                () => request.Send(mock.Object)
+            );
+            Assert.Equal("User is not available.", ex.Message);
+
+            mock.VerifyAll();
+        }
     }
 }
diff --git a/OpenTween.Tests/Resources/Responses/UserByScreenName_Empty.json b/OpenTween.Tests/Resources/Responses/UserByScreenName_Empty.json
new file mode 100644 (file)
index 0000000..73cd5c3
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "data": {}
+}
index 4b3c8cc..f3d627d 100644 (file)
@@ -71,20 +71,33 @@ namespace OpenTween.Api.GraphQL
 
             ErrorResponse.ThrowIfError(rootElm);
 
-            var userElm = rootElm.XPathSelectElement("/data/user/result");
-            this.ThrowIfUserUnavailable(userElm);
+            try
+            {
+                var userElm = rootElm.XPathSelectElement("/data/user/result");
+                this.ThrowIfUserUnavailable(userElm);
 
-            return new(userElm);
+                return new(userElm);
+            }
+            catch (WebApiException ex)
+            {
+                ex.ResponseText = JsonUtils.JsonXmlToString(rootElm);
+                throw;
+            }
         }
 
-        private void ThrowIfUserUnavailable(XElement userElm)
+        private void ThrowIfUserUnavailable(XElement? userElm)
         {
+            if (userElm == null)
+            {
+                var errorText = "User is not available.";
+                throw new WebApiException(errorText);
+            }
+
             var typeName = userElm.Element("__typename")?.Value;
             if (typeName == "UserUnavailable")
             {
                 var errorText = userElm.Element("message")?.Value ?? "User is not available.";
-                var json = JsonUtils.JsonXmlToString(userElm);
-                throw new WebApiException(errorText, json);
+                throw new WebApiException(errorText);
             }
         }
     }