Skip to content

Commit 2aa4961

Browse files
committed
Mastodon APIでのエラーレスポンスの詳細を例外メッセージに含める
1 parent 4c0c458 commit 2aa4961

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// OpenTween - Client of Twitter
2+
// Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+
// All rights reserved.
4+
//
5+
// This file is part of OpenTween.
6+
//
7+
// This program is free software; you can redistribute it and/or modify it
8+
// under the terms of the GNU General Public License as published by the Free
9+
// Software Foundation; either version 3 of the License, or (at your option)
10+
// any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful, but
13+
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
// for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License along
18+
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+
// Boston, MA 02110-1301, USA.
21+
22+
using System.Runtime.Serialization;
23+
24+
namespace OpenTween.Api.DataModel
25+
{
26+
[DataContract]
27+
public class MastodonError
28+
{
29+
[DataMember(Name = "error")]
30+
public string Error { get; set; }
31+
}
32+
}

OpenTween/Connection/MastodonApiConnection.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
using System.Collections.Generic;
2424
using System.IO;
2525
using System.Linq;
26+
using System.Net;
2627
using System.Net.Cache;
2728
using System.Net.Http;
2829
using System.Net.Http.Headers;
2930
using System.Runtime.Serialization;
3031
using System.Threading.Tasks;
32+
using OpenTween.Api.DataModel;
3133

3234
namespace OpenTween.Connection
3335
{
@@ -74,7 +76,8 @@ public async Task<T> GetAsync<T>(Uri uri, IEnumerable<KeyValuePair<string, strin
7476
using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
7577
.ConfigureAwait(false))
7678
{
77-
response.EnsureSuccessStatusCode();
79+
await this.CheckStatusCode(response)
80+
.ConfigureAwait(false);
7881

7982
using (var content = response.Content)
8083
{
@@ -120,7 +123,8 @@ public async Task<Stream> GetStreamAsync(Uri uri, IEnumerable<KeyValuePair<strin
120123
using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
121124
.ConfigureAwait(false))
122125
{
123-
response.EnsureSuccessStatusCode();
126+
await this.CheckStatusCode(response)
127+
.ConfigureAwait(false);
124128

125129
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
126130
}
@@ -162,7 +166,8 @@ public async Task<LazyJson<T>> PostLazyAsync<T>(HttpMethod method, Uri uri, IEnu
162166
response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
163167
.ConfigureAwait(false);
164168

165-
response.EnsureSuccessStatusCode();
169+
await this.CheckStatusCode(response)
170+
.ConfigureAwait(false);
166171

167172
var result = new LazyJson<T>(response);
168173
response = null;
@@ -191,6 +196,35 @@ public void Dispose()
191196
this.http.Dispose();
192197
}
193198

199+
private async Task CheckStatusCode(HttpResponseMessage response)
200+
{
201+
var statusCode = response.StatusCode;
202+
if (statusCode == HttpStatusCode.OK)
203+
return;
204+
205+
string responseText;
206+
using (var content = response.Content)
207+
{
208+
responseText = await content.ReadAsStringAsync()
209+
.ConfigureAwait(false);
210+
}
211+
212+
if (!string.IsNullOrWhiteSpace(responseText))
213+
{
214+
try
215+
{
216+
var error = MyCommon.CreateDataFromJson<MastodonError>(responseText);
217+
var errorText = error?.Error;
218+
219+
if (!string.IsNullOrEmpty(errorText))
220+
throw new WebApiException(errorText, responseText);
221+
}
222+
catch (SerializationException) { }
223+
}
224+
225+
throw new WebApiException(statusCode.ToString(), responseText);
226+
}
227+
194228
private void InitializeHttpClient()
195229
{
196230
var innerHandler = Networking.CreateHttpClientHandler();

OpenTween/OpenTween.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Compile Include="Api\DataModel\MastodonAccount.cs" />
7676
<Compile Include="Api\DataModel\MastodonApplication.cs" />
7777
<Compile Include="Api\DataModel\MastodonAttachment.cs" />
78+
<Compile Include="Api\DataModel\MastodonError.cs" />
7879
<Compile Include="Api\DataModel\MastodonInstance.cs" />
7980
<Compile Include="Api\DataModel\MastodonMention.cs" />
8081
<Compile Include="Api\DataModel\MastodonRegisteredApp.cs" />

0 commit comments

Comments
 (0)