Skip to content

Commit

Permalink
fix: response is not declared when a request fails resulting in a res…
Browse files Browse the repository at this point in the history
…ponse.text exception (#42)
  • Loading branch information
Yaocool authored Jun 11, 2024
1 parent ad66fee commit a87c910
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions dingtalk_stream/card_replier.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def create_and_send_card(
response.raise_for_status()
except Exception as e:
self.logger.error(
f"CardResponder.send_card failed, create card instance failed, error={e}, response.text={response.text}"
f"CardResponder.send_card failed, create card instance failed, error={e}, response.text={response.text if 'response' in locals() else ''}"
)
return ""

Expand Down Expand Up @@ -166,7 +166,7 @@ def create_and_send_card(
return card_instance_id
except Exception as e:
self.logger.error(
f"put_card_data.create_and_send_card failed, send card failed, error={e}, response.text={response.text}"
f"put_card_data.create_and_send_card failed, send card failed, error={e}, response.text={response.text if 'response' in locals() else ''}"
)
return ""

Expand Down Expand Up @@ -267,7 +267,7 @@ def create_and_deliver_card(
)
except Exception as e:
self.logger.error(
f"CardReplier.put_card_data failed, update card failed, error={e}, response.text={response.text}"
f"CardReplier.put_card_data failed, update card failed, error={e}, response.text={response.text if 'response' in locals() else ''}"
)

return card_instance_id
Expand Down Expand Up @@ -304,7 +304,7 @@ def put_card_data(self, card_instance_id: str, card_data: dict, **kwargs):
response.raise_for_status()
except Exception as e:
self.logger.error(
f"CardReplier.put_card_data failed, update card failed, error={e}, response.text={response.text}"
f"CardReplier.put_card_data failed, update card failed, error={e}, response.text={response.text if 'response' in locals() else ''}"
)
return

Expand Down Expand Up @@ -416,6 +416,6 @@ def streaming(
response.raise_for_status()
except Exception as e:
self.logger.error(
f"AICardReplier.streaming failed, error={e}, response.text={response.text}"
f"AICardReplier.streaming failed, error={e}, response.text={response.text if 'response' in locals() else ''}"
)
return
12 changes: 6 additions & 6 deletions dingtalk_stream/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def get_image_download_url(self, download_code: str) -> str:

response.raise_for_status()
except Exception as e:
self.logger.error(f'get_image_download_url, error={e}, response.text={response.text}')
self.logger.error(f'get_image_download_url, error={e}, response.text={response.text if 'response' in locals() else ''}')
return ""
return response.json()["downloadUrl"]

Expand Down Expand Up @@ -607,7 +607,7 @@ def set_off_duty_prompt(self, text: str, title: str = "", logo: str = ""):

response.raise_for_status()
except Exception as e:
self.logger.error(f'set_off_duty_prompt, error={e}, response.text={response.text}')
self.logger.error(f'set_off_duty_prompt, error={e}, response.text={response.text if 'response' in locals() else ''}')
return response.status_code
return response.json()

Expand All @@ -633,7 +633,7 @@ def reply_text(self,
data=json.dumps(values))
response.raise_for_status()
except Exception as e:
self.logger.error(f'reply text failed, error={e}, response.text={response.text}')
self.logger.error(f'reply text failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return None
return response.json()

Expand Down Expand Up @@ -661,7 +661,7 @@ def reply_markdown(self,
data=json.dumps(values))
response.raise_for_status()
except Exception as e:
self.logger.error(f'reply markdown failed, error={e}, response.text={response.text}')
self.logger.error(f'reply markdown failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return None
return response.json()

Expand Down Expand Up @@ -742,7 +742,7 @@ def reply_card(self,

return card_biz_id
except Exception as e:
self.logger.error(f'reply card failed, error={e}, response.text={response.text}')
self.logger.error(f'reply card failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return ""

def update_card(self, card_biz_id: str, card_data: dict):
Expand Down Expand Up @@ -778,7 +778,7 @@ def update_card(self, card_biz_id: str, card_data: dict):
data=json.dumps(values))
response.raise_for_status()
except Exception as e:
self.logger.error(f'update card failed, error={e}, response.text={response.text}')
self.logger.error(f'update card failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return response.status_code
return response.json()

Expand Down
8 changes: 4 additions & 4 deletions dingtalk_stream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def open_connection(self):
data=request_body)
response.raise_for_status()
except Exception as e:
self.logger.error(f'open connection failed, error={e}, response.text={response.text}')
self.logger.error(f'open connection failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return None
return response.json()

Expand Down Expand Up @@ -196,7 +196,7 @@ def get_access_token(self):
data=json.dumps(values))
response.raise_for_status()
except Exception as e:
self.logger.error(f'get dingtalk access token failed, error={e}, response.text={response.text}')
self.logger.error(f'get dingtalk access token failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return None

result = response.json()
Expand All @@ -223,9 +223,9 @@ def upload_to_dingtalk(self, image_content, filetype='image', filename='image.pn
self.reset_access_token()
response.raise_for_status()
except Exception as e:
self.logger.error(f'upload to dingtalk failed, error={e}, response.text={response.text}')
self.logger.error(f'upload to dingtalk failed, error={e}, response.text={response.text if 'response' in locals() else ''}')
return None
if 'media_id' not in response.json():
self.logger.error('upload to dingtalk failed, error resonse is %s', response.json())
self.logger.error('upload to dingtalk failed, error response is %s', response.json())
raise Exception('upload failed, error=%s' % response.json())
return response.json()['media_id']

0 comments on commit a87c910

Please sign in to comment.