Skip to content

Fix: Safely parse OAuth refresh error responses#108

Open
glensc wants to merge 3 commits into
mainfrom
improve-parse
Open

Fix: Safely parse OAuth refresh error responses#108
glensc wants to merge 3 commits into
mainfrom
improve-parse

Conversation

@glensc
Copy link
Copy Markdown
Owner

@glensc glensc commented May 28, 2026

Extracted from #104

@glensc glensc self-assigned this May 28, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

Review Change Stack

Warning

Review limit reached

@Copilot, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 59 minutes. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b05d7fc6-ba63-4a00-ba8c-7455c6496a17

📥 Commits

Reviewing files that changed from the base of the PR and between fe8548c and d65434c.

📒 Files selected for processing (1)
  • trakt/errors.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch improve-parse

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens OAuthRefreshException to avoid crashing when the OAuth refresh error response is missing or cannot be decoded as JSON, improving reliability in common failure paths.

Changes:

  • Add a specific message for OAuthRefreshException.
  • Replace direct response.json() usage with _load_data() that safely returns {} for missing/invalid responses.
  • Add minimal validation to ensure parsed JSON is a dict before storing it.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread trakt/errors.py
Comment thread trakt/errors.py Outdated
Comment thread trakt/errors.py
- Handle missing, invalid, or non-dict JSON payloads
- Avoid crashes when refresh failures return unexpected bodies
- Add fallback type safe values for error, error_description

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: OpenCode (gpt-5.4) <noreply@openai.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread trakt/errors.py
Comment on lines 71 to 84
class OAuthRefreshException(OAuthException):
message = 'Unauthorized - OAuth token refresh failed'

def __init__(self, response=None):
super().__init__(response)
self.data = self.response.json()
self.data = self._error_data(self.response)

@property
def error(self):
return self.data["error"]

@property
def error_description(self):
return self.data["error_description"]
Copy link
Copy Markdown
Owner Author

@glensc glensc May 28, 2026

Choose a reason for hiding this comment

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

The current PR doesn't plan to fully implement. That may be a separate PR instead.

Comment thread trakt/errors.py
Comment on lines 74 to +108
def __init__(self, response=None):
super().__init__(response)
self.data = self.response.json()
self.data = self._error_data(self.response)

@property
def error(self):
return self.data["error"]

@property
def error_description(self):
return self.data["error_description"]

@classmethod
def _error_data(cls, response):
data = cls._response_json(response)

return {
"error": data.get("error", ""),
"error_description": data.get("error_description", ""),
}

@staticmethod
def _response_json(response):
if response is None:
return {}

try:
data = response.json()
except (ValueError, AttributeError):
return {}

if not isinstance(data, dict):
return {}

return data
Comment thread trakt/errors.py
Comment on lines 73 to 75

def __init__(self, response=None):
super().__init__(response)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Implemented in commit d65434c by adding a class docstring to OAuthRefreshException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants