@@ -3,6 +3,19 @@ import { faker } from '@faker-js/faker';
33
44import { hash } from '../models/helpers' ;
55
6+ async function withErrorLogging < T > ( operation : string , fn : ( ) => Promise < T > ) : Promise < T > {
7+ try {
8+ return await fn ( ) ;
9+ } catch ( e : any ) {
10+ console . error ( `[usersService] ${ operation } failed:` ) ;
11+ console . error ( ' Status:' , e . status ) ;
12+ console . error ( ' Message:' , e . message ) ;
13+ console . error ( ' Errors:' , JSON . stringify ( e . errors , null , 2 ) ) ;
14+ console . error ( ' Clerk Trace ID:' , e . clerkTraceId ) ;
15+ throw e ;
16+ }
17+ }
18+
619type FakeUserOptions = {
720 /**
821 * A fictional email is an email that contains +clerk_test and can always be verified using 424242 as the OTP. No email will be sent.
@@ -126,15 +139,17 @@ export const createUserService = (clerkClient: ClerkClient) => {
126139 } ;
127140 } ,
128141 createBapiUser : async fakeUser => {
129- return await clerkClient . users . createUser ( {
130- emailAddress : fakeUser . email !== undefined ? [ fakeUser . email ] : undefined ,
131- password : fakeUser . password ,
132- firstName : fakeUser . firstName ,
133- lastName : fakeUser . lastName ,
134- phoneNumber : fakeUser . phoneNumber !== undefined ? [ fakeUser . phoneNumber ] : undefined ,
135- username : fakeUser . username ,
136- skipPasswordRequirement : fakeUser . password === undefined ,
137- } ) ;
142+ return withErrorLogging ( 'createBapiUser' , ( ) =>
143+ clerkClient . users . createUser ( {
144+ emailAddress : fakeUser . email !== undefined ? [ fakeUser . email ] : undefined ,
145+ password : fakeUser . password ,
146+ firstName : fakeUser . firstName ,
147+ lastName : fakeUser . lastName ,
148+ phoneNumber : fakeUser . phoneNumber !== undefined ? [ fakeUser . phoneNumber ] : undefined ,
149+ username : fakeUser . username ,
150+ skipPasswordRequirement : fakeUser . password === undefined ,
151+ } ) ,
152+ ) ;
138153 } ,
139154 getOrCreateUser : async fakeUser => {
140155 const existingUser = await self . getUser ( { email : fakeUser . email } ) ;
@@ -147,10 +162,12 @@ export const createUserService = (clerkClient: ClerkClient) => {
147162 let id = opts . id ;
148163
149164 if ( ! id ) {
150- const { data : users } = await clerkClient . users . getUserList ( {
151- emailAddress : [ opts . email ] ,
152- phoneNumber : [ opts . phoneNumber ] ,
153- } ) ;
165+ const { data : users } = await withErrorLogging ( 'getUserList' , ( ) =>
166+ clerkClient . users . getUserList ( {
167+ emailAddress : [ opts . email ] ,
168+ phoneNumber : [ opts . phoneNumber ] ,
169+ } ) ,
170+ ) ;
154171 id = users [ 0 ] ?. id ;
155172 }
156173
@@ -159,12 +176,12 @@ export const createUserService = (clerkClient: ClerkClient) => {
159176 return ;
160177 }
161178
162- await clerkClient . users . deleteUser ( id ) ;
179+ await withErrorLogging ( 'deleteUser' , ( ) => clerkClient . users . deleteUser ( id ) ) ;
163180 } ,
164181 getUser : async ( opts : { id ?: string ; email ?: string } ) => {
165182 if ( opts . id ) {
166183 try {
167- const user = await clerkClient . users . getUser ( opts . id ) ;
184+ const user = await withErrorLogging ( 'getUser' , ( ) => clerkClient . users . getUser ( opts . id ) ) ;
168185 return user ;
169186 } catch ( err ) {
170187 console . log ( `Error fetching user "${ opts . id } ": ${ err . message } ` ) ;
@@ -173,7 +190,9 @@ export const createUserService = (clerkClient: ClerkClient) => {
173190 }
174191
175192 if ( opts . email ) {
176- const { data : users } = await clerkClient . users . getUserList ( { emailAddress : [ opts . email ] } ) ;
193+ const { data : users } = await withErrorLogging ( 'getUserList' , ( ) =>
194+ clerkClient . users . getUserList ( { emailAddress : [ opts . email ] } ) ,
195+ ) ;
177196 if ( users . length > 0 ) {
178197 return users [ 0 ] ;
179198 } else {
@@ -186,33 +205,41 @@ export const createUserService = (clerkClient: ClerkClient) => {
186205 } ,
187206 createFakeOrganization : async userId => {
188207 const name = faker . animal . dog ( ) ;
189- const organization = await clerkClient . organizations . createOrganization ( {
190- name : faker . animal . dog ( ) ,
191- createdBy : userId ,
192- } ) ;
208+ const organization = await withErrorLogging ( 'createOrganization' , ( ) =>
209+ clerkClient . organizations . createOrganization ( {
210+ name : faker . animal . dog ( ) ,
211+ createdBy : userId ,
212+ } ) ,
213+ ) ;
193214 return {
194215 name,
195216 organization,
196- delete : ( ) => clerkClient . organizations . deleteOrganization ( organization . id ) ,
217+ delete : ( ) =>
218+ withErrorLogging ( 'deleteOrganization' , ( ) => clerkClient . organizations . deleteOrganization ( organization . id ) ) ,
197219 } satisfies FakeOrganization ;
198220 } ,
199221 createFakeAPIKey : async ( userId : string ) => {
200222 const TWENTY_MINUTES = 20 * 60 ;
201223
202- const apiKey = await clerkClient . apiKeys . create ( {
203- subject : userId ,
204- name : `Integration Test - ${ faker . string . uuid ( ) } ` ,
205- secondsUntilExpiration : TWENTY_MINUTES ,
206- } ) ;
224+ const apiKey = await withErrorLogging ( 'createAPIKey' , ( ) =>
225+ clerkClient . apiKeys . create ( {
226+ subject : userId ,
227+ name : `Integration Test - ${ faker . string . uuid ( ) } ` ,
228+ secondsUntilExpiration : TWENTY_MINUTES ,
229+ } ) ,
230+ ) ;
207231
208232 return {
209233 apiKey,
210234 secret : apiKey . secret ?? '' ,
211- revoke : ( ) => clerkClient . apiKeys . revoke ( { apiKeyId : apiKey . id , revocationReason : 'For testing purposes' } ) ,
235+ revoke : ( ) =>
236+ withErrorLogging ( 'revokeAPIKey' , ( ) =>
237+ clerkClient . apiKeys . revoke ( { apiKeyId : apiKey . id , revocationReason : 'For testing purposes' } ) ,
238+ ) ,
212239 } satisfies FakeAPIKey ;
213240 } ,
214241 passwordCompromised : async ( userId : string ) => {
215- await clerkClient . users . __experimental_passwordCompromised ( userId ) ;
242+ await withErrorLogging ( 'passwordCompromised' , ( ) => clerkClient . users . __experimental_passwordCompromised ( userId ) ) ;
216243 } ,
217244 } ;
218245
0 commit comments