Skip to content

Conversation

@wheatliu
Copy link
Contributor

Pull Request

🧩 Issue Link

Closes #237

📝 Description

frendly error message for 401

Copilot AI review requested due to automatic review settings November 20, 2025 11:12
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @wheatliu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the error handling mechanism for OAuth2 authentication across several social media platform integrations. By introducing a dedicated PlatformAuthExpiredException, the system now provides a more consistent, explicit, and user-friendly response when access tokens or refresh tokens are missing or have expired. This change improves the clarity of authentication-related errors, making it easier to diagnose and resolve issues related to platform authorization.

Highlights

  • Introduced PlatformAuthExpiredException: A new custom exception class is added to specifically handle cases where OAuth2 credentials or access tokens are expired or missing across various platform services.
  • Standardized Authentication Error Handling: Across multiple social media platform services (Bilibili, Kwai, Meta platforms, Pinterest, TikTok, Twitter, YouTube), previous null returns or generic AppException throws for authentication issues have been replaced with the new PlatformAuthExpiredException.
  • Improved Error Messaging: The new exception provides a consistent and user-friendly error message: "OAuth2 credential expired, please re-authorize", along with a 401 HTTP status code, making it clearer to users and developers when re-authorization is needed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new PlatformAuthExpiredException to standardize handling of 401 authentication errors, which is a great step towards more consistent error management. The changes replace various platform-specific error handling mechanisms with this new exception. My review focuses on enhancing this new exception to include contextual information, such as the accountId, which was available in the previous logging statements. This will improve debuggability and make the new error handling more robust.

