Skip to content

Commit d9d1aaa

Browse files
committed
GCM: Add response body to generic exceptions
1 parent 7549b97 commit d9d1aaa

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

PushSharp.Google/Exceptions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ namespace PushSharp.Google
55
{
66
public class GcmConnectionException : Exception
77
{
8-
public GcmConnectionException (string msg ) : base (msg)
8+
public GcmConnectionException (string msg) : base (msg)
99
{
1010
}
11+
12+
public GcmConnectionException (string msg, string description) : base (msg)
13+
{
14+
Description = description;
15+
}
16+
17+
public string Description { get; private set; }
1118
}
1219

1320
public class GcmMulticastResultException : Exception

PushSharp.Google/GcmServiceConnection.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ public async Task Send (GcmNotification notification)
5858
var response = await http.PostAsync (Configuration.GcmUrl, content);
5959

6060
if (response.IsSuccessStatusCode) {
61-
await processResponseOk (response, notification);
61+
await processResponseOk (response, notification).ConfigureAwait (false);
6262
} else {
63-
var body = await response.Content.ReadAsStringAsync ();
64-
processResponseError (response, notification);
63+
await processResponseError (response, notification).ConfigureAwait (false);
6564
}
6665
}
6766

@@ -168,14 +167,20 @@ async Task processResponseOk (HttpResponseMessage httpResponse, GcmNotification
168167
throw multicastResult;
169168
}
170169

171-
void processResponseError (HttpResponseMessage httpResponse, GcmNotification notification)
170+
async Task processResponseError (HttpResponseMessage httpResponse, GcmNotification notification)
172171
{
172+
string responseBody = null;
173+
174+
try {
175+
responseBody = await httpResponse.Content.ReadAsStringAsync ().ConfigureAwait (false);
176+
} catch { }
177+
173178
//401 bad auth token
174179
if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
175180
throw new UnauthorizedAccessException ("GCM Authorization Failed");
176181

177182
if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
178-
throw new GcmConnectionException ("HTTP 400 Bad Request");
183+
throw new GcmConnectionException ("HTTP 400 Bad Request", responseBody);
179184

180185
if ((int)httpResponse.StatusCode >= 500 && (int)httpResponse.StatusCode < 600) {
181186
//First try grabbing the retry-after header and parsing it.
@@ -187,7 +192,7 @@ void processResponseError (HttpResponseMessage httpResponse, GcmNotification not
187192
}
188193
}
189194

190-
throw new GcmConnectionException ("GCM HTTP Error: " + httpResponse.StatusCode);
195+
throw new GcmConnectionException ("GCM HTTP Error: " + httpResponse.StatusCode, responseBody);
191196
}
192197

193198
static GcmResponseStatus GetGcmResponseStatus (string str)

0 commit comments

Comments
 (0)