Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jan 13, 2012
@technocoreai technocoreai Better error messages
This should provide somewhat better error messages. In case we actually
reach GitHub (and not fail because of a network error), user will see
the HTTP error code and contents of message field from the response.
It's not perfect - ideally, we should use GitHub's response to show
specific error messages (like 'duplicate filename), but that's a
task for another day.
45a5de4
@technocoreai technocoreai Fix some style issues with command names adfcd4e
Commits on Jan 15, 2012
@condemil Merge pull request #11 from technocoreai/master
Better error messages and Fix some style issues with command names
3a72cf6
Showing with 62 additions and 33 deletions.
  1. +2 −2 Gist.sublime-commands
  2. +2 −2 Main.sublime-menu
  3. +58 −29 gist.py
View
4 Gist.sublime-commands
@@ -12,7 +12,7 @@
"command": "gist_list"
},
{
- "caption": "Gist: Add File To Gist",
+ "caption": "Gist: Add File to Gist",
"command": "gist_add_file"
},
{
@@ -20,7 +20,7 @@
"command": "gist_copy_url"
},
{
- "caption": "Gist: Open Gist In Browser",
+ "caption": "Gist: Open Gist in Browser",
"command": "gist_open_browser"
},
{
View
4 Main.sublime-menu
@@ -12,10 +12,10 @@
{ "command": "gist", "caption": "Create Public Gist…" },
{ "command": "gist_private", "caption": "Create Private Gist…" },
{ "command": "gist_list", "caption": "Open Gist…" },
- { "command": "gist_add_file", "caption": "Add To Gist…" },
+ { "command": "gist_add_file", "caption": "Add File to Gist…" },
{ "caption": "-" },
{ "command": "gist_copy_url", "caption": "Copy Gist URL" },
- { "command": "gist_open_browser", "caption": "Open Gist In Browser"},
+ { "command": "gist_open_browser", "caption": "Open Gist in Browser"},
{ "command": "gist_update_file", "caption": "Update File" },
{ "command": "gist_rename_file", "caption": "Rename File…" },
{ "command": "gist_delete_file", "caption": "Delete File" },
View
87 gist.py
@@ -10,6 +10,7 @@
import webbrowser
import tempfile
import traceback
+import contextlib
DEFAULT_CREATE_PUBLIC_VALUE = 'false'
DEFAULT_USE_PROXY_VALUE = 'false'
@@ -22,6 +23,11 @@ class MissingCredentialsException(Exception):
class CurlNotFoundException(Exception):
pass
+class SimpleHTTPError(Exception):
+ def __init__(self, code, response):
+ self.code = code
+ self.response = response
+
def get_credentials():
username = settings.get('username')
password = settings.get('password')
@@ -123,6 +129,16 @@ def _fn(*args, **kwargs):
if err.strerror:
msg += err.strerror
sublime.error_message(msg)
+ except SimpleHTTPError as err:
+ msg = "Gist: GitHub returned error %d" % err.code
+ try:
+ response_json = json.loads(err.response)
+ response_msg = response_json.get('message')
+ if response_msg:
+ msg += ": " + response_msg
+ except ValueError:
+ pass
+ sublime.error_message(msg)
except:
traceback.print_exc()
sublime.error_message("Gist: unknown error (please, report a bug!)")
@@ -198,14 +214,24 @@ def api_request_native(url, data=None, method=None):
urllib2.install_opener(opener)
- response = urllib2.urlopen(request)
try:
- if response.code == 204: # No Content
- return None
- else:
- return json.loads(response.read())
+ with contextlib.closing(urllib2.urlopen(request)) as response:
+ if response.code == 204: # No Content
+ return None
+ else:
+ return json.loads(response.read())
+ except urllib2.HTTPError as err:
+ with contextlib.closing(err):
+ raise SimpleHTTPError(err.code, err.read())
+
+@contextlib.contextmanager
+def named_tempfile():
+ tmpfile = tempfile.NamedTemporaryFile(delete=False)
+ try:
+ yield tmpfile
finally:
- response.close()
+ tmpfile.close()
+ os.unlink(tmpfile.name)
def api_request_curl(url, data=None, method=None):
command = ["curl", '-K', '-', url]
@@ -223,29 +249,32 @@ def api_request_curl(url, data=None, method=None):
if settings.get('https_proxy'):
config.append(settings.get('https_proxy'))
- data_file = None
- try:
- if data is not None:
- data_file = tempfile.NamedTemporaryFile(delete=False)
- data_file.write(data)
- data_file.close()
- config.append('--data-binary "@%s"' % data_file.name)
-
- process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- response, _ = process.communicate('\n'.join(config))
- returncode = process.returncode
-
- if returncode != 0:
- raise subprocess.CalledProcessError(returncode, 'curl')
-
- if response: # No way to get 204 out of cURL, so just check if response text is empty
- return json.loads(response)
- else:
- return None
- finally:
- if data_file:
- os.unlink(data_file.name)
- data_file.close()
+ with named_tempfile() as header_output_file:
+ config.append('--dump-header "%s"' % header_output_file.name)
+ header_output_file.close()
+ with named_tempfile() as data_file:
+ if data is not None:
+ data_file.write(data)
+ data_file.close()
+ config.append('--data-binary "@%s"' % data_file.name)
+
+ process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ response, _ = process.communicate('\n'.join(config))
+ returncode = process.returncode
+
+ if returncode != 0:
+ raise subprocess.CalledProcessError(returncode, 'curl')
+
+ with open(header_output_file.name, "r") as headers:
+ _, responsecode, message = headers.readline().split(None, 2)
+ responsecode = int(responsecode)
+
+ if responsecode == 204: # No Content
+ return None
+ elif 200 <= responsecode < 300:
+ return json.loads(response)
+ else:
+ raise SimpleHTTPError(responsecode, response)
api_request = api_request_curl if ('ssl' not in sys.modules and os.name != 'nt') else api_request_native

No commit comments for this range

Something went wrong with that request. Please try again.