Skip to content

Commit

Permalink
fix: email-service - added try-catch block to sendEmail function
Browse files Browse the repository at this point in the history
  • Loading branch information
BEdev24 authored Jul 10, 2024
1 parent bb97bfc commit 7ec16bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/src/auth/strategy/magiclogin.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class MagicLoginStrategy extends PassportStrategy(
callbackUrl: configService.getOrThrow('FE_LOGIN_CALLBACK_URL'),
sendMagicLink: async (destination: string, href: string) => {
const emailDto: EmailDto = EmailMapper.loginEmail(destination, href);
this.authFacade.sendEmail(emailDto);
await this.authFacade.sendEmail(emailDto);
this.logger.log(`sending email to ${destination}, with link ${href}`);
},
verify: async (payload, callback) =>
Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth/strategy/magicregister.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MagicRegisterStrategy extends PassportStrategy(
destination,
href,
);
this.authFacade.sendEmail(emailDto);
await this.authFacade.sendEmail(emailDto);
this.logger.log(`sending email to ${destination}, with link ${href}`);
},
verify: async (payload, callback) =>
Expand Down
16 changes: 14 additions & 2 deletions backend/src/email/service/email.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { MailerService } from '@nestjs-modules/mailer';
import { Injectable } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import { EmailDto } from '../dto/email.dto';

@Injectable()
export class EmailService {
private logger = new Logger(EmailService.name);
constructor(private readonly mailerService: MailerService) {}
async sendEmail(emailDto: EmailDto): Promise<void> {
await this.mailerService.sendMail(emailDto);
try {
await this.mailerService.sendMail(emailDto);
} catch (e) {
this.logger.error(`There has been an error when sending email: ${e}`);
throw new InternalServerErrorException(
`There has been an error when sending email: ${e}`,
);
}
}
}

0 comments on commit 7ec16bd

Please sign in to comment.