-
Notifications
You must be signed in to change notification settings - Fork 132
fix: Guard response usage in validate_credentials #288
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,6 +171,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None: | |
| :param credentials: model credentials | ||
| :return: | ||
| """ | ||
| response = None | ||
| try: | ||
| headers = {"Content-Type": "application/json"} | ||
|
|
||
|
|
@@ -255,9 +256,15 @@ def validate_credentials(self, model: str, credentials: dict) -> None: | |
| ) | ||
| except CredentialsValidateFailedError: | ||
| raise | ||
| except requests.RequestException as ex: | ||
| raise CredentialsValidateFailedError( | ||
| f"Credentials validation request failed: {ex!s}" | ||
| ) from ex | ||
|
Comment on lines
+259
to
+262
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| except Exception as ex: | ||
| response_body = response.text if response is not None else "<no response>" | ||
| raise CredentialsValidateFailedError( | ||
| f"An error occurred during credentials validation: {ex!s}, response body {response.text}" | ||
| f"An error occurred during credentials validation: {ex!s}, " | ||
| f"response body {response_body}" | ||
| ) from ex | ||
|
Comment on lines
263
to
268
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the conditional assignment to |
||
|
|
||
| def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initializing
response = Noneis a crucial improvement. It preventsUnboundLocalErrorin scenarios whererequests.postmight raise an exception beforeresponseis assigned, ensuring that the variable is always defined when accessed in theexceptblocks.