Skip to content

Commit

Permalink
Show better errors if something goes wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyennis145 committed Jun 9, 2016
1 parent 00b6d12 commit 43c8c90
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,30 @@ def run(self, edit):
headers={'Content-Type': 'application/json','User-Agent': 'Mozilla/5.0' }
)

response = urllib.request.urlopen(request)
response_as_string = response.read().decode('utf8')
response_as_json = json.loads(response_as_string)
try:
response = urllib.request.urlopen(request)
response_as_string = response.read().decode('utf8')
response_as_json = json.loads(response_as_string)

chunk_identifier = response_as_json['identifier']
chunk_content = response_as_json['content']
chunk_info = response_as_json["chunk"]

chunk_identifier = chunk_info['identifier']
chunk_content = chunk_info['content']

shortened_original_content = chunk_content[:10] + (chunk_content[10:] and '..')
shortened_original_content = chunk_content[:10] + (chunk_content[10:] and '..')

replacement_text = snippet_template.replace("{identifier}", chunk_identifier)
replacement_text = replacement_text.replace("{label}", shortened_original_content)

# Make the replacement
self.view.replace(edit, selection, replacement_text )

except urllib.error.HTTPError as e:

response_as_string = e.read().decode('utf8')
response_as_json = json.loads(response_as_string)
message = response_as_json['message']

sublime.error_message(message)

replacement_text = snippet_template.replace("{identifier}", chunk_identifier)
replacement_text = replacement_text.replace("{label}", shortened_original_content)

# Make the replacement
self.view.replace(edit, selection, replacement_text )

0 comments on commit 43c8c90

Please sign in to comment.