diff --git a/SparklrLib/SparklrClient.cs b/SparklrLib/SparklrClient.cs index 58c49f1..b2e6b45 100644 --- a/SparklrLib/SparklrClient.cs +++ b/SparklrLib/SparklrClient.cs @@ -181,7 +181,8 @@ private async Task> requestJsonObjectAsync(string pat Response = null }; } - + bool error = false; + WebException exc; try { HttpWebResponse streamResp = (HttpWebResponse)await streamReq.GetResponseAsync(); @@ -213,26 +214,57 @@ private async Task> requestJsonObjectAsync(string pat } catch (WebException ex) { - if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Forbidden) - { - raiseCredentialsExpired(); - } - + error = true; + exc = ex; + } + catch (Exception ex) + { return new JSONRequestEventArgs() { IsSuccessful = false, - Error = ex, - Response = (HttpWebResponse)ex.Response + Error = ex }; } - catch (Exception ex) + if (error) { + if (((HttpWebResponse)exc.Response).StatusCode == HttpStatusCode.Forbidden) + { + using (StreamReader strReader = new StreamReader(exc.Response.GetResponseStream(), Encoding.UTF8)) + { + string json = await strReader.ReadToEndAsync(); + + if (json == "") + { + raiseCredentialsExpired(); + } + else + { + //TODO: Handle other errors here, for example not authorized to see user profile: + //{ + // "error": true, + // "info": { + // "notFriends": true, + // "following": false + // } + //} + } + } + } + return new JSONRequestEventArgs() { IsSuccessful = false, - Error = ex + Error = exc, + Response = (HttpWebResponse)exc.Response }; } + + return new JSONRequestEventArgs() + { + IsSuccessful = false, + Error = null, + Response = null + }; } public void ManualLogin(string authToken, long userId)