|
23 | 23 | using System.Collections.Generic; |
24 | 24 | using System.IO; |
25 | 25 | using System.Linq; |
| 26 | +using System.Net; |
26 | 27 | using System.Net.Cache; |
27 | 28 | using System.Net.Http; |
28 | 29 | using System.Net.Http.Headers; |
29 | 30 | using System.Runtime.Serialization; |
30 | 31 | using System.Threading.Tasks; |
| 32 | +using OpenTween.Api.DataModel; |
31 | 33 |
|
32 | 34 | namespace OpenTween.Connection |
33 | 35 | { |
@@ -74,7 +76,8 @@ public async Task<T> GetAsync<T>(Uri uri, IEnumerable<KeyValuePair<string, strin |
74 | 76 | using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead) |
75 | 77 | .ConfigureAwait(false)) |
76 | 78 | { |
77 | | - response.EnsureSuccessStatusCode(); |
| 79 | + await this.CheckStatusCode(response) |
| 80 | + .ConfigureAwait(false); |
78 | 81 |
|
79 | 82 | using (var content = response.Content) |
80 | 83 | { |
@@ -120,7 +123,8 @@ public async Task<Stream> GetStreamAsync(Uri uri, IEnumerable<KeyValuePair<strin |
120 | 123 | using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead) |
121 | 124 | .ConfigureAwait(false)) |
122 | 125 | { |
123 | | - response.EnsureSuccessStatusCode(); |
| 126 | + await this.CheckStatusCode(response) |
| 127 | + .ConfigureAwait(false); |
124 | 128 |
|
125 | 129 | return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); |
126 | 130 | } |
@@ -162,7 +166,8 @@ public async Task<LazyJson<T>> PostLazyAsync<T>(HttpMethod method, Uri uri, IEnu |
162 | 166 | response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead) |
163 | 167 | .ConfigureAwait(false); |
164 | 168 |
|
165 | | - response.EnsureSuccessStatusCode(); |
| 169 | + await this.CheckStatusCode(response) |
| 170 | + .ConfigureAwait(false); |
166 | 171 |
|
167 | 172 | var result = new LazyJson<T>(response); |
168 | 173 | response = null; |
@@ -191,6 +196,35 @@ public void Dispose() |
191 | 196 | this.http.Dispose(); |
192 | 197 | } |
193 | 198 |
|
| 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 | + |
194 | 228 | private void InitializeHttpClient() |
195 | 229 | { |
196 | 230 | var innerHandler = Networking.CreateHttpClientHandler(); |
|
0 commit comments