@@ -22,6 +22,28 @@ function b64_to_utf8(str: string) {
2222 return decodeURIComponent ( escape ( atob ( str ) ) ) ;
2323}
2424
25+ async function createGitHubError ( res : Response ) : Promise < Error > {
26+ const statusText = res . statusText || 'Unknown error' ;
27+ let detail = '' ;
28+ try {
29+ const json = await res . json ( ) ;
30+ if ( json && typeof json === 'object' && 'message' in json ) {
31+ detail = String ( ( json as any ) . message ) ;
32+ } else if ( typeof json === 'string' ) {
33+ detail = json ;
34+ }
35+ } catch {
36+ // ignore parse errors
37+ }
38+
39+ const message = detail
40+ ? `GitHub API error: ${ res . status } ${ statusText } - ${ detail } `
41+ : `GitHub API error: ${ res . status } ${ statusText } ` ;
42+ const error = new Error ( message ) ;
43+ ( error as any ) . status = res . status ;
44+ return error ;
45+ }
46+
2547export async function getFile ( path : string ) {
2648 try {
2749 const res = await fetch (
@@ -30,8 +52,7 @@ export async function getFile(path: string) {
3052 ) ;
3153 if ( res . status === 404 ) return null ;
3254 if ( ! res . ok ) {
33- const statusText = res . statusText || 'Unknown error' ;
34- throw new Error ( `GitHub API error: ${ res . status } ${ statusText } ` ) ;
55+ throw await createGitHubError ( res ) ;
3556 }
3657 const data = await res . json ( ) ;
3758 if ( data . content && ! Array . isArray ( data ) ) {
@@ -66,8 +87,7 @@ sha?: string)
6687 body : JSON . stringify ( body )
6788 } ) ;
6889 if ( ! res . ok ) {
69- const statusText = res . statusText || 'Unknown error' ;
70- throw new Error ( `GitHub API error: ${ res . status } ${ statusText } ` ) ;
90+ throw await createGitHubError ( res ) ;
7191 }
7292 return res . json ( ) ;
7393 } catch ( error : any ) {
@@ -83,8 +103,7 @@ export async function listFiles(path: string) {
83103 ) ;
84104 if ( res . status === 404 ) return [ ] ;
85105 if ( ! res . ok ) {
86- const statusText = res . statusText || 'Unknown error' ;
87- throw new Error ( `GitHub API error: ${ res . status } ${ statusText } ` ) ;
106+ throw await createGitHubError ( res ) ;
88107 }
89108 const data = await res . json ( ) ;
90109 if ( Array . isArray ( data ) ) return data ;
@@ -103,8 +122,7 @@ export async function deleteFile(path: string, message: string, sha: string) {
103122 body : JSON . stringify ( { message, sha, branch } )
104123 } ) ;
105124 if ( ! res . ok ) {
106- const statusText = res . statusText || 'Unknown error' ;
107- throw new Error ( `GitHub API error: ${ res . status } ${ statusText } ` ) ;
125+ throw await createGitHubError ( res ) ;
108126 }
109127 } catch ( error : any ) {
110128 handleApiError ( error , 'deleteFile' ) ;
0 commit comments