[banshee] [Lastfm] Fix crash using auth Refresh button (bgo#610911)



commit 16b8d4af0a6ebf8e7deba11f691e024cd343f4e8
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Mar 12 14:21:53 2010 -0800

    [Lastfm] Fix crash using auth Refresh button (bgo#610911)

 src/Libraries/Lastfm/Lastfm/Account.cs |   74 ++++++++++++++++++--------------
 1 files changed, 42 insertions(+), 32 deletions(-)
---
diff --git a/src/Libraries/Lastfm/Lastfm/Account.cs b/src/Libraries/Lastfm/Lastfm/Account.cs
index d5048b4..0e5533c 100644
--- a/src/Libraries/Lastfm/Lastfm/Account.cs
+++ b/src/Libraries/Lastfm/Lastfm/Account.cs
@@ -96,47 +96,57 @@ namespace Lastfm
 
         public StationError RequestAuthorization ()
         {
-            LastfmRequest get_token = new LastfmRequest ("auth.getToken", RequestType.Read, ResponseFormat.Json);
-            get_token.Send ();
-
-            var response = get_token.GetResponseObject ();
-            object error_code;
-            if (response.TryGetValue ("error", out error_code)) {
-                Log.WarningFormat ("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
-                return (StationError) error_code;
+            try {
+                LastfmRequest get_token = new LastfmRequest ("auth.getToken", RequestType.Read, ResponseFormat.Json);
+                get_token.Send ();
+
+                var response = get_token.GetResponseObject ();
+                object error_code;
+                if (response.TryGetValue ("error", out error_code)) {
+                    Log.WarningFormat ("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
+                    return (StationError) error_code;
+                }
+
+                authentication_token = (string)response["token"];
+                Browser.Open (String.Format ("http://www.last.fm/api/auth?api_key={0}&token={1}";, LastfmCore.ApiKey, authentication_token));
+
+                return StationError.None;
+            } catch (Exception e) {
+                Log.Exception ("Error in Lastfm.Account.RequestAuthorization", e);
+                return StationError.Unknown;
             }
-
-            authentication_token = (string)response["token"];
-            Browser.Open (String.Format ("http://www.last.fm/api/auth?api_key={0}&token={1}";, LastfmCore.ApiKey, authentication_token));
-
-            return StationError.None;
         }
 
         public StationError FetchSessionKey ()
         {
             if (authentication_token == null) {
-                throw new InvalidOperationException ("RequestAuthorization should be called before calling FetchSessionKey");
+                return StationError.TokenNotAuthorized;
             }
 
-            LastfmRequest get_session = new LastfmRequest ("auth.getSession", RequestType.SessionRequest, ResponseFormat.Json);
-            get_session.AddParameter ("token", authentication_token);
-            get_session.Send ();
-            var response = get_session.GetResponseObject ();
-            object error_code;
-            if (response.TryGetValue ("error", out error_code)) {
-                Log.WarningFormat ("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
-                return (StationError) error_code;
+            try {
+                LastfmRequest get_session = new LastfmRequest ("auth.getSession", RequestType.SessionRequest, ResponseFormat.Json);
+                get_session.AddParameter ("token", authentication_token);
+                get_session.Send ();
+                var response = get_session.GetResponseObject ();
+                object error_code;
+                if (response.TryGetValue ("error", out error_code)) {
+                    Log.WarningFormat ("Lastfm error {0} : {1}", (int)error_code, (string)response["message"]);
+                    return (StationError) error_code;
+                }
+
+                var session = (Hyena.Json.JsonObject)response["session"];
+                UserName = (string)session["name"];
+                SessionKey = (string)session["key"];
+                Subscriber = session["subscriber"].ToString ().Equals ("1");
+
+                // The authentication token is only valid once, and for a limited time
+                authentication_token = null;
+
+                return StationError.None;
+            } catch (Exception e) {
+                Log.Exception ("Error in Lastfm.Account.FetchSessionKey", e);
+                return StationError.Unknown;
             }
-
-            var session = (Hyena.Json.JsonObject)response["session"];
-            UserName = (string)session["name"];
-            SessionKey = (string)session["key"];
-            Subscriber = session["subscriber"].ToString ().Equals ("1");
-
-            // The authentication token is only valid once, and for a limited time
-            authentication_token = null;
-
-            return StationError.None;
         }
 
         protected void OnUpdated ()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]