@@ -7,7 +7,7 @@ const WS_URL = 'wss://ws.warpcast.com/stream';
77
88export const formatMessage = ( message ) => {
99 if ( message . serverTimestamp < 1740239068884 ) {
10- return false ;
10+ // return false;
1111 }
1212 const isAgent = message . senderFid === parseInt ( process . env . AGENT_FID ) ;
1313 return {
@@ -34,12 +34,14 @@ class Farcaster {
3434 } ) ;
3535
3636 if ( ! response . ok ) {
37- throw new Error ( `API call failed with status ${ response . status } : ${ await response . text ( ) } ` ) ;
37+ const error = new Error ( `API call failed with status ${ response . status } : ${ await response . text ( ) } ` ) ;
38+ error . statusCode = response . status ;
39+ throw error ;
3840 }
3941
4042 return await response . json ( ) ;
4143 } catch ( error ) {
42- console . error ( `Error making API call to ${ endpoint } :` , error ) ;
44+ // console.error(`Error making API call to ${endpoint}:`, error);
4345 throw error ;
4446 }
4547 }
@@ -159,7 +161,24 @@ class Farcaster {
159161 if ( ! conversationId ) {
160162 conversationId = [ p1 , p2 ] . sort ( ( a , b ) => a - b ) . join ( '-' ) ;
161163 }
162- const messages = await this . fetchMessages ( conversationId ) ;
164+ let messages ;
165+ try {
166+ messages = await this . fetchMessages ( conversationId ) ;
167+ } catch ( error ) {
168+ if ( error . statusCode && error . statusCode === 403 ) {
169+ console . log ( '403 error encountered, reverting conversationId and trying again' ) ;
170+ conversationId = [ p2 , p1 ] . sort ( ( a , b ) => b - a ) . join ( '-' ) ;
171+ try {
172+ messages = await this . fetchMessages ( conversationId ) ;
173+ } catch ( retryError ) {
174+ console . error ( 'Retry failed:' , retryError ) ;
175+ throw retryError ;
176+ }
177+ } else {
178+ console . error ( 'Failed to fetch messages:' , error ) ;
179+ throw error ;
180+ }
181+ }
163182 return messages . map ( message => formatMessage ( message ) ) . filter ( Boolean ) ;
164183 }
165184
0 commit comments