File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -144,18 +144,43 @@ export function isRateLimitError(e: unknown): boolean {
144144 return false ;
145145}
146146
147- export function getErrorCode ( e : any ) : string | undefined {
147+ function isObject ( value : unknown ) : value is Record < string , unknown > {
148+ return typeof value === 'object' && value !== null ;
149+ }
150+
151+ export function getErrorCode ( e : unknown ) : string | undefined {
152+ if ( ! isObject ( e ) ) {
153+ return undefined ;
154+ }
155+
148156 if ( e . status !== undefined ) {
149157 return String ( e . status ) ;
150- } else if ( e . networkError ?. statusCode !== undefined ) {
151- return String ( e . networkError . statusCode ) ;
152- } else if ( e . graphQLErrors ?. [ 0 ] ?. extensions ?. code ) {
153- return String ( e . graphQLErrors [ 0 ] . extensions . code ) ;
154- } else if ( e . code !== undefined ) {
158+ }
159+
160+ const networkError = e . networkError ;
161+ if ( isObject ( networkError ) && networkError . statusCode !== undefined ) {
162+ return String ( networkError . statusCode ) ;
163+ }
164+
165+ const graphQLErrors = e . graphQLErrors ;
166+ if ( Array . isArray ( graphQLErrors ) ) {
167+ const firstGraphQLError = graphQLErrors [ 0 ] ;
168+ if ( isObject ( firstGraphQLError ) ) {
169+ const extensions = firstGraphQLError . extensions ;
170+ if ( isObject ( extensions ) && extensions . code !== undefined ) {
171+ return String ( extensions . code ) ;
172+ }
173+ }
174+ }
175+
176+ if ( e . code !== undefined ) {
155177 return String ( e . code ) ;
156- } else if ( e . name ) {
178+ }
179+
180+ if ( typeof e . name === 'string' && e . name ) {
157181 return e . name ;
158182 }
183+
159184 return undefined ;
160185}
161186
You can’t perform that action at this time.
0 commit comments