Skip to content

Commit

Permalink
Merge pull request #291 from opentween/fix-empty-user
Browse files Browse the repository at this point in the history
UserByScreenNameで空のユーザー情報が返ってきた場合にエラーとして扱う
  • Loading branch information
upsilon authored Jan 6, 2024
2 parents b8fb1c9 + 781fc24 commit bc8c76d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
==== Unreleased
* NEW: Cookie使用時の関連発言表示に対応
* FIX: APIリクエストのタイムアウト時に接続が切断されない場合がある不具合を修正
* FIX: 存在しないユーザーのプロフィールを取得しようとした場合のエラーが適切に処理されない不具合を修正

==== Ver 3.10.1(2023/12/23)
* FIX: OAuth 1.0a によるAPIアクセスに失敗する不具合を修正
Expand Down
25 changes: 25 additions & 0 deletions OpenTween.Tests/Api/GraphQL/UserByScreenNameRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,30 @@ public async Task Send_UserUnavailableTest()

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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": {}
}
25 changes: 19 additions & 6 deletions OpenTween/Api/GraphQL/UserByScreenNameRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,33 @@ public async Task<TwitterGraphqlUser> Send(IApiConnection apiConnection)

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);
}
}
}
Expand Down

0 comments on commit bc8c76d

Please sign in to comment.