-
Notifications
You must be signed in to change notification settings - Fork 201
fix(python): Add timeout to ErrorDocHelper requests.get() calls #1661
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,15 +103,15 @@ class ErrorDocHelper: | |||||||||||
| ) | ||||||||||||
| pattern = re.compile(RE_PATTERN, flags=re.IGNORECASE) | ||||||||||||
|
|
||||||||||||
| def get_index(self, url: str = ERROR_CODES_URL) -> None: | ||||||||||||
| r = requests.get(f"{url}index.json") | ||||||||||||
| def get_index(self, url: str = ERROR_CODES_URL, timeout: int = 10) -> None: | ||||||||||||
| r = requests.get(f"{url}index.json", timeout=timeout) | ||||||||||||
| self.lookup_dict = json.loads(r.text) | ||||||||||||
|
|
||||||||||||
| def lookup( | ||||||||||||
| self, url: str = ERROR_CODES_URL, code: str = "", path: str = "" | ||||||||||||
| self, url: str = ERROR_CODES_URL, code: str = "", path: str = "", timeout: int = 10 | ||||||||||||
| ) -> Tuple[str, str]: | ||||||||||||
| if len(self.lookup_dict) == 0: | ||||||||||||
| self.get_index(url=url) | ||||||||||||
| self.get_index(url=url, timeout=timeout) | ||||||||||||
|
|
||||||||||||
| error_doc_url: str = "" | ||||||||||||
| error_doc: str = "" | ||||||||||||
|
|
@@ -128,13 +128,13 @@ def lookup( | |||||||||||
| error_doc = f"### No documentation found for {code}" | ||||||||||||
|
|
||||||||||||
| if error_doc_url: | ||||||||||||
| r = requests.get(f"{self.ERROR_CODES_URL}{error_doc_url}") | ||||||||||||
| r = requests.get(f"{self.ERROR_CODES_URL}{error_doc_url}", timeout=timeout) | ||||||||||||
| error_doc = r.text | ||||||||||||
|
Comment on lines
+131
to
132
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. Similar to the other Adding
Suggested change
|
||||||||||||
|
|
||||||||||||
| return (f"{self.ERROR_CODES_URL}{error_doc_url}", error_doc) | ||||||||||||
|
|
||||||||||||
| def parse_and_lookup( | ||||||||||||
| self, error_url: str, url: str = ERROR_CODES_URL | ||||||||||||
| self, error_url: str, url: str = ERROR_CODES_URL, timeout: int = 10 | ||||||||||||
| ) -> Tuple[str, str]: | ||||||||||||
| m = re.search(self.RE_PATTERN, error_url) | ||||||||||||
| if not m: | ||||||||||||
|
|
@@ -143,6 +143,6 @@ def parse_and_lookup( | |||||||||||
| code: str = cast(Tuple[str, str, str, str], m.groups())[2] | ||||||||||||
| path: str = cast(Tuple[str, str, str, str], m.groups())[3] | ||||||||||||
| try: | ||||||||||||
| return self.lookup(url=url, code=code, path=path) | ||||||||||||
| return self.lookup(url=url, code=code, path=path, timeout=timeout) | ||||||||||||
| except requests.exceptions.RequestException: | ||||||||||||
| return ("", "") | ||||||||||||
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.
The
requests.get()call doesn't check for unsuccessful HTTP status codes. If the server returns an error (e.g., 404, 500),r.textmight not be valid JSON, which could lead to ajson.JSONDecodeError. It's a good practice to handle such responses explicitly.I suggest using
r.raise_for_status()to raise anHTTPErrorfor bad responses. This exception is a subclass ofrequests.exceptions.RequestExceptionand will be caught by the handler inparse_and_lookup, ensuring graceful failure. Usingr.json()is also more idiomatic for parsing JSON responses.