Skip to content

Commit

Permalink
feat(BE): Custom exception 반영 BUS-203-Authority-check #156
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemonade255 committed Oct 25, 2023
1 parent bdacb9b commit f829608
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ResponseEntity<?> exceptionHandler(CustomException e) {
return ResponseEntity.status(e.getErrorCodeEnum().getHttpStatus())
.body(new ExceptionDto(e.getErrorCodeEnum()));
}

@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
Expand All @@ -32,7 +32,7 @@ public ResponseEntity<?> constraintViolationException(ConstraintViolationExcepti
log.error(e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}

@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String runtimeExceptionHandler(RuntimeException e) {
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/example/api/common/type/ErrorCodeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
@Getter
@AllArgsConstructor
public enum ErrorCodeEnum {
LOGIN_IS_NOT_DONE(HttpStatus.UNAUTHORIZED, "로그인 정보가 없습니다"),
INVALID_PERMISSION(HttpStatus.UNAUTHORIZED, "권한이 없습니다"),
DATABASE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "데이터베이스 오류"),
CODE_IS_NOT_VALID(HttpStatus.BAD_REQUEST, "잘못된 인증번호입니다"),
// 200 OK
SUCCESS(HttpStatus.OK, "정상 처리되었습니다"),
// 201 Created
CREATED(HttpStatus.CREATED, "생성되었습니다"),
// 400 Bad Request
USER_NOT_FOUND(HttpStatus.BAD_REQUEST, "유저 정보가 없습니다"),
CODE_IS_EXPIRED(HttpStatus.BAD_REQUEST, "휴대전화를 인증해주세요");
CODE_IS_EXPIRED(HttpStatus.BAD_REQUEST, "휴대전화를 인증해주세요"),
CODE_IS_NOT_VALID(HttpStatus.BAD_REQUEST, "잘못된 인증번호입니다"),
MATCHING_NOT_FOUND(HttpStatus.BAD_REQUEST, "매칭 정보가 없습니다"),
// 401 Unauthorized
LOGIN_IS_NOT_DONE(HttpStatus.UNAUTHORIZED, "로그인 정보가 없습니다"),
// 403 Forbidden
INVALID_PERMISSION(HttpStatus.FORBIDDEN, "권한이 없습니다"),
// 500 Internal Server Error
DATABASE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "데이터베이스 오류");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.example.api.auth.domain.SecurityUser;
import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.common.exception.CustomException;
import com.example.api.common.type.ApplicationStateEnum;
import com.example.api.common.type.ErrorCodeEnum;
import com.example.api.common.utils.AuthenticationUtils;
import com.example.api.matching.adapter.out.persistence.MatchingApplicationPK;
import com.example.api.matching.application.port.in.*;
Expand Down Expand Up @@ -57,17 +59,17 @@ public ChatRoom createMatchingApplication(@RequestBody SaveMatchingApplicationDt
Optional<FindMatchingDto> matchingDto = findMatchingUsecase.getMatchingById(matchingApplicationDto.getMatchingId());
if (matchingDto.isEmpty()) {
log.error("MatchingController::createMatchingApplication: No such matching.");
return ChatRoom.builder().build();
throw new CustomException(ErrorCodeEnum.MATCHING_NOT_FOUND);
}

SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication();
if (securityUser == null) {
log.error("MatchingController::createMatchingApplication: Authentication is needed.");
return ChatRoom.builder().build();
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}
if (securityUser.getUserId().equals(matchingApplicationDto.getUserId())) {
log.error("MatchingController::createMatchingApplication: WriterId equals to applicantId.");
return ChatRoom.builder().build();
throw new CustomException(ErrorCodeEnum.INVALID_PERMISSION);
}

MatchingApplication matchingApplication = matchingApplicationUsecase.createMatchingApplicationData(matchingApplicationDto);
Expand Down Expand Up @@ -186,7 +188,7 @@ public void processMatchingApplication(SaveMatchingApplicationDto matchingApplic
public void deleteAll() {
if (!(findUserUsecase.getUser().getRole().equals(UserRoleEnum.Admin))) {
log.error("MatchingController::deleteAll: Admin authority is needed.");
return;
throw new CustomException(ErrorCodeEnum.INVALID_PERMISSION);
}
deleteMatchingUsecase.deleteAll();
}
Expand All @@ -202,10 +204,11 @@ public void deleteMatching(@PathVariable Long matchingId) {
Optional<FindMatchingDto> matchingDto = findMatchingUsecase.getMatchingById(matchingId);
if (matchingDto.isEmpty()) {
log.error("MatchingController::deleteMatching: No such matching.");
return;
throw new CustomException(ErrorCodeEnum.MATCHING_NOT_FOUND);
}
if (!(userDto.getRole().equals(UserRoleEnum.Admin)) && !(userDto.getUserId().equals(matchingDto.get().getWriterId()))) {
log.error("MatchingController::deleteMatching: Admin or owner authority is needed.");
throw new CustomException(ErrorCodeEnum.INVALID_PERMISSION);
}
deleteMatchingUsecase.deleteMatching(matchingId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.api.user.adapter.in.rest;

import com.example.api.common.exception.CustomException;
import com.example.api.common.type.ApplicationStateEnum;
import com.example.api.common.type.ErrorCodeEnum;
import com.example.api.matching.application.port.in.FindMatchingUsecase;
import com.example.api.matching.application.port.in.MatchingApplicationUsecase;
import com.example.api.matching.dto.FindMatchingDto;
Expand Down Expand Up @@ -131,6 +133,7 @@ public FindUserDto updateUser(@RequestBody UpdateUserDto userDto) {
public void deleteAll() {
if (!(findUserUsecase.getUser().getRole().equals(UserRoleEnum.Admin))) {
log.error("UserController::deleteAll: Admin authority is needed.");
throw new CustomException(ErrorCodeEnum.INVALID_PERMISSION);
}
deleteUserUsecase.deleteAll();
}
Expand All @@ -143,6 +146,7 @@ public void deleteAll() {
public void deleteUser() {
if (!(findUserUsecase.getUser().getRole().equals(UserRoleEnum.Admin))) {
log.error("UserController::deleteUser: Admin authority is needed.");
throw new CustomException(ErrorCodeEnum.INVALID_PERMISSION);
}
deleteUserUsecase.deleteUser();
}
Expand Down

0 comments on commit f829608

Please sign in to comment.