Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pybit/_http_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from datetime import datetime as dt, timezone

from .exceptions import FailedRequestError, InvalidRequestError
from .exceptions import FailedRequestError, InvalidRequestError, RetryableError
from . import _helpers

# Requests will use simplejson if available.
Expand Down Expand Up @@ -201,6 +201,9 @@ def _submit_request(self, method=None, path=None, query=None, auth=False):
self._handle_network_error(e, retries_attempted)
except JSONDecodeError as e:
self._handle_json_error(e, retries_attempted)
except RetryableError as e:
self._handle_retryable_error(e.response, e.status_code, e.message, recv_window)
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recv_window parameter modifications in _handle_retryable_error will not persist across retries. When error code 10002 is encountered, _handle_retryable_error adds 2500 to recv_window (line 312), but since recv_window is an integer (immutable in Python), this modification is lost when the exception handler returns and the retry loop continues. The next retry will still use the original recv_window value from line 182. This means the recv_window adjustment feature for error code 10002 is non-functional. Consider making recv_window an instance variable or returning the updated value from _handle_retryable_error and using it in subsequent iterations.

Suggested change
self._handle_retryable_error(e.response, e.status_code, e.message, recv_window)
recv_window = self._handle_retryable_error(e.response, e.status_code, e.message, recv_window)

Copilot uses AI. Check for mistakes.


Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of a blank line here creates two consecutive blank lines (lines 206 and 207) before the raise statement. The codebase convention throughout this file shows only one blank line separating code blocks (e.g., between methods at lines 305-306, 322-323, 330-331). Consider removing this extra blank line to maintain consistency.

Suggested change

Copilot uses AI. Check for mistakes.
raise FailedRequestError(
request=f"{method} {path}: {req_params}",
Expand Down Expand Up @@ -275,8 +278,11 @@ def _handle_response(self, response, method, path, params, recv_window, retries_
error_msg = f"{s_json[ret_msg]} (ErrCode: {error_code})"

if error_code in self.retry_codes:
self._handle_retryable_error(response, error_code, error_msg, recv_window)
raise Exception("Retryable error occurred, retrying...")
raise RetryableError(
response=response,
message=error_msg,
status_code=error_code
)

if error_code not in self.ignore_codes:
raise InvalidRequestError(
Expand Down
18 changes: 18 additions & 0 deletions pybit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ def __init__(self, request, message, status_code, time, resp_headers):
f"{message} (ErrCode: {status_code}) (ErrTime: {time})"
f".\nRequest → {request}."
)


class RetryableError(Exception):
"""
Exception raised for retryable bybit error code
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word "bybit" should be capitalized as "Bybit" to match the proper noun format used elsewhere in the codebase. For example, other exception classes like InvalidRequestError use "Bybit" in their docstrings (line 39: "Exception raised for returned Bybit errors").

Suggested change
Exception raised for retryable bybit error code
Exception raised for retryable Bybit error code

Copilot uses AI. Check for mistakes.

Attributes:
response -- Response object
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring states "Attributes: response -- Response object" but the attribute name in the code is "response", not matching the naming convention seen in other exception classes like InvalidRequestError and FailedRequestError which use more descriptive names like "request", "message", "status_code", "time", and "resp_headers". For consistency, consider documenting what type of response object this is (e.g., "response -- HTTP Response object from requests library").

Suggested change
response -- Response object
response -- HTTP response object (for example, a Response instance from the requests library).

Copilot uses AI. Check for mistakes.
message -- Explanation of the error.
status_code -- The code number returned.
"""

def __init__(self, response, message, status_code):
self.response = response
self.message = message
self.status_code = status_code

super().__init__(f"{message} (ErrCode: {status_code})")
Loading