Skip to content

Let's use AI to Earn!

feat(publishing): frendly error message (#315)

wheat159522696eb5
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts
index 8dd08b37..bbe97304 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts
@@ -23,6 +23,7 @@ import {
import { OAuth2Credential } from '../../../libs/database/schema/oauth2Credential.schema'
import { PlatformBaseService } from '../base.service'
import { AuthTaskInfo } from '../common'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { BilibiliAuthInfo } from './common'
@Injectable()
@@ -343,11 +344,8 @@ export class BilibiliService extends PlatformBaseService {
*/
async getAccountAccessToken(accountId: string): Promise<string> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- return Promise.resolve('')
- }
- if (!credential.access_token) {
- return Promise.resolve('')
+ if (!credential || !credential.access_token) {
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
// 剩余时间
@@ -361,7 +359,7 @@ export class BilibiliService extends PlatformBaseService {
/**
* 刷新AccessToken
- * @param userId
+ * @param accountId
* @param refreshToken
* @returns
*/
@@ -372,11 +370,11 @@ export class BilibiliService extends PlatformBaseService {
const accessTokenInfo
= await this.bilibiliApiService.refreshAccessToken(refreshToken)
if (!accessTokenInfo)
- return ''
+ throw new PlatformAuthExpiredException(this.platform, accountId)
const res = await this.saveOAuthCredential(accountId, accessTokenInfo)
if (!res)
- return ''
+ throw new PlatformAuthExpiredException(this.platform, accountId)
return accessTokenInfo.access_token
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts
index cc19c1fe..a0dc24f6 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts
@@ -13,6 +13,7 @@ import { OAuth2Credential } from '../../../libs/database/schema/oauth2Credential
import { KwaiOAuthCredentialsResponse } from '../../../libs/kwai/kwai.interfaces'
import { KwaiApiService } from '../../../libs/kwai/kwai.service'
import { PlatformBaseService } from '../base.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { KWAI_TIME_CONSTANTS } from './constants'
@Injectable()
@@ -40,7 +41,7 @@ export class KwaiService extends PlatformBaseService {
platform: this.platform,
})
if (!oauth2Credential) {
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential = {
result: 0,
@@ -75,15 +76,11 @@ export class KwaiService extends PlatformBaseService {
*/
async getAccessTokenAndRefresh(accountId: string) {
const accessTokenInfo = await this.getOAuth2Credential(accountId)
- if (!accessTokenInfo)
- return null
-
// 判断 refresh_token 是否过期
const isRefreshTokenExpired
= getCurrentTimestamp() >= accessTokenInfo.refresh_token_expires_in
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, accountId)
}
// 判断 access_token 是否过期
@@ -96,8 +93,7 @@ export class KwaiService extends PlatformBaseService {
accessTokenInfo.refresh_token,
)
if (!newAccountToken) {
- this.logger.warn(`Kwai account ${accountId} access_token refresh failed`)
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
const success = await this.saveOAuthCredential(accountId, newAccountToken)
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts
index 5a6c57c0..a8bdaef7 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts
@@ -6,6 +6,7 @@ import { getCurrentTimestamp } from '../../../common'
import { OAuth2Credential } from '../../../libs/database/schema/oauth2Credential.schema'
import { PlatformBaseService } from '../base.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { META_TIME_CONSTANTS, MetaRedisKeys } from './constants'
import { MetaUserOAuthCredential } from './meta.interfaces'
@@ -39,8 +40,7 @@ export class MetaBaseService extends PlatformBaseService {
},
)
if (!oauth2Credential) {
- this.logger.error(`No access token found for accountId: ${this.platform} ${accountId} in database`)
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential = JSON.parse(oauth2Credential.raw) as MetaUserOAuthCredential
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts
index 843039b7..074d33c9 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts
@@ -38,6 +38,7 @@ import {
UploadPhotoResponse,
} from '../../../libs/facebook/facebook.interfaces'
import { FacebookService as FacebookAPIService } from '../../../libs/facebook/facebook.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { MetaBaseService } from './base.service'
import { META_TIME_CONSTANTS, metaOAuth2ConfigMap, MetaRedisKeys } from './constants'
import { FacebookAccountResponse, FacebookPageCredentials, MetaFacebookPageResponse, MetaUserOAuthCredential } from './meta.interfaces'
@@ -57,10 +58,6 @@ export class FacebookService extends MetaBaseService {
accountId: string,
): Promise<MetaUserOAuthCredential | null> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- this.logger.warn(`No access token found for accountId: ${accountId}`)
- return null
- }
const now = getCurrentTimestamp()
const tokenExpiredAt = now + credential.expires_in
const requestTime
@@ -73,10 +70,7 @@ export class FacebookService extends MetaBaseService {
credential.access_token,
)
if (!refreshedToken) {
- this.logger.error(
- `Failed to refresh access token for accountId: ${accountId}`,
- )
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential.access_token = refreshedToken.access_token
credential.expires_in = refreshedToken.expires_in || META_TIME_CONSTANTS.FACEBOOK_LONG_LIVED_TOKEN_DEFAULT_EXPIRE
@@ -85,7 +79,7 @@ export class FacebookService extends MetaBaseService {
this.logger.error(
`Failed to save refreshed access token for accountId: ${accountId}`,
)
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
return credential
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts
index 0cc7e1a3..a728caa1 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts
@@ -17,6 +17,7 @@ import {
InstagramUserPostResponse,
} from '../../../libs/instagram/instagram.interfaces'
import { InstagramService as InstagramAPIService } from '../../../libs/instagram/instagram.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { MetaBaseService } from './base.service'
import { META_TIME_CONSTANTS } from './constants'
import { MetaPublishPlaintextCommentResponse, MetaUserOAuthCredential } from './meta.interfaces'
@@ -36,10 +37,6 @@ export class InstagramService extends MetaBaseService {
accountId: string,
): Promise<MetaUserOAuthCredential> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- this.logger.error(`No access token found for accountId: ${accountId}`)
- throw new Error(`No access token found for accountId: ${accountId}`)
- }
const now = getCurrentTimestamp()
const tokenExpiredAt = now + credential.expires_in
const requestTime
@@ -52,19 +49,13 @@ export class InstagramService extends MetaBaseService {
credential.access_token,
)
if (!refreshedToken) {
- this.logger.error(
- `Failed to refresh access token for accountId: ${accountId}`,
- )
- throw new Error(`Failed to refresh access token for accountId: ${accountId}`)
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential.access_token = refreshedToken.access_token
credential.expires_in = refreshedToken.expires_in
const saved = await this.saveOAuth2Credential(accountId, credential, this.platform)
if (!saved) {
- this.logger.error(
- `Failed to save refreshed access token for accountId: ${accountId}`,
- )
- throw new Error(`Failed to save refreshed access token for accountId: ${accountId}`)
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
return credential
}
@@ -75,8 +66,7 @@ export class InstagramService extends MetaBaseService {
const credential
= await this.instagramAPIService.refreshOAuthCredential(refresh_token)
if (!credential) {
- this.logger.error(`Failed to refresh access token`)
- return null
+ throw new PlatformAuthExpiredException(this.platform)
}
return credential
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/linkedin.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/linkedin.service.ts
index cdf92f89..54849dff 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/linkedin.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/linkedin.service.ts
@@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common'
import { getCurrentTimestamp } from '../../../common'
import { LinkedInShareRequest, LinkedInUploadRequest, MemberNetworkVisibility, ShareMediaCategory, UploadRecipe } from '../../../libs/linkedin/linkedin.interface'
import { LinkedinService as LinkedinAPIService } from '../../../libs/linkedin/linkedin.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { MetaBaseService } from './base.service'
import { META_TIME_CONSTANTS } from './constants'
import { MetaUserOAuthCredential } from './meta.interfaces'
@@ -21,10 +22,6 @@ export class LinkedinService extends MetaBaseService {
accountId: string,
): Promise<MetaUserOAuthCredential | null> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- this.logger.warn(`No access token found for accountId: ${accountId}`)
- return null
- }
const now = getCurrentTimestamp()
const tokenExpiredAt = now + credential.expires_in
const requestTime
@@ -37,19 +34,13 @@ export class LinkedinService extends MetaBaseService {
credential.access_token,
)
if (!refreshedToken) {
- this.logger.error(
- `Failed to refresh access token for accountId: ${accountId}`,
- )
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential.access_token = refreshedToken.access_token
credential.expires_in = refreshedToken.expires_in
const saved = await this.saveOAuth2Credential(accountId, credential, 'linkedin')
if (!saved) {
- this.logger.error(
- `Failed to save refreshed access token for accountId: ${accountId}`,
- )
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
return credential
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts
index 37a0d0f7..71fd8b5d 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts
@@ -15,6 +15,7 @@ import {
ThreadsSearchLocationRequest,
} from '../../../libs/threads/threads.interfaces'
import { ThreadsService as ThreadsAPIService } from '../../../libs/threads/threads.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { MetaBaseService } from './base.service'
import { MetaLocation, MetaUserOAuthCredential } from './meta.interfaces'
@@ -33,10 +34,6 @@ export class ThreadsService extends MetaBaseService {
accountId: string,
): Promise<MetaUserOAuthCredential> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- this.logger.warn(`No access token found for accountId: ${accountId}`)
- throw new Error(`No access token found for accountId: ${accountId}`)
- }
const now = getCurrentTimestamp()
if (now >= credential.expires_in) {
this.logger.debug(
@@ -46,10 +43,7 @@ export class ThreadsService extends MetaBaseService {
credential.access_token,
)
if (!refreshedToken) {
- this.logger.error(
- `Failed to refresh access token for accountId: ${accountId}`,
- )
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential.access_token = refreshedToken.access_token
credential.expires_in = refreshedToken.expires_in
@@ -58,7 +52,7 @@ export class ThreadsService extends MetaBaseService {
this.logger.error(
`Failed to save refreshed access token for accountId: ${accountId}`,
)
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
return credential
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/pinterest/pinterest.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/pinterest/pinterest.service.ts
index 5342541f..36265d8c 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/pinterest/pinterest.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/pinterest/pinterest.service.ts
@@ -23,6 +23,7 @@ import {
import { PinterestApiService } from '../../../libs/pinterest/pinterestApi.service'
import { PlatformBaseService } from '../base.service'
import { META_TIME_CONSTANTS } from '../meta/constants'
+import { PlatformAuthExpiredException } from '../platform.exception'
@Injectable()
export class PinterestService extends PlatformBaseService {
@@ -93,7 +94,7 @@ export class PinterestService extends PlatformBaseService {
platform: AccountType.PINTEREST,
})
if (!oauth2Credential) {
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential = {
status: ILoginStatus.success,
@@ -124,10 +125,6 @@ export class PinterestService extends PlatformBaseService {
private async authorize(accountId: string): Promise<AuthInfo | null> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- this.logger.warn(`No access token found for accountId: ${accountId}`)
- return null
- }
const now = getCurrentTimestamp()
if (credential.expires_in && now >= credential.expires_in) {
// attempt refresh using DB-stored refresh token
@@ -139,8 +136,7 @@ export class PinterestService extends PlatformBaseService {
}
const refreshed = await this.refreshOAuthCredential(refreshToken)
if (!refreshed) {
- this.logger.error(`Failed to refresh access token for accountId: ${accountId}`)
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
const saved = await this.saveOAuthCredential(accountId, {
access_token: refreshed.access_token,
@@ -149,8 +145,7 @@ export class PinterestService extends PlatformBaseService {
refresh_token_expires_in: refreshed.refresh_token_expires_in,
}, refreshed)
if (!saved) {
- this.logger.error(`Failed to save refreshed access token for accountId: ${accountId}`)
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
const updated = await this.getOAuth2Credential(accountId)
return updated
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/platform.exception.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/platform.exception.ts
new file mode 100644
index 00000000..e903538f
--- /dev/null
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/platform.exception.ts
@@ -0,0 +1,7 @@
+import { SocialMediaError } from '../../libs/exception'
+
+export class PlatformAuthExpiredException extends SocialMediaError {
+ constructor(platform: string, accountId?: string, message?: string) {
+ super(platform, 'GetAccessToken', 'AuthError', message || 'OAuth2 credential expired, please re-authorize', 401, 401, undefined, false, { accountId })
+ }
+}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/tiktok/tiktok.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/tiktok/tiktok.service.ts
index de98c4cd..b395106c 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/tiktok/tiktok.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/tiktok/tiktok.service.ts
@@ -10,7 +10,6 @@ import { randomBytes } from 'node:crypto'
import { Injectable, Logger } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { AccountStatus, AccountType, NewAccount } from '@yikart/aitoearn-server-client'
-import { AppException, ResponseCode } from '@yikart/common'
import { RedisService } from '@yikart/redis'
import { Model } from 'mongoose'
import { getCurrentTimestamp } from '../../../common'
@@ -30,6 +29,7 @@ import {
} from '../../../libs/tiktok/tiktok.interfaces'
import { TiktokService as TiktokApiService } from '../../../libs/tiktok/tiktok.service'
import { PlatformBaseService } from '../base.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { TIKTOK_DEFAULT_SCOPES, TIKTOK_TIME_CONSTANTS, TiktokRedisKeys } from './constants'
import {
PhotoSourceInfoDto,
@@ -115,7 +115,7 @@ export class TiktokService extends PlatformBaseService {
platform: this.platform,
})
if (!oauth2Credential) {
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential = {
access_token: oauth2Credential.accessToken,
@@ -250,10 +250,6 @@ export class TiktokService extends PlatformBaseService {
*/
private async getValidAccessToken(accountId: string): Promise<string> {
let tokenInfo = await this.getOAuth2Credential(accountId)
- if (!tokenInfo) {
- throw new AppException(ResponseCode.ChannelAccountNotAuthorized)
- }
-
// 检查是否需要刷新令牌
const currentTime = getCurrentTimestamp()
if (
@@ -263,9 +259,10 @@ export class TiktokService extends PlatformBaseService {
accountId,
tokenInfo.refresh_token,
)
- if (refreshedToken) {
- tokenInfo = refreshedToken
+ if (!refreshedToken) {
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
+ tokenInfo = refreshedToken
}
return tokenInfo.access_token
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/twitter/twitter.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/twitter/twitter.service.ts
index 1891f189..d310072c 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/twitter/twitter.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/twitter/twitter.service.ts
@@ -12,6 +12,7 @@ import { XMediaCategory, XMediaType } from '../../../libs/twitter/twitter.enum'
import { TwitterOAuthCredential, XChunkedMediaUploadRequest, XCreatePostRequest, XCreatePostResponse, XLikePostResponse, XMediaUploadInitRequest, XMediaUploadResponse, XPostDetailResponse, XRePostResponse, XUserTimelineRequest } from '../../../libs/twitter/twitter.interfaces'
import { TwitterService as TwitterApiService } from '../../../libs/twitter/twitter.service'
import { PlatformBaseService } from '../base.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
import { TWITTER_TIME_CONSTANTS, TwitterRedisKeys } from './constants'
import { UserTimelineDto } from './dto/twitter.dto'
import { TwitterOAuthTaskInfo } from './twitter.interfaces'
@@ -89,7 +90,7 @@ export class TwitterService extends PlatformBaseService {
platform: this.platform,
})
if (!oauth2Credential) {
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential = {
access_token: oauth2Credential.accessToken,
@@ -104,10 +105,6 @@ export class TwitterService extends PlatformBaseService {
accountId: string,
): Promise<TwitterOAuthCredential> {
const credential = await this.getOAuth2Credential(accountId)
- if (!credential) {
- this.logger.warn(`No access token found for accountId: ${accountId}`)
- throw new Error(`No access token found for accountId: ${accountId}`)
- }
const now = getCurrentTimestamp()
if (now >= credential.expires_in) {
this.logger.debug(
@@ -117,20 +114,14 @@ export class TwitterService extends PlatformBaseService {
credential.refresh_token,
)
if (!refreshedToken) {
- this.logger.error(
- `Failed to refresh access token for accountId: ${accountId}`,
- )
- return null
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
credential.access_token = refreshedToken.access_token
credential.refresh_token = refreshedToken.refresh_token
credential.expires_in = refreshedToken.expires_in
const saved = await this.saveOAuthCredential(accountId, credential)
if (!saved) {
- this.logger.error(
- `Failed to save refreshed access token for accountId: ${accountId}`,
- )
- throw new Error(`Failed to save refreshed access token for accountId: ${accountId}`)
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
return credential
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/youtube/youtube.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/youtube/youtube.service.ts
index 872a4c92..f5d52267 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/youtube/youtube.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/youtube/youtube.service.ts
@@ -26,6 +26,7 @@ import {
} from '../../../libs/database/schema/oauth2Credential.schema'
import { YoutubeApiService } from '../../../libs/youtube/youtubeApi.service'
import { PlatformBaseService } from '../base.service'
+import { PlatformAuthExpiredException } from '../platform.exception'
interface AuthTaskInfo {
state: string
@@ -480,7 +481,7 @@ export class YoutubeService extends PlatformBaseService {
const credential = await this.getOAuth2Credential(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, accountId)
}
const isTokenExpired = credential.expires_in <= getCurrentTimestamp()
@@ -490,14 +491,12 @@ export class YoutubeService extends PlatformBaseService {
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, accountId)
}
const isRefreshTokenExpired = credential.refresh_expires_in && credential.refresh_expires_in <= getCurrentTimestamp()
if (isRefreshTokenExpired) {
- 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, accountId)
}
// 刷新并获取新令牌
@@ -508,7 +507,7 @@ export class YoutubeService extends PlatformBaseService {
if (!accessToken) {
this.logger.error(`refresh Token failed for accountId: ${accountId}`)
- throw new AppException(ResponseCode.ChannelRefreshTokenFailed, { accountId })
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
return accessToken
@@ -2076,7 +2075,7 @@ export class YoutubeService extends PlatformBaseService {
const accessToken = await this.getUserAccessToken(accountId)
if (!accessToken) {
- return false
+ throw new PlatformAuthExpiredException(this.platform, accountId)
}
this.initializeYouTubeClient(accessToken)
return true
diff --git a/project/backend/apps/aitoearn-channel/src/libs/exception/interfaces.ts b/project/backend/apps/aitoearn-channel/src/libs/exception/interfaces.ts
index 6ebf3b6d..ca7c51ef 100644
--- a/project/backend/apps/aitoearn-channel/src/libs/exception/interfaces.ts
+++ b/project/backend/apps/aitoearn-channel/src/libs/exception/interfaces.ts
@@ -32,6 +32,9 @@ export interface ISocialMediaError {
* Error context information.
*/
export interface IErrorContext {
+ /** Account ID. */
+ accountId?: string
+
/** Request URL. */
url?: string