Skip to content

Commit

Permalink
feat: save provider error response in repository (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitij-k-osmosys authored Oct 21, 2024
1 parent 60beb46 commit fa5b44d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
18 changes: 16 additions & 2 deletions apps/api/src/jobs/consumers/notifications/notification.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,26 @@ export abstract class NotificationConsumer {
}

this.logger.debug(`Updating result of notification with id ${notification.id}`);
notification.result = { result: { message: error.message, stack: error.stack } };
notification.result = {
result: {
message: error.message,
response: error.response?.data, // 360Dialog
moreInfo: error.moreInfo, // Twilio
apiID: error.apiID, // Plivo
stack: error.stack,
},
};
this.logger.error(`Error sending notification with id: ${id}`);
this.logger.error(JSON.stringify(error, ['message', 'stack'], 2));

// Save retry attempt record
await this.saveRetryAttempt(notification, { message: error.message, stack: error.stack });
await this.saveRetryAttempt(notification, {
message: error.message,
response: error.response?.data, // 360Dialog
apiID: error.apiID, // Plivo
moreInfo: error.moreInfo, // Twilio, Plivo
stack: error.stack,
});
} finally {
this.logger.debug(
`processNotificationQueue completed. Saving notification in DB: ${JSON.stringify(notification)}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ export class SmsPlivoService {

return response;
} catch (error) {
throw new Error(`Failed to send message: ${error.message}`);
if (error.apiID) {
// Log relevant parts of the error response
this.logger.error(`Error sent from provider: ${providerId}`, error.message);
throw error;
} else {
// Handle cases where there is no response (network issues, etc.)
throw new Error(`Failed to send message: ${error.message}`);
}
}
}
async getDeliveryStatus(
Expand Down
33 changes: 25 additions & 8 deletions apps/api/src/modules/providers/wa360dialog/wa360dialog.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { Injectable, Logger } from '@nestjs/common';
import { ProvidersService } from '../providers.service';
import { lastValueFrom } from 'rxjs';

export interface Wa360DialogData {
to: string;
Expand Down Expand Up @@ -61,13 +62,29 @@ export class Wa360dialogService {
}

async sendMessage(body: Wa360DialogData, providerId: number): Promise<Wa360DialogResponse> {
await this.assignWA360Values(providerId);
const headers = {
'D360-API-KEY': this.apiKey,
'Content-Type': 'application/json',
};
this.logger.debug('Sending 360Dialog Whatsapp');
const response = await this.httpService.post(this.apiUrl, body, { headers }).toPromise();
return response.data;
try {
await this.assignWA360Values(providerId);
const headers = {
'D360-API-KEY': this.apiKey,
'Content-Type': 'application/json',
};
this.logger.debug('Sending 360Dialog Whatsapp');
const response = await lastValueFrom(this.httpService.post(this.apiUrl, body, { headers }));
return response.data;
} catch (error) {
if (error.response) {
const providerResponseError = {
status: error.response.status,
statusText: error.response.statusText,
};
// Log relevant parts of the error response
this.logger.error(`Error sent from provider: ${providerId}`, providerResponseError);

throw error;
} else {
// Handle cases where there is no response (network issues, etc.)
throw error;
}
}
}
}

0 comments on commit fa5b44d

Please sign in to comment.