@@ -106,57 +106,71 @@ export class KlaviyoSdkService extends KlaviyoService {
106106
107107 private async createOrUpdateProfile ( body : KlaviyoRequestType , create : boolean ) {
108108 try {
109- return await ( create ? Profiles . createProfile ( body ) : Profiles . updateProfile ( body . data . id ! , body ) ) ;
109+ return await this . processProfileOperation ( body , create ) ;
110110 } catch ( e : any ) {
111- if ( e . status === 400 ) {
112- let errorCauses ;
113- try {
114- errorCauses = ( e . response ?. data ?. errors || [ ] ) . map ( ( err : any ) => err ?. source ?. pointer ) ;
115- } catch ( e ) {
116- logger . error ( 'Error getting error source pointer from error response' , e ) ;
117- throw new StatusError (
118- 400 ,
119- `Bad request, error getting error source pointer from error response. Request body: ${ JSON . stringify (
120- body ,
121- ) } `,
122- ) ;
123- }
124- if ( errorCauses . includes ( '/data/attributes/phone_number' ) ) {
125- logger . info (
126- `Invalid phone number when ${
127- create ? 'creating' : 'updating'
128- } profile. Retrying after removing phone number from profile...`,
129- e . response ?. data ,
130- ) ;
131- const modifiedBody : any = {
132- data : {
133- ...body . data ,
134- attributes : {
135- ...( body . data as any ) . attributes ,
136- phoneNumber : undefined ,
137- } ,
138- } ,
139- } ;
140- try {
141- return await ( create
142- ? Profiles . createProfile ( modifiedBody )
143- : Profiles . updateProfile ( modifiedBody . data ?. id , modifiedBody ) ) ;
144- } catch ( e : any ) {
145- logger . error (
146- `Error ${
147- create ? 'creating' : 'updating'
148- } profile in Klaviyo after removing phone_number. Response code ${ e . status } , ${ e . message } `,
149- e . response ?. data || e ,
150- ) ;
151- throw e ;
152- }
153- }
111+ if ( e . status !== 400 ) {
112+ logger . error (
113+ `Error ${ create ? 'creating' : 'updating' } profile in Klaviyo. Response code ${ e . status } , ${
114+ e . message
115+ } `,
116+ e ,
117+ ) ;
118+ throw e ;
119+ }
120+
121+ const errorCauses = this . extractErrorCauses ( e ) ;
122+ if ( errorCauses . includes ( '/data/attributes/phone_number' ) ) {
123+ return this . retryProfileOperationWithoutPhoneNumber ( body , create , e ) ;
154124 }
125+
126+ throw new StatusError (
127+ 400 ,
128+ `Bad request. Error causes: ${ errorCauses . join ( ', ' ) } . Request body: ${ JSON . stringify ( body ) } ` ,
129+ ) ;
130+ }
131+ }
132+
133+ private async processProfileOperation ( body : KlaviyoRequestType , create : boolean ) {
134+ return create ? Profiles . createProfile ( body ) : Profiles . updateProfile ( body . data . id ! , body ) ;
135+ }
136+
137+ private extractErrorCauses ( e : any ) : string [ ] {
138+ try {
139+ return ( e . response ?. data ?. errors || [ ] ) . map ( ( err : any ) => err ?. source ?. pointer ) ;
140+ } catch ( error ) {
141+ logger . error ( 'Error getting error source pointer from error response' , error ) ;
142+ throw new StatusError ( 400 , 'Bad request, error getting error source pointer from error response.' ) ;
143+ }
144+ }
145+
146+ private async retryProfileOperationWithoutPhoneNumber ( body : KlaviyoRequestType , create : boolean , e : any ) {
147+ logger . info (
148+ `Invalid phone number when ${
149+ create ? 'creating' : 'updating'
150+ } profile. Retrying after removing phone number from profile...`,
151+ e . response ?. data ,
152+ ) ;
153+
154+ const modifiedBody : any = {
155+ data : {
156+ ...body . data ,
157+ attributes : {
158+ ...( body . data as any ) . attributes ,
159+ phoneNumber : undefined ,
160+ } ,
161+ } ,
162+ } ;
163+
164+ try {
165+ return await this . processProfileOperation ( modifiedBody , create ) ;
166+ } catch ( error : any ) {
155167 logger . error (
156- `Error ${ create ? 'creating' : 'updating' } profile in Klaviyo. Response code ${ e . status } , ${ e . message } ` ,
157- e ,
168+ `Error ${
169+ create ? 'creating' : 'updating'
170+ } profile in Klaviyo after removing phone_number. Response code ${ error . status } , ${ error . message } `,
171+ error . response ?. data || error ,
158172 ) ;
159- throw e ;
173+ throw error ;
160174 }
161175 }
162176
0 commit comments