@@ -4,7 +4,7 @@ import { RestEndpointMethodTypes } from "@octokit/rest";
44
55import AnonymousError from "../AnonymousError" ;
66import { isConnected } from "../../server/database" ;
7- import { octokit } from "../GitHubUtils" ;
7+ import { checkToken , octokit } from "../GitHubUtils" ;
88import { IRepositoryDocument } from "../model/repositories/repositories.types" ;
99import RepositoryModel from "../model/repositories/repositories.model" ;
1010
@@ -306,13 +306,36 @@ export async function getRepositoryFromGitHub(opt: {
306306 } )
307307 ) . data as RestEndpointMethodTypes [ "repos" ] [ "get" ] [ "response" ] [ "data" ] ;
308308 } catch ( idError ) {
309+ const idStatus = ( idError as { status ?: number } ) . status ;
310+ if (
311+ idStatus === 401 ||
312+ idStatus === 403 ||
313+ ! ( await checkToken ( opt . accessToken ) )
314+ ) {
315+ throw new AnonymousError ( "token_expired" , {
316+ httpStatus : 401 ,
317+ object : { owner : opt . owner , repo : opt . repo } ,
318+ cause : idError as Error ,
319+ } ) ;
320+ }
309321 throw new AnonymousError ( "repo_not_found" , {
310- httpStatus : ( idError as { status ?: number } ) . status || 404 ,
322+ httpStatus : idStatus || 404 ,
311323 object : { owner : opt . owner , repo : opt . repo } ,
312324 cause : idError as Error ,
313325 } ) ;
314326 }
315327 } else {
328+ if (
329+ status === 401 ||
330+ status === 403 ||
331+ ! ( await checkToken ( opt . accessToken ) )
332+ ) {
333+ throw new AnonymousError ( "token_expired" , {
334+ httpStatus : 401 ,
335+ object : { owner : opt . owner , repo : opt . repo } ,
336+ cause : error as Error ,
337+ } ) ;
338+ }
316339 throw new AnonymousError ( "repo_not_found" , {
317340 httpStatus : status ,
318341 object : {
0 commit comments