@@ -71,15 +71,29 @@ async function denialFromResponse(
7171 }
7272}
7373
74- /** Own-property `code` lookup at the top level or under `error`. No coercion, no accessors. */
74+ function ownStringField ( container : Record < string , unknown > , field : string ) : string | undefined {
75+ const descriptor = Object . getOwnPropertyDescriptor ( container , field ) ;
76+ return descriptor && "value" in descriptor && typeof descriptor . value === "string"
77+ ? descriptor . value
78+ : undefined ;
79+ }
80+
81+ /** Own-property `code` lookup at the top level or under `error` / `detail`. No coercion or accessors. */
7582function structuredDenialCode ( payload : unknown ) : string | undefined {
7683 if ( payload === null || typeof payload !== "object" || Array . isArray ( payload ) ) return undefined ;
77- const direct = ( payload as Record < string , unknown > ) . code ;
78- if ( typeof direct === "string" ) return direct ;
79- const error = ( payload as Record < string , unknown > ) . error ;
80- if ( error === null || typeof error !== "object" || Array . isArray ( error ) ) return undefined ;
81- const nested = ( error as Record < string , unknown > ) . code ;
82- return typeof nested === "string" ? nested : undefined ;
84+ const record = payload as Record < string , unknown > ;
85+ const direct = ownStringField ( record , "code" ) ;
86+ if ( direct !== undefined ) return direct ;
87+
88+ for ( const field of [ "error" , "detail" ] as const ) {
89+ const descriptor = Object . getOwnPropertyDescriptor ( record , field ) ;
90+ if ( ! descriptor || ! ( "value" in descriptor ) ) continue ;
91+ const nested = descriptor . value ;
92+ if ( nested === null || typeof nested !== "object" || Array . isArray ( nested ) ) continue ;
93+ const code = ownStringField ( nested as Record < string , unknown > , "code" ) ;
94+ if ( code !== undefined ) return code ;
95+ }
96+ return undefined ;
8397}
8498
8599const RESET_ELIGIBLE_CODES : ReadonlySet < string > = new Set ( RESET_ELIGIBLE_CODE_VALUES ) ;
0 commit comments