@@ -17,26 +17,30 @@ const context = {
1717const div = document . createElement ( 'div' ) ;
1818document . body . appendChild ( div ) ;
1919div . appendChild ( document . createTextNode ( 'Initializing...' ) ) ;
20+ try {
21+ const ldclient = initialize ( clientSideID ) ;
22+ const render = ( ) => {
23+ const flagValue = ldclient . variation ( flagKey , false ) ;
24+ const label = `The ${ flagKey } feature flag evaluates to ${ flagValue } .` ;
25+ document . body . style . background = flagValue ? '#00844B' : '#373841' ;
26+ div . replaceChild ( document . createTextNode ( label ) , div . firstChild as Node ) ;
27+ } ;
2028
21- const ldclient = initialize ( clientSideID ) ;
22-
23- function render ( ) {
24- const flagValue = ldclient . variation ( flagKey , false ) ;
25- const label = `The ${ flagKey } feature flag evaluates to ${ flagValue } .` ;
26- document . body . style . background = flagValue ? '#00844B' : '#373841' ;
27- div . replaceChild ( document . createTextNode ( label ) , div . firstChild as Node ) ;
28- }
29+ ldclient . on ( 'error' , ( ) => {
30+ div . replaceChild ( document . createTextNode ( 'Error caught in client SDK' ) , div . firstChild as Node ) ;
31+ } ) ;
2932
30- ldclient . identify ( context ) . then ( ( ) => {
31- ldclient . on ( 'initialized' , ( ) => {
32- div . replaceChild (
33- document . createTextNode ( 'SDK successfully initialized!' ) ,
34- div . firstChild as Node ,
35- ) ;
33+ ldclient . on ( 'change' , ( ) => {
34+ render ( ) ;
3635 } ) ;
37- ldclient . on ( 'failed' , ( ) => {
38- div . replaceChild ( document . createTextNode ( 'SDK failed to initialize' ) , div . firstChild as Node ) ;
36+
37+ ldclient . identify ( context ) . catch ( ( ) => {
38+ div . replaceChild ( document . createTextNode ( 'Error identifying client' ) , div . firstChild as Node ) ;
3939 } ) ;
40- ldclient . on ( 'ready' , render ) ;
41- ldclient . on ( 'change' , render ) ;
42- } ) ;
40+ } catch ( error ) {
41+ div . replaceChild (
42+ document . createTextNode ( `Error initializing LaunchDarkly client: ${ error } ` ) ,
43+ div . firstChild as Node ,
44+ ) ;
45+ document . body . style . background = '#373841' ;
46+ }
0 commit comments