Skip to content

Commit

Permalink
Merge branch 'dev' into feat/#68-daily-check-and-send-arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Minjae-An committed May 10, 2024
2 parents 7719b79 + 68154a2 commit 4ab3239
Show file tree
Hide file tree
Showing 30 changed files with 513 additions and 399 deletions.
2 changes: 0 additions & 2 deletions be/src/main/java/yeonba/be/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public void addInterceptors(InterceptorRegistry registry) {
"/error")
.excludePathPatterns(
"/users/join/**",
"/users/email-inquiry/**",
"/users/pw-inquiry",
"/users/login",
"/users/refresh");

Expand Down
194 changes: 78 additions & 116 deletions be/src/main/java/yeonba/be/exception/ExceptionAdvice.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package yeonba.be.exception;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -21,118 +19,82 @@
@RestControllerAdvice(annotations = {RestController.class})
public class ExceptionAdvice extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = GeneralException.class)
public ResponseEntity<Object> handleGeneralException(
GeneralException exception,
HttpServletRequest request) {

return handleExceptionInternal(
exception,
request);
}

@ExceptionHandler(value = ConstraintViolationException.class)
public ResponseEntity<Object> handleConstraintViolationException(
ConstraintViolationException exception,
WebRequest request) {

String exceptionMessage = getConstraintViolationMessage(exception);

return handleExceptionInternalConstraint(
exception,
exceptionMessage,
request);
}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException exception,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {

Map<String, String> exceptionArgs = getMethodArgumentExceptionArgs(exception);

return handleExceptionInternalArgs(
exception,
exceptionArgs,
request);
}

private ResponseEntity<Object> handleExceptionInternal(
GeneralException exception,
HttpServletRequest request) {

CustomResponse<Object> body = new CustomResponse<>(exception.getExceptionReason());
WebRequest webRequest = new ServletWebRequest(request);

return super.handleExceptionInternal(
exception,
body,
HttpHeaders.EMPTY,
exception.getHttpStatus(),
webRequest);
}

private Map<String, String> getMethodArgumentExceptionArgs(
MethodArgumentNotValidException exception) {

Map<String, String> exceptionArgs = new LinkedHashMap<>();
exception.getBindingResult().getFieldErrors()
.forEach(fieldError -> {
String fieldName = fieldError.getField();
String errorMessage = Optional
.ofNullable(fieldError.getDefaultMessage())
.orElse("");
exceptionArgs.merge(
fieldName,
errorMessage,
(existingErrorMessage, newErrorMessage) ->
existingErrorMessage
.concat(", ")
.concat(newErrorMessage));
});

return exceptionArgs;
}

private ResponseEntity<Object> handleExceptionInternalArgs(
MethodArgumentNotValidException exception,
Map<String, String> exceptionArgs,
WebRequest request) {

CustomResponse<Map<String, String>> body = CustomResponse.onFailure(
CommonException.BAD_REQUEST.getReason(),
exceptionArgs);

return super.handleExceptionInternal(
exception,
body,
HttpHeaders.EMPTY,
CommonException.BAD_REQUEST.getHttpStatus(),
request);
}

private String getConstraintViolationMessage(ConstraintViolationException exception) {

return exception.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.findFirst()
.orElseThrow(() -> new RuntimeException("ConstraintViolationException 추출 도중 오류 발생"));
}

private ResponseEntity<Object> handleExceptionInternalConstraint(
ConstraintViolationException exception,
String exceptionMessage,
WebRequest request) {

CustomResponse<Object> body = new CustomResponse<>(exceptionMessage);

return super.handleExceptionInternal(
exception,
body,
HttpHeaders.EMPTY,
CommonException.BAD_REQUEST.getHttpStatus(),
request);
}
@ExceptionHandler(value = GeneralException.class)
public ResponseEntity<Object> handleGeneralException(
GeneralException exception,
HttpServletRequest request) {

return handleExceptionInternal(
exception,
request);
}

private ResponseEntity<Object> handleExceptionInternal(
GeneralException exception,
HttpServletRequest request) {

CustomResponse<Object> body = new CustomResponse<>(exception.getExceptionReason());
WebRequest webRequest = new ServletWebRequest(request);

return super.handleExceptionInternal(
exception,
body,
HttpHeaders.EMPTY,
exception.getHttpStatus(),
webRequest);
}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException exception,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {

Map<String, String> exceptionArgs = getMethodArgumentExceptionArgs(exception);

return handleExceptionInternalArgs(
exception,
exceptionArgs,
request);
}

