@@ -13,6 +13,12 @@ export default {
1313 }
1414} ;
1515
16+ class GitHubApiError extends Error {
17+ constructor ( public status : number ) {
18+ super ( `GitHub API error: ${ status } ` ) ;
19+ }
20+ }
21+
1622async function ghFetch ( url : string , env : Env ) : Promise < any > {
1723 const res = await fetch ( url , {
1824 headers : {
@@ -23,7 +29,7 @@ async function ghFetch(url: string, env: Env): Promise<any> {
2329 }
2430 } ) ;
2531 if ( ! res . ok ) {
26- return new Response ( `GitHub API error: ${ res . status } ` , { status : 502 } ) ;
32+ throw new GitHubApiError ( res . status ) ;
2733 }
2834 return res . json ( ) ;
2935}
@@ -33,7 +39,15 @@ function apiBase(env: Env) {
3339}
3440
3541async function handleList ( env : Env ) {
36- const deliveries = await ghFetch ( `${ apiBase ( env ) } /deliveries?per_page=20` , env ) ;
42+ let deliveries ;
43+ try {
44+ deliveries = await ghFetch ( `${ apiBase ( env ) } /deliveries?per_page=20` , env ) ;
45+ } catch ( e ) {
46+ if ( e instanceof GitHubApiError ) {
47+ return new Response ( `GitHub API error: ${ e . status } ` , { status : 502 } ) ;
48+ }
49+ throw e ;
50+ }
3751
3852
3953 const rows = deliveries . map ( ( d : any ) => `
@@ -67,7 +81,15 @@ async function handleList(env: Env) {
6781}
6882
6983async function handleSingleDelivery ( id : string , env : Env ) {
70- const delivery = await ghFetch ( `${ apiBase ( env ) } /deliveries/${ id } ` , env ) ;
84+ let delivery ;
85+ try {
86+ delivery = await ghFetch ( `${ apiBase ( env ) } /deliveries/${ id } ` , env ) ;
87+ } catch ( e ) {
88+ if ( e instanceof GitHubApiError ) {
89+ return new Response ( `GitHub API error: ${ e . status } ` , { status : 502 } ) ;
90+ }
91+ throw e ;
92+ }
7193
7294 return new Response ( JSON . stringify ( delivery , null , 2 ) , {
7395 headers : { 'Content-Type' : 'application/json; charset=utf-8' }
0 commit comments