Commit 35c45bbe authored by Jalin's avatar Jalin

优化错误提示

parent f781c353
...@@ -53,14 +53,17 @@ class Request(HTMLSession): ...@@ -53,14 +53,17 @@ class Request(HTMLSession):
def request(self, *args, **kwargs): # 拦截所有错误 def request(self, *args, **kwargs): # 拦截所有错误
try: try:
return super().request(*args, **kwargs) response = super().request(*args, **kwargs)
return response
except RequestException as e: except RequestException as e:
from py12306.log.common_log import CommonLog
if e.response: if e.response:
response = e.response response = e.response
else: else:
response = HTMLResponse(HTMLSession) response = HTMLResponse(HTMLSession)
response.status_code = 500 # response.status_code = 500
expand_class(response, 'json', Request.json) expand_class(response, 'json', Request.json)
response.reason = response.reason if response.reason else CommonLog.MESSAGE_RESPONSE_EMPTY_ERROR
return response return response
def cdn_request(self, url: str, cdn=None, method='GET', **kwargs): def cdn_request(self, url: str, cdn=None, method='GET', **kwargs):
......
...@@ -170,8 +170,8 @@ class QueryLog(BaseLog): ...@@ -170,8 +170,8 @@ class QueryLog(BaseLog):
return self return self
@classmethod @classmethod
def add_query_time_log(cls, start, end, is_cdn): def add_query_time_log(cls, time, is_cdn):
return cls().add_log(('*' if is_cdn else '') + '耗时 %.2f' % (end - start)) return cls().add_log(('*' if is_cdn else '') + '耗时 %.2f' % time)
@classmethod @classmethod
def add_stay_log(cls, second): def add_stay_log(cls, second):
......
...@@ -103,11 +103,9 @@ class Job: ...@@ -103,11 +103,9 @@ class Job:
self.refresh_station(station) self.refresh_station(station)
for date in self.left_dates: for date in self.left_dates:
self.left_date = date self.left_date = date
tmp_start_time = time.time()
response = self.query_by_date(date) response = self.query_by_date(date)
tmp_end_time = time.time() # 耗时
self.handle_response(response) 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 if not self.is_alive: return
self.safe_stay() self.safe_stay()
if is_main_thread(): if is_main_thread():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment