Skip to content

Commit

Permalink
Merge pull request #214 from Wingle-SMWU/fix/custom-exception-분리-#210
Browse files Browse the repository at this point in the history
[fix] CustomException http status별로 분리
  • Loading branch information
Lightieey committed Jul 16, 2023
2 parents ca2b9c8 + 0c5dce8 commit 7693368
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import lombok.Getter;

@Getter
public class CustomException extends RuntimeException {
public class BadRequestException extends RuntimeException {
private final ErrorCode code;

public CustomException(ErrorCode code) {
public BadRequestException(ErrorCode code) {
super(code.getMessage());
this.code = code;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.co.wingle.common.exception;

import kr.co.wingle.common.constants.ErrorCode;
import lombok.Getter;

@Getter
public class InternalServerErrorException extends RuntimeException {

private final ErrorCode code;

public InternalServerErrorException(ErrorCode code) {
super(code.getMessage());
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package kr.co.wingle.common.exception;

import javax.servlet.http.HttpServletRequest;

import java.io.PrintWriter;
import java.io.StringWriter;

import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.dto.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.MDC;
import org.springframework.http.HttpStatus;
Expand All @@ -18,12 +14,36 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.dto.ApiResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {

@ExceptionHandler(CustomException.class)
protected ApiResponse<Object> handleCustomException(CustomException exception, HttpServletRequest request) {
// Custom Bad Request Error
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BadRequestException.class)
protected ApiResponse<Object> handleBadRequestException(BadRequestException exception, HttpServletRequest request) {
logInfo(request, exception.getCode().getStatus(), exception.getCode().getMessage());
return ApiResponse.error(exception.getCode());
}

// Custom Unauthorized Error
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(UnauthorizedException.class)
protected ApiResponse<Object> handleUnauthorizedException(UnauthorizedException exception,
HttpServletRequest request) {
logInfo(request, exception.getCode().getStatus(), exception.getCode().getMessage());
return ApiResponse.error(exception.getCode());
}

// Custom Internal Server Error
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(InternalServerErrorException.class)
protected ApiResponse<Object> handleInternalServerErrorException(InternalServerErrorException exception,
HttpServletRequest request) {
logInfo(request, exception.getCode().getStatus(), exception.getCode().getMessage());
return ApiResponse.error(exception.getCode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.co.wingle.common.exception;

import kr.co.wingle.common.constants.ErrorCode;
import lombok.Getter;

@Getter
public class UnauthorizedException extends RuntimeException {
private final ErrorCode code;

public UnauthorizedException(ErrorCode code) {
super(code.getMessage());
this.code = code;
}
}
11 changes: 6 additions & 5 deletions wingle/src/main/java/kr/co/wingle/common/util/S3Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import com.amazonaws.util.IOUtils;

import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.exception.CustomException;
import kr.co.wingle.common.exception.BadRequestException;
import kr.co.wingle.common.exception.InternalServerErrorException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -52,7 +53,7 @@ public String idCardImageUpload(MultipartFile file) {
return upload(file, "idCardImage");
} catch (IOException e) {
log.warn(e.getMessage());
throw new CustomException(ErrorCode.FILE_UPLOAD_FAIL);
throw new InternalServerErrorException(ErrorCode.FILE_UPLOAD_FAIL);
}
}

Expand All @@ -61,7 +62,7 @@ public String profileImageUpload(MultipartFile file) {
return upload(file, "profileImage");
} catch (IOException e) {
log.warn(e.getMessage());
throw new CustomException(ErrorCode.FILE_UPLOAD_FAIL);
throw new InternalServerErrorException(ErrorCode.FILE_UPLOAD_FAIL);
}
}

Expand Down Expand Up @@ -90,15 +91,15 @@ private String getFileName(MultipartFile file) {

private String getFileExtension(String fileName) {
if (!fileName.contains(".")) {
throw new CustomException(ErrorCode.BAD_FILE_NAME);
throw new BadRequestException(ErrorCode.BAD_FILE_NAME);
}
String fileExtension = fileName.substring(fileName.lastIndexOf("."));

if (fileExtension.equalsIgnoreCase(".jpg") || fileExtension.equalsIgnoreCase(".jpeg")
|| fileExtension.equalsIgnoreCase(".png") || fileExtension.equalsIgnoreCase(".heic")) {
return fileExtension;
} else {
throw new CustomException(ErrorCode.BAD_FILE_EXTENSION);
throw new BadRequestException(ErrorCode.BAD_FILE_EXTENSION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.security.core.context.SecurityContextHolder;

import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.exception.CustomException;
import kr.co.wingle.common.exception.UnauthorizedException;

public class SecurityUtil {

Expand All @@ -15,7 +15,7 @@ public static String getCurrentUserEmail() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null || authentication.getName() == null) {
throw new CustomException(ErrorCode.UNAUTHORIZED_USER);
throw new UnauthorizedException(ErrorCode.UNAUTHORIZED_USER);
}

return authentication.getName();
Expand Down
6 changes: 3 additions & 3 deletions wingle/src/main/java/kr/co/wingle/common/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
import org.springframework.stereotype.Component;

import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.exception.CustomException;
import kr.co.wingle.common.exception.BadRequestException;

@Component
public class StringUtil {
public static Long StringToLong(String string) {
try {
return Long.parseLong(string);
} catch (Exception e) {
throw new CustomException(ErrorCode.BAD_PARAMETER_TYPE);
throw new BadRequestException(ErrorCode.BAD_PARAMETER_TYPE);
}
}

public static int StringToInt(String string) {
try {
return Integer.parseInt(string);
} catch (Exception e) {
throw new CustomException(ErrorCode.BAD_PARAMETER_TYPE);
throw new BadRequestException(ErrorCode.BAD_PARAMETER_TYPE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.web.multipart.MultipartFile;

import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.exception.CustomException;
import kr.co.wingle.common.exception.BadRequestException;
import kr.co.wingle.common.exception.DuplicateException;
import kr.co.wingle.common.exception.ForbiddenException;
import kr.co.wingle.common.exception.NotFoundException;
Expand Down Expand Up @@ -98,7 +98,7 @@ public SignupResponseDto signup(SignupRequestDto request) {
if (profileUtil.isDuplicatedNicknameByMemberId(request.getNickname(), member.getId())) {
throw new DuplicateException(ErrorCode.DUPLICATE_NICKNAME);
}

// save profile
Profile existProfile = profileRepository.findByMember(member).get();
profile = Profile.copyProfile(profile, existProfile);
Expand Down Expand Up @@ -209,7 +209,7 @@ public EmailResponseDto sendCodeMail(EmailRequestDto emailRequestDto) {
}

if (Integer.parseInt(redisUtil.getData(attemptEmailKey)) > 5) {
throw new CustomException(ErrorCode.TOO_MANY_EMAIL_REQUEST);
throw new BadRequestException(ErrorCode.TOO_MANY_EMAIL_REQUEST);
}

String certificationKey = mailService.sendEmail(to, new CodeMail());
Expand All @@ -221,9 +221,9 @@ public CertificationResponseDto checkEmailAndCode(CertificationRequestDto certif
String inputCode = certificationRequestDto.getCertificationCode();
String code = redisUtil.getData(email);
if (code == null)
throw new CustomException(ErrorCode.NO_EMAIL_CODE);
throw new BadRequestException(ErrorCode.NO_EMAIL_CODE);
if (!code.equals(inputCode))
throw new CustomException(ErrorCode.INCONSISTENT_CODE);
throw new BadRequestException(ErrorCode.INCONSISTENT_CODE);
return CertificationResponseDto.of(true);
}

Expand All @@ -232,7 +232,7 @@ public PermissionResponseDto sendAcceptanceMail(AcceptanceRequestDto acceptanceR
Long userId = acceptanceRequestDto.getUserId();
Member member = memberService.findMemberByMemberId(userId);
if (member.getPermission() == Permission.APPROVE.getStatus())
throw new CustomException(ErrorCode.ALREADY_ACCEPTANCE);
throw new BadRequestException(ErrorCode.ALREADY_ACCEPTANCE);

member.setPermission(Permission.APPROVE.getStatus());
mailService.sendEmail(member.getEmail(), new AcceptanceMail(member.getName()));
Expand All @@ -244,9 +244,9 @@ public PermissionResponseDto sendRejectionMail(RejectionRequestDto rejectionRequ
Long userId = rejectionRequestDto.getUserId();
Member member = memberService.findMemberByMemberId(userId);
if (member.getPermission() == Permission.DENY.getStatus())
throw new CustomException(ErrorCode.ALREADY_DENY);
throw new BadRequestException(ErrorCode.ALREADY_DENY);
if (member.getPermission() == Permission.APPROVE.getStatus())
throw new CustomException(ErrorCode.ALREADY_ACCEPTANCE);
throw new BadRequestException(ErrorCode.ALREADY_ACCEPTANCE);

member.setPermission(Permission.DENY.getStatus());
memberService.saveRejectionReason(rejectionRequestDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import kr.co.wingle.common.config.MailConfig;
import kr.co.wingle.common.constants.ErrorCode;
import kr.co.wingle.common.exception.CustomException;
import kr.co.wingle.common.exception.BadRequestException;
import kr.co.wingle.common.util.RedisUtil;
import kr.co.wingle.member.mailVo.CodeMail;
import kr.co.wingle.member.mailVo.Mail;
Expand Down Expand Up @@ -45,7 +45,7 @@ public String sendEmail(String to, Mail mail) {
} catch (MailException | MessagingException | UnsupportedEncodingException es) {
log.error(es.getMessage());
log.error(es.getStackTrace().toString());
throw new CustomException(ErrorCode.EMAIL_SEND_FAIL);
throw new BadRequestException(ErrorCode.EMAIL_SEND_FAIL);
}
return to;
}
Expand Down

0 comments on commit 7693368

Please sign in to comment.