Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced error handling #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions plugin/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,50 @@ def to_file(expr):
json_data = json.loads(data)
data = None

response = requests.request(method, url, verify=verify_ssl, headers=headers, data=data, files=files, json=json_data)
content_type = response.headers.get('Content-Type', '').split(';')[0]

response_body = response.text
if JSON_REGEX.search(content_type):
content_type = 'application/json'
try:
response_body = json.dumps(
json.loads(response.text), sort_keys=True, indent=2,
separators=(',', ': '),
ensure_ascii=vim.eval('g:http_client_json_escape_utf')=='1')
except ValueError:
pass
try:
response = requests.request(method, url, verify=verify_ssl, headers=headers, data=data, files=files, json=json_data)
content_type = response.headers.get('Content-Type', '').split(';')[0]
response_body = response.text
if JSON_REGEX.search(content_type):
content_type = 'application/json'
try:
response_body = json.dumps(
json.loads(response.text), sort_keys=True, indent=2,
separators=(',', ': '),
ensure_ascii=vim.eval('g:http_client_json_escape_utf')=='1')
except ValueError:
pass

display = (
response_body.split('\n') +
['', '// status code: %s' % response.status_code] +
['// %s: %s' % (k, v) for k, v in response.headers.items()]
)

except requests.exceptions.Timeout as err:
display, content_type = on_error("Timeout Error", err)

except requests.exceptions.TooManyRedirects as err:
display, content_type = on_error("Too many redirects", err)

except requests.exceptions.RequestException as err:
display, content_type = on_error("Request exception", err)

return display, content_type

def on_error(message, err):
print(message)
display = (
response_body.split('\n') +
['', '// status code: %s' % response.status_code] +
['// %s: %s' % (k, v) for k, v in response.headers.items()]
[message] + ["Request data:", ""] +
["Method: {0}".format(err.request.method)] +
["Path: {0}".format(err.request.path_url)] +
["Headers: {0}".format(err.request.headers)] +
["Body: {0}".format(err.request.body)] +
["Response: {0}".format(err.response)]
)

content_type = "text/plain"
return display, content_type


# Vim methods.

def vim_filetypes_by_content_type():
Expand Down