Skip to content

Commit

Permalink
Merge pull request novuhq#1290 from novuhq/fix-missing-error-message
Browse files Browse the repository at this point in the history
fix: missing error message due object object print
  • Loading branch information
scopsy authored Sep 28, 2022
2 parents 220ddc8 + 6da9548 commit 0042345
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,15 @@ export class SendMessageChat extends SendMessageType {
content,
});
} catch (e) {
await this.createLogUsecase.execute(
CreateLogCommand.create({
transactionId: command.transactionId,
status: LogStatusEnum.ERROR,
environmentId: command.environmentId,
organizationId: command.organizationId,
text: e.message || e.name || 'Un-expect CHAT provider error',
userId: command.userId,
code: LogCodeEnum.CHAT_ERROR,
templateId: notification._templateId,
raw: {
payload: command.payload,
triggerIdentifier: command.identifier,
},
})
);

await this.messageRepository.updateMessageStatus(
message._id,
await this.sendErrorStatus(
message,
'error',
e,
'unexpected_chat_error',
e.message || e.name || 'Un-expect CHAT provider error'
e.message || e.name || 'Un-expect CHAT provider error',
command,
notification,
LogCodeEnum.CHAT_ERROR,
e
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import {
OrganizationEntity,
IntegrationEntity,
} from '@novu/dal';
import { ChannelTypeEnum, LogCodeEnum, LogStatusEnum } from '@novu/shared';
import { ChannelTypeEnum, LogCodeEnum } from '@novu/shared';
import * as Sentry from '@sentry/node';
import { IAttachmentOptions, IEmailOptions } from '@novu/stateless';
import { CreateLog } from '../../../logs/usecases/create-log/create-log.usecase';
import { CreateLogCommand } from '../../../logs/usecases/create-log/create-log.command';
import { CompileTemplate } from '../../../content-templates/usecases/compile-template/compile-template.usecase';
import { CompileTemplateCommand } from '../../../content-templates/usecases/compile-template/compile-template.command';
import { MailFactory } from '../../services/mail-service/mail.factory';
Expand Down Expand Up @@ -208,36 +207,15 @@ export class SendMessageEmail extends SendMessageType {
try {
await mailHandler.send(mailData);
} catch (error) {
console.error(error);
Sentry.captureException(error?.response?.body || error?.response || error?.errors || error);
const text =
String(error?.response?.body || error?.response || error) || 'Error while sending email with provider';
this.messageRepository.updateMessageStatus(
message._id,
await this.sendErrorStatus(
message,
'error',
String(error?.response?.body || error?.response || error),
'mail_unexpected_error',
text
);
await this.createLogUsecase.execute(
CreateLogCommand.create({
transactionId: command.transactionId,
status: LogStatusEnum.ERROR,
environmentId: command.environmentId,
organizationId: command.organizationId,
notificationId: notification._id,
messageId: message._id,
text,
userId: command.userId,
subscriberId: command.subscriberId,
code: LogCodeEnum.MAIL_PROVIDER_DELIVERY_ERROR,
templateId: notification._templateId,
raw: {
error: String(error?.response?.body || error?.response || error),
payload: command.payload,
triggerIdentifier: command.identifier,
},
})
'Error while sending email with provider',
command,
notification,
LogCodeEnum.MAIL_PROVIDER_DELIVERY_ERROR,
error
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,29 +183,15 @@ export class SendMessagePush extends SendMessageType {
overrides,
});
} catch (e) {
await this.createLogUsecase.execute(
CreateLogCommand.create({
transactionId: command.transactionId,
status: LogStatusEnum.ERROR,
environmentId: command.environmentId,
organizationId: command.organizationId,
text: e.message || e.name || 'Un-expect Push provider error',
userId: command.userId,
code: LogCodeEnum.PUSH_ERROR,
templateId: notification._templateId,
raw: {
payload: command.payload,
triggerIdentifier: command.identifier,
},
})
);

await this.messageRepository.updateMessageStatus(
message._id,
await this.sendErrorStatus(
message,
'error',
e,
'unexpected_push_error',
e.message || e.name || 'Un-expect Push provider error'
e.message || e.name || 'Un-expect Push provider error',
command,
notification,
LogCodeEnum.PUSH_ERROR,
e
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,15 @@ export class SendMessageSms extends SendMessageType {
attachments: null,
});
} catch (e) {
await this.createLogUsecase.execute(
CreateLogCommand.create({
transactionId: command.transactionId,
status: LogStatusEnum.ERROR,
environmentId: command.environmentId,
organizationId: command.organizationId,
text: e.message || e.name || 'Un-expect SMS provider error',
userId: command.userId,
code: LogCodeEnum.SMS_ERROR,
templateId: notification._templateId,
raw: {
payload: command.payload,
triggerIdentifier: command.identifier,
},
})
);

await this.messageRepository.updateMessageStatus(
message._id,
await this.sendErrorStatus(
message,
'error',
e,
'unexpected_sms_error',
e.message || e.name || 'Un-expect SMS provider error'
e.message || e.name || 'Un-expect SMS provider error',
command,
notification,
LogCodeEnum.SMS_ERROR,
e
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LogCodeEnum, LogStatusEnum } from '@novu/shared';
import { CreateLog } from '../../../logs/usecases/create-log/create-log.usecase';
import { CreateLogCommand } from '../../../logs/usecases/create-log/create-log.command';
import { SendMessageCommand } from './send-message.command';
import * as Sentry from '@sentry/node';

export abstract class SendMessageType {
protected constructor(protected messageRepository: MessageRepository, protected createLogUsecase: CreateLog) {}
Expand All @@ -13,12 +14,23 @@ export abstract class SendMessageType {
message,
status: 'error' | 'sent' | 'warning',
errorId: string,
errorMessage: string,
errorMessageFallback: string,
command: SendMessageCommand,
notification: NotificationEntity,
logCodeEnum: LogCodeEnum
logCodeEnum: LogCodeEnum,
error?: any
) {
await this.messageRepository.updateMessageStatus(message._id, status, null, errorId, errorMessage);
const errorString =
stringifyObject(error?.response?.body) ||
stringifyObject(error?.response) ||
stringifyObject(error) ||
errorMessageFallback;

if (error) {
Sentry.captureException(errorString);
}

await this.messageRepository.updateMessageStatus(message._id, status, null, errorId, errorString);

await this.createLogUsecase.execute(
CreateLogCommand.create({
Expand All @@ -27,7 +39,7 @@ export abstract class SendMessageType {
environmentId: command.environmentId,
organizationId: command.organizationId,
notificationId: notification._id,
text: errorMessage,
text: errorString,
userId: command.userId,
subscriberId: command.subscriberId,
code: logCodeEnum,
Expand All @@ -36,3 +48,19 @@ export abstract class SendMessageType {
);
}
}

function stringifyObject(error: any): string {
if (!error) return;

if (typeof error === 'string') {
return error;
}

if (error instanceof String) {
return error.toString();
}

if (Object.keys(error)?.length > 0) {
return JSON.stringify(error);
}
}

0 comments on commit 0042345

Please sign in to comment.