优化错误提示

This commit is contained in:
Jalin
2019-01-22 22:16:15 +08:00
parent f781c353c3
commit 35c45bbeac
3 changed files with 8 additions and 7 deletions

View File

@@ -53,14 +53,17 @@ class Request(HTMLSession):
def request(self, *args, **kwargs): # 拦截所有错误
try:
return super().request(*args, **kwargs)
response = super().request(*args, **kwargs)
return response
except RequestException as e:
from py12306.log.common_log import CommonLog
if e.response:
response = e.response
else:
response = HTMLResponse(HTMLSession)
response.status_code = 500
# response.status_code = 500
expand_class(response, 'json', Request.json)
response.reason = response.reason if response.reason else CommonLog.MESSAGE_RESPONSE_EMPTY_ERROR
return response
def cdn_request(self, url: str, cdn=None, method='GET', **kwargs):

View File

@@ -170,8 +170,8 @@ class QueryLog(BaseLog):
return self
@classmethod
def add_query_time_log(cls, start, end, is_cdn):
return cls().add_log(('*' if is_cdn else '') + '耗时 %.2f' % (end - start))
def add_query_time_log(cls, time, is_cdn):
return cls().add_log(('*' if is_cdn else '') + '耗时 %.2f' % time)
@classmethod
def add_stay_log(cls, second):

View File

@@ -103,11 +103,9 @@ class Job:
self.refresh_station(station)
for date in self.left_dates:
self.left_date = date
tmp_start_time = time.time()
response = self.query_by_date(date)
tmp_end_time = time.time() # 耗时
self.handle_response(response)
QueryLog.add_query_time_log(tmp_start_time, tmp_end_time, is_cdn=self.is_cdn)
QueryLog.add_query_time_log(time=response.elapsed.total_seconds(), is_cdn=self.is_cdn)
if not self.is_alive: return
self.safe_stay()
if is_main_thread():