Skip to content

Commit

Permalink
refactor : Messaging Exception 상황에 따라 다른 CustomException 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Feb 12, 2024
1 parent ce7e858 commit bae67e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gdschongik.gdsc.domain.integration;

import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.exception.ErrorCode;
import jakarta.mail.Message.RecipientType;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
Expand All @@ -20,30 +22,45 @@ public class JavaMailSender implements MailSender {

@Override
public void send(String recipient, String content) {
MimeMessage message = writeMimeMessage(recipient, content);
javaMailSender.send(message);
}

private MimeMessage writeMimeMessage(String recipient, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
addRecipients(message, recipient);
setMessageAttributes(message, content);
return message;
}

private void addRecipients(MimeMessage message, String recipient) {
try {
MimeMessage message = writeMimeMessage(recipient, content);
javaMailSender.send(message);
message.addRecipients(RecipientType.TO, recipient);
} catch (MessagingException e) {
throw new RuntimeException(e);
throwCustomExceptionWithAdditionalMessage(ErrorCode.MESSAGING_EXCEPTION, e.getMessage());
}
}

private MimeMessage writeMimeMessage(String recipient, String content)
throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();

message.addRecipients(RecipientType.TO, recipient);
message.setSubject(MESSAGE_SUBJECT);
message.setText(content, "utf-8", "html");
message.setFrom(getInternetAddress());
return message;
private void setMessageAttributes(MimeMessage message, String content) {
try {
message.setSubject(MESSAGE_SUBJECT);
message.setText(content, "utf-8", "html");
message.setFrom(getInternetAddress());
} catch (MessagingException e) {
throwCustomExceptionWithAdditionalMessage(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
}
}

private InternetAddress getInternetAddress() {
private InternetAddress getInternetAddress() throws MessagingException {
try {
return new InternetAddress(SENDER_ADDRESS, SENDER_PERSONAL);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
return new InternetAddress();
throw new MessagingException("UnsupportedEncodingException");
}
}

private void throwCustomExceptionWithAdditionalMessage(ErrorCode errorCode, String additionalMessage) {
String errorMessage = errorCode.getMessage() + " : " + additionalMessage;
throw new CustomException(errorCode, errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public enum ErrorCode {

VERIFICATION_CODE_NOT_FOUND(HttpStatus.NOT_FOUND, "Univ Email 인증 코드가 존재하지 않습니다."),
UNIV_EMAIL_VERIFICATION_CODE_NOT_MATCH(HttpStatus.BAD_REQUEST, "Email 인증 번호가 불일치합니다."),
MESSAGING_EXCEPTION(HttpStatus.BAD_REQUEST, "수신자 이메일이 올바르지 않습니다."),

;

private final HttpStatus status;
Expand Down

0 comments on commit bae67e8

Please sign in to comment.