File tree Expand file tree Collapse file tree
apps/dashboard-api/src/controllers Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -87,20 +87,35 @@ const clearGithubStateCookie = (res) => {
8787 } ) ;
8888} ;
8989
90+ const OAUTH_FETCH_TIMEOUT_MS = 10000 ;
91+
9092const fetchJson = async ( url , options , defaultMessage ) => {
91- const response = await fetch ( url , options ) ;
92- const payload = await response . json ( ) . catch ( ( ) => null ) ;
93-
94- if ( ! response . ok ) {
95- const message =
96- payload ?. error_description ||
97- payload ?. error ||
98- payload ?. message ||
99- defaultMessage ;
100- throw new Error ( message ) ;
101- }
93+ // Prevent OAuth requests from hanging forever
94+ const controller = new AbortController ( ) ;
95+ const timeout = setTimeout ( ( ) => controller . abort ( ) , OAUTH_FETCH_TIMEOUT_MS ) ;
96+
97+ try {
98+ const response = await fetch ( url , { signal : controller . signal , ...options } ) ;
99+ const payload = await response . json ( ) . catch ( ( ) => null ) ;
100+
101+ if ( ! response . ok ) {
102+ const message =
103+ payload ?. error_description ||
104+ payload ?. error ||
105+ payload ?. message ||
106+ defaultMessage ;
107+ throw new Error ( message ) ;
108+ }
102109
103- return payload ;
110+ return payload ;
111+ } catch ( err ) {
112+ if ( err . name === 'AbortError' ) {
113+ throw new Error ( 'OAuth request timed out.' ) ;
114+ }
115+ throw err ;
116+ } finally {
117+ clearTimeout ( timeout ) ;
118+ }
104119} ;
105120
106121const exchangeGithubCodeForToken = async ( { code, req } ) => {
You can’t perform that action at this time.
0 commit comments