@@ -20,6 +20,7 @@ type WidgetAgentHealth = {
2020 widgetKeyConfigured ?: boolean ;
2121 anthropicConfigured ?: boolean ;
2222 proKeyConfigured ?: boolean ;
23+ errorCode ?: 'invalid_widget_key' | 'invalid_pro_key' ;
2324 error ?: string ;
2425} ;
2526
@@ -141,11 +142,10 @@ export function openWidgetChatModal(options: WidgetChatOptions): void {
141142 const headers : Record < string , string > = { 'X-Widget-Key' : getWidgetAgentKey ( ) } ;
142143 if ( isPro ) headers [ 'X-Pro-Key' ] = getProWidgetKey ( ) ;
143144 const res = await fetch ( widgetAgentHealthUrl ( ) , { headers } ) ;
144- let payload : WidgetAgentHealth | null = null ;
145- try { payload = await res . json ( ) as WidgetAgentHealth ; } catch { /* ignore */ }
145+ const payload = await parseWidgetAgentJson ( res ) ;
146146
147147 if ( ! res . ok ) {
148- const message = resolvePreflightMessage ( res . status , payload , isPro ) ;
148+ const message = resolvePreflightMessage ( res . status , payload ) ;
149149 preflightReady = false ;
150150 setReadinessState ( readinessEl , 'error' , message ) ;
151151 setFooterStatus ( footerStatusEl , message , 'error' ) ;
@@ -234,7 +234,8 @@ export function openWidgetChatModal(options: WidgetChatOptions): void {
234234 } ) ;
235235
236236 if ( ! res . ok || ! res . body ) {
237- throw new Error ( t ( 'widgets.serverError' , { status : res . status } ) ) ;
237+ const payload = await parseWidgetAgentJson ( res ) ;
238+ throw new Error ( resolveRequestErrorMessage ( res . status , payload ) ) ;
238239 }
239240
240241 let resultHtml = '' ;
@@ -367,13 +368,37 @@ function renderExampleChips(container: HTMLElement, inputEl: HTMLTextAreaElement
367368 }
368369}
369370
370- function resolvePreflightMessage ( status : number , payload : WidgetAgentHealth | null , isPro : boolean ) : string {
371- if ( status === 403 ) return isPro ? t ( 'widgets.preflightInvalidProKey' ) : t ( 'widgets.preflightInvalidKey' ) ;
371+ async function parseWidgetAgentJson ( res : Response ) : Promise < WidgetAgentHealth | null > {
372+ try {
373+ return await res . json ( ) as WidgetAgentHealth ;
374+ } catch {
375+ return null ;
376+ }
377+ }
378+
379+ function resolveWidgetAgentFailureMessage ( status : number , payload : WidgetAgentHealth | null ) : string | null {
380+ if ( status === 403 ) {
381+ return payload ?. errorCode === 'invalid_pro_key'
382+ ? t ( 'widgets.preflightInvalidProKey' )
383+ : t ( 'widgets.preflightInvalidKey' ) ;
384+ }
372385 if ( status === 503 && payload ?. proKeyConfigured === false ) return t ( 'widgets.preflightProUnavailable' ) ;
373386 if ( payload ?. anthropicConfigured === false ) return t ( 'widgets.preflightAiUnavailable' ) ;
387+ return null ;
388+ }
389+
390+ function resolvePreflightMessage ( status : number , payload : WidgetAgentHealth | null ) : string {
391+ const message = resolveWidgetAgentFailureMessage ( status , payload ) ;
392+ if ( message ) return message ;
374393 return t ( 'widgets.preflightUnavailable' ) ;
375394}
376395
396+ function resolveRequestErrorMessage ( status : number , payload : WidgetAgentHealth | null ) : string {
397+ const message = resolveWidgetAgentFailureMessage ( status , payload ) ;
398+ if ( message ) return message ;
399+ return t ( 'widgets.serverError' , { status } ) ;
400+ }
401+
377402function setReadinessState ( container : HTMLElement , tone : 'checking' | 'ready' | 'error' , text : string ) : void {
378403 container . className = `widget-chat-readiness is-${ tone } ` ;
379404 container . textContent = text ;
0 commit comments