private Map<String, String> getMethodArgumentExceptionArgs(
MethodArgumentNotValidException exception) {

Map<String, String> exceptionArgs = new LinkedHashMap<>();
exception.getBindingResult().getFieldErrors()
.forEach(fieldError -> {
String fieldName = fieldError.getField();
String errorMessage = Optional
.ofNullable(fieldError.getDefaultMessage())
.orElse("");
exceptionArgs.merge(
fieldName,
errorMessage,
(existingErrorMessage, newErrorMessage) ->
existingErrorMessage
.concat(", ")
.concat(newErrorMessage));
});

return exceptionArgs;
}

private ResponseEntity<Object> handleExceptionInternalArgs(
MethodArgumentNotValidException exception,
Map<String, String> exceptionArgs,
WebRequest request) {

CustomResponse<Map<String, String>> body = CustomResponse.onFailure(
CommonException.BAD_REQUEST.getReason(),
exceptionArgs);

return super.handleExceptionInternal(
exception,
body,
HttpHeaders.EMPTY,
CommonException.BAD_REQUEST.getHttpStatus(),
request);
}
}
16 changes: 0 additions & 16 deletions be/src/main/java/yeonba/be/exception/JoinException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ public enum JoinException implements BaseException {
HttpStatus.BAD_REQUEST,
"비밀번호 확인 값이 비밀번호와 일치하지 않습니다."),

VOCAL_RANGE_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"존재하지 않는 음역대입니다."),

ANIMAL_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"존재하지 않는 동물상입니다."),

AREA_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"존재하지 않는 지역입니다."),

ALREADY_USED_EMAIL(
HttpStatus.BAD_REQUEST,
"이미 사용 중인 이메일입니다."),

ALREADY_USED_NICKNAME(
HttpStatus.BAD_REQUEST,
"이미 사용 중인 닉네임입니다."),
Expand Down
26 changes: 25 additions & 1 deletion be/src/main/java/yeonba/be/exception/UserException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public enum UserException implements BaseException {
HttpStatus.BAD_REQUEST,
"다른 로그인 방식을 이용해주세요."),

VOCAL_RANGE_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"존재하지 않는 음역대입니다."),

ANIMAL_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"존재하지 않는 동물상입니다."),

AREA_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"존재하지 않는 지역입니다."),

USER_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"해당 사용자가 존재하지 않습니다."),
Expand All @@ -30,7 +42,19 @@ public enum UserException implements BaseException {

INACTIVE_USER(
HttpStatus.BAD_REQUEST,
"휴면 상태 사용자입니다. 휴면 해제가 필요합니다.");
"휴면 상태 사용자입니다. 휴면 해제가 필요합니다."),

USER_PREFERENCE_NOT_FOUND(
HttpStatus.BAD_REQUEST,
"해당 사용자의 선호내역이 존재하지 않습니다."),

AGE_OUT_OF_RANGE(
HttpStatus.BAD_REQUEST,
"서비스 이용이 가능한 사용자 나이는 20~40세입니다."),

LOWER_BOUND_LESS_THAN_OR_EQUAL_UPPER_BOUND(
HttpStatus.BAD_REQUEST,
"하한 값은 상한 값보다 작거나 같아야 합니다.");

private final HttpStatus httpStatus;
private final String reason;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import yeonba.be.login.dto.request.UserEmailInquiryRequest;
import yeonba.be.login.dto.request.UserJoinRequest;
import yeonba.be.login.dto.request.UserLoginRequest;
import yeonba.be.login.dto.request.UserPasswordInquiryRequest;
import yeonba.be.login.dto.request.UserRefreshJwtRequest;
import yeonba.be.login.dto.request.UserVerificationCodeRequest;
import yeonba.be.login.dto.request.UserVerifyPhoneNumberRequest;
import yeonba.be.login.dto.response.UserEmailInquiryResponse;
import yeonba.be.login.dto.response.UserJoinResponse;
import yeonba.be.login.dto.response.UserLoginResponse;
import yeonba.be.login.dto.response.UserRefrehJwtResponse;
Expand Down

This file was deleted.

Loading

0 comments on commit 4ab3239

Please sign in to comment.