@@ -541,10 +541,16 @@ async function fetchApiContentsRemote(
541541 branch : string ,
542542 startingPath : string ,
543543) : Promise < Array < GitHubFileNode > | null > {
544+ const githubToken = env . GITHUB_AUTH_TOKEN
545+ const hasConfiguredGitHubToken =
546+ Boolean ( githubToken ) && githubToken !== 'USE_A_REAL_KEY_IN_PRODUCTION'
547+
544548 const fetchOptions : RequestInit = {
545549 headers : {
546550 'X-GitHub-Api-Version' : '2022-11-28' ,
547- Authorization : `Bearer ${ env . GITHUB_AUTH_TOKEN } ` ,
551+ ...( hasConfiguredGitHubToken
552+ ? { Authorization : `Bearer ${ githubToken } ` }
553+ : { } ) ,
548554 } ,
549555 }
550556 const res = await fetch (
@@ -556,8 +562,42 @@ async function fetchApiContentsRemote(
556562 if ( res . status === 404 ) {
557563 return null
558564 }
565+
566+ const githubRequestId = res . headers . get ( 'x-github-request-id' )
567+ const rateLimitLimit = res . headers . get ( 'x-ratelimit-limit' )
568+ const rateLimitRemaining = res . headers . get ( 'x-ratelimit-remaining' )
569+ const rateLimitReset = res . headers . get ( 'x-ratelimit-reset' )
570+
571+ let errorBody = ''
572+ try {
573+ errorBody = ( await res . text ( ) ) . replace ( / \s + / g, ' ' ) . trim ( )
574+ } catch {
575+ // Ignore parse failures for error response body
576+ }
577+
578+ if ( res . status === 403 ) {
579+ console . error ( '[GitHub API] 403 while fetching repository contents' , {
580+ repo,
581+ branch,
582+ startingPath,
583+ hasConfiguredGitHubToken,
584+ githubRequestId,
585+ rateLimitLimit,
586+ rateLimitRemaining,
587+ rateLimitReset,
588+ errorBody : errorBody . slice ( 0 , 500 ) ,
589+ } )
590+ }
591+
592+ const hint =
593+ res . status === 403
594+ ? rateLimitRemaining === '0'
595+ ? 'GitHub rate limit exceeded.'
596+ : 'GitHub forbidden. Check token permissions/access.'
597+ : 'GitHub request failed.'
598+
559599 throw new Error (
560- `Failed to fetch repo contents for ${ repo } /${ branch } /${ startingPath } : Status is ${ res . statusText } - ${ res . status } ` ,
600+ `${ hint } Failed to fetch repo contents for ${ repo } /${ branch } /${ startingPath } : Status is ${ res . statusText } - ${ res . status } . requestId= ${ githubRequestId ?? 'unknown' } rateLimitRemaining= ${ rateLimitRemaining ?? 'unknown' } rateLimitLimit= ${ rateLimitLimit ?? 'unknown' } rateLimitReset= ${ rateLimitReset ?? 'unknown' } ${ errorBody ? ` body= ${ errorBody . slice ( 0 , 500 ) } ` : '' } ` ,
561601 )
562602 }
563603
0 commit comments