@@ -50,10 +50,10 @@ export const migrateUserStateAddContactId = async (): Promise<{
5050 return { changed : false } ;
5151 }
5252
53- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- data could be undefined
53+
5454 if (
55- ! existingConfig . user ?. data ? .contactId &&
56- existingConfig . user ?. data ? .userId
55+ ! existingConfig . user ?. data . contactId &&
56+ existingConfig . user ?. data . userId
5757 ) {
5858 return { changed : true } ;
5959 }
@@ -63,7 +63,9 @@ export const migrateUserStateAddContactId = async (): Promise<{
6363} ;
6464
6565// Helper: Handle missing field error
66- function handleMissingField ( field : string ) {
66+ function handleMissingField (
67+ field : string
68+ ) : Result < void , MissingFieldError > {
6769 const logger = Logger . getInstance ( ) ;
6870 logger . debug ( `No ${ field } provided` ) ;
6971 return err ( {
@@ -91,21 +93,21 @@ async function syncEnvironmentStateIfExpired(
9193
9294 if ( environmentStateResponse . ok ) {
9395 return ok ( environmentStateResponse . data ) ;
94- } else {
95- logger . error (
96- `Error fetching environment state: ${ environmentStateResponse . error . code } - ${ environmentStateResponse . error . responseMessage ?? "" } `
97- ) ;
98-
99- return err ( {
100- code : "network_error" ,
101- message : "Error fetching environment state" ,
102- status : 500 ,
103- url : new URL (
104- `${ configInput . appUrl } /api/v1/client/${ configInput . environmentId } /environment`
105- ) ,
106- responseMessage : environmentStateResponse . error . message ,
107- } ) ;
10896 }
97+
98+ logger . error (
99+ `Error fetching environment state: ${ environmentStateResponse . error . code } - ${ environmentStateResponse . error . responseMessage ?? "" } `
100+ ) ;
101+
102+ return err ( {
103+ code : "network_error" ,
104+ message : "Error fetching environment state" ,
105+ status : 500 ,
106+ url : new URL (
107+ `${ configInput . appUrl } /api/v1/client/${ configInput . environmentId } /environment`
108+ ) ,
109+ responseMessage : environmentStateResponse . error . message ,
110+ } ) ;
109111}
110112
111113// Helper: Sync user state if expired
@@ -125,7 +127,7 @@ async function syncUserStateIfExpired(
125127
126128 logger . debug ( "Person state expired. Syncing." ) ;
127129
128- if ( userState ?. data ? .userId ) {
130+ if ( userState ?. data . userId ) {
129131 const updatesResponse = await sendUpdatesToBackend ( {
130132 appUrl : configInput . appUrl ,
131133 environmentId : configInput . environmentId ,
@@ -135,23 +137,23 @@ async function syncUserStateIfExpired(
135137 } ) ;
136138 if ( updatesResponse . ok ) {
137139 return ok ( updatesResponse . data . state ) ;
138- } else {
139- logger . error (
140- `Error updating user state: ${ updatesResponse . error . code } - ${ updatesResponse . error . responseMessage ?? "" } `
141- ) ;
142- return err ( {
143- code : "network_error" ,
144- message : "Error updating user state" ,
145- status : 500 ,
146- url : new URL (
147- `${ configInput . appUrl } /api/v1/client/${ configInput . environmentId } /update/contacts/${ userState . data . userId } `
148- ) ,
149- responseMessage : "Unknown error" ,
150- } as const ) ;
151140 }
152- } else {
153- return ok ( DEFAULT_USER_STATE_NO_USER_ID ) ;
141+
142+ logger . error (
143+ `Error updating user state: ${ updatesResponse . error . code } - ${ updatesResponse . error . responseMessage ?? "" } `
144+ ) ;
145+ return err ( {
146+ code : "network_error" ,
147+ message : "Error updating user state" ,
148+ status : 500 ,
149+ url : new URL (
150+ `${ configInput . appUrl } /api/v1/client/${ configInput . environmentId } /update/contacts/${ userState . data . userId } `
151+ ) ,
152+ responseMessage : "Unknown error" ,
153+ } as const ) ;
154154 }
155+
156+ return ok ( DEFAULT_USER_STATE_NO_USER_ID ) ;
155157}
156158
157159// Helper: Update app config with synced states
@@ -199,22 +201,35 @@ const createNewConfigAndSync = async (
199201 appUrl : configInput . appUrl ,
200202 environmentId : configInput . environmentId ,
201203 } ) ;
202- if ( ! environmentStateResponse . ok ) {
203- throw environmentStateResponse . error ;
204+
205+ if ( environmentStateResponse . ok ) {
206+ const personState = DEFAULT_USER_STATE_NO_USER_ID ;
207+ const environmentState = environmentStateResponse . data ;
208+ const filteredSurveys = filterSurveys ( environmentState , personState ) ;
209+ appConfig . update ( {
210+ appUrl : configInput . appUrl ,
211+ environmentId : configInput . environmentId ,
212+ user : personState ,
213+ environment : environmentState ,
214+ filteredSurveys,
215+ } ) ;
216+ return ;
204217 }
205- const personState = DEFAULT_USER_STATE_NO_USER_ID ;
206- const environmentState = environmentStateResponse . data ;
207- const filteredSurveys = filterSurveys ( environmentState , personState ) ;
208- appConfig . update ( {
209- appUrl : configInput . appUrl ,
210- environmentId : configInput . environmentId ,
211- user : personState ,
212- environment : environmentState ,
213- filteredSurveys,
218+
219+ await handleErrorOnFirstSetup ( {
220+ code : environmentStateResponse . error . code ,
221+ responseMessage :
222+ environmentStateResponse . error . responseMessage ??
223+ environmentStateResponse . error . message ,
214224 } ) ;
215- } catch ( e ) {
225+ } catch ( e : unknown ) {
226+ const setupError = normalizeSetupError ( e ) ;
216227 await handleErrorOnFirstSetup (
217- e as { code : string ; responseMessage : string }
228+ {
229+ code : setupError . code ?? "network_error" ,
230+ responseMessage :
231+ setupError . responseMessage ?? setupError . message ?? "Unknown error" ,
232+ }
218233 ) ;
219234 }
220235} ;
@@ -260,10 +275,10 @@ const finalizeSetup = (): void => {
260275} ;
261276
262277// Helper: Load existing config
263- const loadExistingConfig = async (
278+ const loadExistingConfig = (
264279 appConfig : RNConfig ,
265280 logger : ReturnType < typeof Logger . getInstance >
266- ) : Promise < TConfig | undefined > => {
281+ ) : TConfig | undefined => {
267282 let existingConfig : TConfig | undefined ;
268283 try {
269284 existingConfig = appConfig . get ( ) ;
@@ -294,7 +309,7 @@ export const setup = async (
294309 return okVoid ( ) ;
295310 }
296311
297- const existingConfig = await loadExistingConfig ( appConfig , logger ) ;
312+ const existingConfig = loadExistingConfig ( appConfig , logger ) ;
298313 if ( shouldReturnEarlyForErrorState ( existingConfig , logger ) ) {
299314 return okVoid ( ) ;
300315 }
@@ -369,8 +384,6 @@ export const checkSetup = (): Result<void, NotSetupError> => {
369384
370385 return okVoid ( ) ;
371386} ;
372-
373- // eslint-disable-next-line @typescript-eslint/require-await -- disabled for now
374387export const tearDown = async ( ) : Promise < void > => {
375388 const logger = Logger . getInstance ( ) ;
376389 const appConfig = await RNConfig . getInstance ( ) ;
@@ -425,3 +438,27 @@ export const handleErrorOnFirstSetup = async (e: {
425438
426439 throw new Error ( "Could not set up formbricks" ) ;
427440} ;
441+
442+ const normalizeSetupError = (
443+ error : unknown
444+ ) : Partial < {
445+ code : string ;
446+ responseMessage : string ;
447+ message : string ;
448+ } > => {
449+ if ( typeof error !== "object" || error === null ) {
450+ return { } ;
451+ }
452+
453+ const candidate = error as Record < string , unknown > ;
454+
455+ return {
456+ code : typeof candidate . code === "string" ? candidate . code : undefined ,
457+ responseMessage :
458+ typeof candidate . responseMessage === "string"
459+ ? candidate . responseMessage
460+ : undefined ,
461+ message :
462+ typeof candidate . message === "string" ? candidate . message : undefined ,
463+ } ;
464+ } ;
0 commit comments