Skip to content
This repository
Browse code

Improved error handling

@catch_errors now handles and pretty-prints other errors that the user
may encounter, not just MissingCredentialsException.
  • Loading branch information...
commit c948a3ee684dcce997b3c6bcb4a8ba3f09d5efb3 1 parent c55b776
Alexey Ermakov authored January 13, 2012

Showing 1 changed file with 26 additions and 11 deletions. Show diff stats Hide diff stats

  1. 37  gist.py
37  gist.py
@@ -9,13 +9,14 @@
9 9
 import functools
10 10
 import webbrowser
11 11
 import tempfile
  12
+import traceback
12 13
 
13 14
 DEFAULT_CREATE_PUBLIC_VALUE = 'false'
14 15
 DEFAULT_USE_PROXY_VALUE = 'false'
15 16
 settings = sublime.load_settings('Gist.sublime-settings')
16 17
 GISTS_URL = 'https://api.github.com/gists'
17 18
 
18  
-class GistMissingCredentialsException(Exception):
  19
+class MissingCredentialsException(Exception):
19 20
     pass
20 21
 
21 22
 class CurlNotFoundException(Exception):
@@ -25,7 +26,7 @@ def get_credentials():
25 26
     username = settings.get('username')
26 27
     password = settings.get('password')
27 28
     if not username or not password:
28  
-        raise GistMissingCredentialsException()
  29
+        raise MissingCredentialsException()
29 30
     return (username, password)
30 31
 
31 32
 if sublime.platform() == 'osx':
@@ -97,20 +98,34 @@ def keychain_get_credentials():
97 98
                         lib_security.SecKeychainItemFreeContent(attrlist_ptr, password_buf)
98 99
 
99 100
             if not username or not password:
100  
-                raise GistMissingCredentialsException()
  101
+                raise MissingCredentialsException()
101 102
             else:
102 103
                 return (username, password)
103 104
 
104 105
         return keychain_get_credentials
105 106
     get_credentials = create_keychain_accessor()
106 107
 
107  
-def catching_credential_errors(fn):
  108
+def catch_errors(fn):
108 109
     @functools.wraps(fn)
109 110
     def _fn(*args, **kwargs):
110 111
         try:
111 112
             return fn(*args, **kwargs)
112  
-        except GistMissingCredentialsException:
113  
-            sublime.error_message("GitHub username or password isn't provided in Gist.sublime-settings file")
  113
+        except MissingCredentialsException:
  114
+            sublime.error_message("Gist: GitHub username or password isn't provided in Gist.sublime-settings file")
  115
+        except subprocess.CalledProcessError as err:
  116
+            sublime.error_message("Gist: Error while contacting GitHub: cURL returned %d" % err.returncode)
  117
+        except EnvironmentError as err:
  118
+            traceback.print_exc()
  119
+            if type(err) == OSError and err.errno == 2 and api_request == api_request_curl:
  120
+                sublime.error_message("Gist: Unable to find Python SSL module or cURL")
  121
+            else:
  122
+                msg = "Gist: Error while contacting GitHub"
  123
+                if err.strerror:
  124
+                    msg += err.strerror
  125
+                sublime.error_message(msg)
  126
+        except:
  127
+            traceback.print_exc()
  128
+            sublime.error_message("Gist: unknown error (please, report a bug!)")
114 129
     return _fn
115 130
 
116 131
 def create_gist(public, text, filename, description):
@@ -218,7 +233,7 @@ class GistCommand(sublime_plugin.TextCommand):
218 233
     def mode(self):
219 234
         return "Public" if self.public else "Private"
220 235
 
221  
-    @catching_credential_errors
  236
+    @catch_errors
222 237
     def run(self, edit):
223 238
         get_credentials()
224 239
         selections = [region for region in self.view.sel() if not region.empty()]
@@ -235,7 +250,7 @@ def create_gist_with_text(text):
235 250
             filename = os.path.basename(self.view.file_name()) if self.view.file_name() else ''
236 251
 
237 252
             def on_gist_filename(filename):
238  
-                @catching_credential_errors
  253
+                @catch_errors
239 254
                 def on_gist_description(description):
240 255
                     gist = create_gist(self.public, text, filename, description)
241 256
                     print gist
@@ -267,7 +282,7 @@ class GistUpdateCommand(sublime_plugin.TextCommand):
267 282
     def is_enabled(self):
268 283
         return self.view.settings().get("gist_url") is not None
269 284
 
270  
-    @catching_credential_errors
  285
+    @catch_errors
271 286
     def run(self, edit):
272 287
         text = self.view.substr(sublime.Region(0, self.view.size()))
273 288
         update_gist(self.view.settings().get("gist_url"), self.view.settings().get("gist_filename"), text)
@@ -276,14 +291,14 @@ class GistPrivateCommand(GistCommand):
276 291
     public = False
277 292
 
278 293
 class GistListCommand(sublime_plugin.WindowCommand):
279  
-    @catching_credential_errors
  294
+    @catch_errors
280 295
     def run(self):
281 296
         gists = get_gists()
282 297
 
283 298
         gist_names = [gist_title(gist) for gist in gists]
284 299
         gist_urls = [gist['url'] for gist in gists]
285 300
 
286  
-        @catching_credential_errors
  301
+        @catch_errors
287 302
         def open_gist(num):
288 303
             if num != -1:
289 304
                 get_gist(gist_urls[num])

0 notes on commit c948a3e

Please sign in to comment.
Something went wrong with that request. Please try again.