Comment on lines 4 to 6
constructor(platform: string) {
super(platform, 'GetAccessToken', 'AuthError', 'OAuth2 credential expired, please re-authorize', 401, 401, undefined, false, undefined)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve debuggability, consider adding a context parameter to the constructor. This allows passing valuable information like accountId when the exception is thrown, which is useful for logging and error tracking. The previous implementation in some services logged this information, and this change would preserve that context.

Suggested change
constructor(platform: string) {
super(platform, 'GetAccessToken', 'AuthError', 'OAuth2 credential expired, please re-authorize', 401, 401, undefined, false, undefined)
}
constructor(platform: string, context?: { accountId: string }) {
super(platform, 'GetAccessToken', 'AuthError', 'OAuth2 credential expired, please re-authorize', 401, 401, undefined, false, context)
}

if (!credential.access_token) {
return Promise.resolve('')
if (!credential || !credential.access_token) {
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

})
if (!oauth2Credential) {
return null
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

        throw new PlatformAuthExpiredException(this.platform, { accountId })

if (isRefreshTokenExpired) {
this.logger.warn(`Kwai account ${accountId} refresh_token is expired, expired at: ${accessTokenInfo.refresh_token_expires_in}`)
return null
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

if (!newAccountToken) {
this.logger.warn(`Kwai account ${accountId} access_token refresh failed`)
return null
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

if (!credential || !credential.access_token) {
this.logger.error(`youtube credential not found for accountId: ${accountId}`)
throw new AppException(ResponseCode.ChannelCredentialNotFound, { channel: 'youtube', accountId })
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

if (!credential.refresh_token) {
this.logger.error(`refresh Token not found for accountId: ${accountId}`)
throw new AppException(ResponseCode.ChannelRefreshTokenNotFound, { channel: 'youtube', accountId })
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

const errMsg = `youtube refresh Token expired for accountId: ${accountId}, expired at: ${credential.refresh_expires_in}, please re-authorize`
this.logger.error(errMsg)
throw new AppException(ResponseCode.ChannelRefreshTokenExpired, { channel: 'youtube', accountId, credential })
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

if (!accessToken) {
this.logger.error(`refresh Token failed for accountId: ${accountId}`)
throw new AppException(ResponseCode.ChannelRefreshTokenFailed, { accountId })
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })


if (!accessToken) {
return false
throw new PlatformAuthExpiredException(this.platform)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better debugging, please pass the accountId in the exception's context. This will require the suggested change to PlatformAuthExpiredException to accept a context object.

      throw new PlatformAuthExpiredException(this.platform, { accountId })

@gemini-code-assist
Copy link
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

Copy link
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 standardizes authentication error handling across all platform services by introducing a unified PlatformAuthExpiredException that returns a 401 status code with a user-friendly message "OAuth2 credential expired, please re-authorize". The changes replace various inconsistent error patterns (returning null, returning false, throwing different exceptions) with this single exception type.

  • Introduces PlatformAuthExpiredException class for consistent auth error handling
  • Replaces platform-specific error codes and messages with a unified 401 response
  • Removes redundant null checks that are now handled by throwing exceptions

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
platform.exception.ts New exception class for OAuth2 authentication failures with 401 status code
youtube.service.ts Replaces multiple auth error codes with PlatformAuthExpiredException; changes ensureValidAccessToken to throw instead of returning false
twitter.service.ts Removes null checks and error handling in authorize and getOAuth2Credential methods
tiktok.service.ts Removes unused imports and simplifies token refresh error handling
pinterest.service.ts Replaces null returns with exception throws in authorize method
threads.service.ts Removes null checks and error logging in favor of exception throwing
linkedin.service.ts Standardizes error handling for token refresh failures
instagram.service.ts Replaces generic Error throws with PlatformAuthExpiredException
facebook.service.ts Standardizes error handling for token refresh and save failures
meta/base.service.ts Replaces null return with exception throw in getOAuth2Credential
kwai.service.ts Removes null checks and standardizes auth error handling
bilibili.service.ts Changes empty string returns to exception throws for missing credentials

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

const newAccountToken = await this.kwaiApiService.refreshToken(
accessTokenInfo.refresh_token,
)
if (!newAccountToken) {
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

[nitpick] The removed warning log provided context about refresh failure. Consider adding logging before throwing the exception:

this.logger.warn(`Kwai account ${accountId} access_token refresh failed`)
throw new PlatformAuthExpiredException(this.platform)
Suggested change
if (!newAccountToken) {
if (!newAccountToken) {
this.logger.warn(`Kwai account ${accountId} access_token refresh failed`)

Copilot uses AI. Check for mistakes.

if (!accessToken) {
return false
throw new PlatformAuthExpiredException(this.platform)
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

The behavior of ensureValidAccessToken has changed from returning false to throwing PlatformAuthExpiredException. This change creates unreachable code at line 2077-2078. Since getUserAccessToken (called at line 2075) will now throw PlatformAuthExpiredException when there's no access token, the null check at line 2077 will never be false. The function will either return true at line 2081 or throw an exception.

Consider either:

  1. Removing the unreachable null check (lines 2077-2078)
  2. Updating all callers that expect boolean returns (there are 32 call sites like if (!(await this.ensureValidAccessToken(accountId)))) to handle exceptions instead

The current implementation makes the function's return type misleading - it's declared as Promise<boolean> but will never return false.

Copilot uses AI. Check for mistakes.
}

const isRefreshTokenExpired = credential.refresh_expires_in && credential.refresh_expires_in <= getCurrentTimestamp()
if (isRefreshTokenExpired) {
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

[nitpick] The removed error logging included useful debugging information (expiration timestamp). Consider adding similar logging before throwing the exception to help with troubleshooting:

this.logger.error(`youtube refresh Token expired for accountId: ${accountId}, expired at: ${credential.refresh_expires_in}`)
throw new PlatformAuthExpiredException(this.platform)
Suggested change
if (isRefreshTokenExpired) {
if (isRefreshTokenExpired) {
this.logger.error(`youtube refresh Token expired for accountId: ${accountId}, expired at: ${credential.refresh_expires_in}`)

Copilot uses AI. Check for mistakes.
// 判断 refresh_token 是否过期
const isRefreshTokenExpired
= getCurrentTimestamp() >= accessTokenInfo.refresh_token_expires_in
if (isRefreshTokenExpired) {
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

[nitpick] The removed warning log included helpful debugging context (expiration timestamp). Consider adding logging before throwing the exception:

this.logger.warn(`Kwai account ${accountId} refresh_token is expired, expired at: ${accessTokenInfo.refresh_token_expires_in}`)
throw new PlatformAuthExpiredException(this.platform)
Suggested change
if (isRefreshTokenExpired) {
if (isRefreshTokenExpired) {
this.logger.warn(`Kwai account ${accountId} refresh_token is expired, expired at: ${accessTokenInfo.refresh_token_expires_in}`)

Copilot uses AI. Check for mistakes.
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.

[Issue] Optimize error handling caused by expired LinkedIn accounts.

2 participants