@@ -3,6 +3,7 @@ import { getJob } from '../../services/storage/jobStorage.js';
3
3
import fetch from 'node-fetch' ;
4
4
import pThrottle from 'p-throttle' ;
5
5
import { normalizeImageUrl } from '../../utils.js' ;
6
+ import logger from '../../services/logger.js' ;
6
7
7
8
const RATE_LIMIT_INTERVAL = 1000 ;
8
9
const chatThrottleMap = new Map ( ) ;
@@ -68,6 +69,11 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
68
69
body : JSON . stringify ( body ) ,
69
70
headers : { 'Content-Type' : 'application/json' } ,
70
71
} ) ;
72
+
73
+ if ( ! res . ok ) {
74
+ const errorBody = await res . text ( ) ;
75
+ throw new Error ( `API error for '${ jobName } '. '${ endpoint } ' returned ${ errorBody } ` ) ;
76
+ }
71
77
return res ;
72
78
} ) ;
73
79
@@ -81,23 +87,23 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
81
87
} ;
82
88
83
89
if ( ! img ) {
84
- return throttledCall ( 'sendMessage' , textPayload ) ;
90
+ return await throttledCall ( 'sendMessage' , textPayload ) . catch ( async ( e ) => {
91
+ logger . error ( `Error sending message to Telegram: ${ e . message } ` ) ;
92
+ } ) ;
85
93
}
86
94
87
- try {
88
- return await throttledCall ( 'sendPhoto' , {
89
- chat_id : chatId ,
90
- photo : img ,
91
- caption : buildCaption ( jobName , serviceName , o ) ,
92
- parse_mode : 'HTML' ,
95
+ return await throttledCall ( 'sendPhoto' , {
96
+ chat_id : chatId ,
97
+ photo : img ,
98
+ caption : buildCaption ( jobName , serviceName , o ) ,
99
+ parse_mode : 'HTML' ,
100
+ } ) . catch ( async ( e ) => {
101
+ logger . error ( `Error sending photo to Telegram and use a fallback: ${ e . message } ` ) ;
102
+ return await throttledCall ( 'sendMessage' , textPayload ) . catch ( ( e ) => {
103
+ logger . error ( `Error sending message to Telegram: ${ e . message } ` ) ;
104
+ throw e ;
93
105
} ) ;
94
- } catch ( e ) {
95
- // If we see a timeout due to sending an image, try sending it without
96
- if ( e && ( e . code === 'ETIMEDOUT' || e . errno === 'ETIMEDOUT' ) ) {
97
- return throttledCall ( 'sendMessage' , textPayload ) ;
98
- }
99
- throw e ;
100
- }
106
+ } ) ;
101
107
} ) ;
102
108
103
109
return Promise . all ( promises ) ;
0 commit comments