Skip to content

Commit

Permalink
添加异常处理
Browse files Browse the repository at this point in the history
  • Loading branch information
gusibi committed Apr 26, 2016
1 parent 17241c1 commit c603368
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ python-weixin
-----
A Python client for the Weixin REST APIs

0.1.1 新增功能
-----
完善服务异常处理

0.1.0 功能
-----
微信公众平台 和开放平台 支持
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages

setup(name="python-weixin",
version="0.1.0",
version="0.1.1",
description="Weixin API client",
license="BSD",
install_requires=["simplejson","requests","six", "chardet"],
Expand Down
47 changes: 30 additions & 17 deletions weixin/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@


import requests
from requests.exceptions import ConnectTimeout
from requests.exceptions import (ConnectTimeout, ReadTimeout,
ConnectionError as _ConnectionError)
from six.moves.urllib.parse import urlencode


Expand All @@ -34,7 +35,16 @@ def __str__(self):


class ConnectTimeoutError(Exception):
def __init__(self, code='timeout', description='Connect timeout'):
def __init__(self, code, description):
self.code = code
self.description = description

def __str__(self):
return '%s: %s' % (self.code, self.description)


class ConnectionError(Exception):
def __init__(self, code, description):
self.code = code
self.description = description

Expand Down Expand Up @@ -141,8 +151,11 @@ def get_authorize_login_url(self, scope=None, state=None):
url = self._url_for_authorize(scope=scope, state=state)
try:
response = requests.get(url, timeout=TIMEOUT)
except ConnectTimeout:
raise ConnectTimeoutError()
except (ConnectTimeout, ReadTimeout):
raise ConnectTimeoutError('timeout', 'Connect timeout')
except _ConnectionError:
raise ConnectionError('conntect_error',
'Failed to establish a new connection')
headers = response.headers
if int(headers.get('content-length', 384)) < 500:
# 微信 参数错误返回html页面 http 状态码也是200
Expand All @@ -161,8 +174,11 @@ def exchange_for_access_token(self, code=None,
refresh_token, scope=scope)
try:
response = requests.get(access_token_url, timeout=TIMEOUT)
except ConnectTimeout:
raise ConnectTimeoutError()
except (ConnectTimeout, ReadTimeout):
raise ConnectTimeoutError('timeout', 'Connect timeout')
except _ConnectionError:
raise ConnectionError('conntect_error',
'Failed to establish a new connection')
parsed_content = simplejson.loads(response.content.decode())
if parsed_content.get('errcode', 0):
raise OAuth2AuthExchangeError(
Expand Down Expand Up @@ -241,14 +257,11 @@ def make_request(self, url, method="GET", body=None, headers=None):
if 'User-Agent' not in headers:
headers.update({"User-Agent":
"%s Python Client" % self.api.api_name})
if method == 'GET':
try:
return requests.get(url, headers=headers, timeout=TIMEOUT)
except ConnectTimeout:
raise ConnectTimeoutError()
elif method == 'POST':
try:
return requests.post(url, data=body, headers=headers,
timeout=TIMEOUT)
except ConnectTimeout:
raise ConnectTimeoutError()
try:
return requests.request(method, url, data=body,
headers=headers, timeout=TIMEOUT)
except (ConnectTimeout, ReadTimeout):
raise ConnectTimeoutError('timeout', 'Connect timeout')
except _ConnectionError:
raise ConnectionError('conntect_error',
'Failed to establish a new connection')

0 comments on commit c603368

Please sign in to comment.