@@ -16,6 +16,7 @@ import { confirm, select } from '@socketsecurity/registry/lib/prompts'
1616import { fetchSupportedScanFileNames } from './fetch-supported-scan-file-names.mts'
1717import { handleCreateNewScan } from './handle-create-new-scan.mts'
1818import constants from '../../constants.mts'
19+ import { debugApiRequest , debugApiResponse } from '../../utils/debug.mts'
1920import { formatErrorWithDetail } from '../../utils/errors.mts'
2021import { isReportSupportedFile } from '../../utils/glob.mts'
2122import { fetchListAllRepos } from '../repository/fetch-list-all-repos.mts'
@@ -390,12 +391,20 @@ async function downloadManifestFile({
390391 const fileUrl = `${ repoApiUrl } /contents/${ file } ?ref=${ defaultBranch } `
391392 debugDir ( 'inspect' , { fileUrl } )
392393
393- const downloadUrlResponse = await fetch ( fileUrl , {
394- method : 'GET' ,
395- headers : {
396- Authorization : `Bearer ${ githubToken } ` ,
397- } ,
398- } )
394+ debugApiRequest ( 'GET' , fileUrl )
395+ let downloadUrlResponse : Response
396+ try {
397+ downloadUrlResponse = await fetch ( fileUrl , {
398+ method : 'GET' ,
399+ headers : {
400+ Authorization : `Bearer ${ githubToken } ` ,
401+ } ,
402+ } )
403+ debugApiResponse ( 'GET' , fileUrl , downloadUrlResponse . status )
404+ } catch ( e ) {
405+ debugApiResponse ( 'GET' , fileUrl , undefined , e )
406+ throw e
407+ }
399408 debugFn ( 'notice' , 'complete: request' )
400409
401410 const downloadUrlText = await downloadUrlResponse . text ( )
@@ -448,7 +457,9 @@ async function streamDownloadWithFetch(
448457 let response // Declare response here to access it in catch if needed
449458
450459 try {
460+ debugApiRequest ( 'GET' , downloadUrl )
451461 response = await fetch ( downloadUrl )
462+ debugApiResponse ( 'GET' , downloadUrl , response . status )
452463
453464 if ( ! response . ok ) {
454465 const errorMsg = `Download failed due to bad server response: ${ response . status } ${ response . statusText } for ${ downloadUrl } `
@@ -483,6 +494,9 @@ async function streamDownloadWithFetch(
483494 // It resolves when the piping is fully complete and fileStream is closed.
484495 return { ok : true , data : localPath }
485496 } catch ( e ) {
497+ if ( ! response ) {
498+ debugApiResponse ( 'GET' , downloadUrl , undefined , e )
499+ }
486500 logger . fail (
487501 'An error was thrown while trying to download a manifest file... url:' ,
488502 downloadUrl ,
@@ -542,11 +556,19 @@ async function getLastCommitDetails({
542556 const commitApiUrl = `${ repoApiUrl } /commits?sha=${ defaultBranch } &per_page=1`
543557 debugFn ( 'inspect' , 'url: commit' , commitApiUrl )
544558
545- const commitResponse = await fetch ( commitApiUrl , {
546- headers : {
547- Authorization : `Bearer ${ githubToken } ` ,
548- } ,
549- } )
559+ debugApiRequest ( 'GET' , commitApiUrl )
560+ let commitResponse : Response
561+ try {
562+ commitResponse = await fetch ( commitApiUrl , {
563+ headers : {
564+ Authorization : `Bearer ${ githubToken } ` ,
565+ } ,
566+ } )
567+ debugApiResponse ( 'GET' , commitApiUrl , commitResponse . status )
568+ } catch ( e ) {
569+ debugApiResponse ( 'GET' , commitApiUrl , undefined , e )
570+ throw e
571+ }
550572
551573 const commitText = await commitResponse . text ( )
552574 debugFn ( 'inspect' , 'response: commit' , commitText )
@@ -646,12 +668,20 @@ async function getRepoDetails({
646668 const repoApiUrl = `${ githubApiUrl } /repos/${ orgGithub } /${ repoSlug } `
647669 debugDir ( 'inspect' , { repoApiUrl } )
648670
649- const repoDetailsResponse = await fetch ( repoApiUrl , {
650- method : 'GET' ,
651- headers : {
652- Authorization : `Bearer ${ githubToken } ` ,
653- } ,
654- } )
671+ let repoDetailsResponse : Response
672+ try {
673+ debugApiRequest ( 'GET' , repoApiUrl )
674+ repoDetailsResponse = await fetch ( repoApiUrl , {
675+ method : 'GET' ,
676+ headers : {
677+ Authorization : `Bearer ${ githubToken } ` ,
678+ } ,
679+ } )
680+ debugApiResponse ( 'GET' , repoApiUrl , repoDetailsResponse . status )
681+ } catch ( e ) {
682+ debugApiResponse ( 'GET' , repoApiUrl , undefined , e )
683+ throw e
684+ }
655685 logger . success ( `Request completed.` )
656686
657687 const repoDetailsText = await repoDetailsResponse . text ( )
@@ -702,12 +732,20 @@ async function getRepoBranchTree({
702732 const treeApiUrl = `${ repoApiUrl } /git/trees/${ defaultBranch } ?recursive=1`
703733 debugFn ( 'inspect' , 'url: tree' , treeApiUrl )
704734
705- const treeResponse = await fetch ( treeApiUrl , {
706- method : 'GET' ,
707- headers : {
708- Authorization : `Bearer ${ githubToken } ` ,
709- } ,
710- } )
735+ let treeResponse : Response
736+ try {
737+ debugApiRequest ( 'GET' , treeApiUrl )
738+ treeResponse = await fetch ( treeApiUrl , {
739+ method : 'GET' ,
740+ headers : {
741+ Authorization : `Bearer ${ githubToken } ` ,
742+ } ,
743+ } )
744+ debugApiResponse ( 'GET' , treeApiUrl , treeResponse . status )
745+ } catch ( e ) {
746+ debugApiResponse ( 'GET' , treeApiUrl , undefined , e )
747+ throw e
748+ }
711749
712750 const treeText = await treeResponse . text ( )
713751 debugFn ( 'inspect' , 'response: tree' , treeText )
0 commit comments