Skip to content

Commit

Permalink
Merge pull request #20 from YAPP-Github/feature/#18
Browse files Browse the repository at this point in the history
feat: μœ μ € μ„œλΉ„μŠ€ μš”μ²­ Validation μž‘μ—…
  • Loading branch information
CChuYong authored Jul 12, 2024
2 parents 35fc52f + e1fe762 commit 4602744
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions user-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.flywaydb:flyway-core")
implementation("org.flywaydb:flyway-mysql")
implementation("org.springframework.boot:spring-boot-starter-validation")
// https://mvnrepository.com/artifact/com.mysql/mysql-connector-j
implementation("com.mysql:mysql-connector-j:8.4.0")

Expand Down
2 changes: 2 additions & 0 deletions user-service/src/main/java/kr/mafoo/user/api/AuthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import kr.mafoo.user.controller.dto.request.KakaoLoginRequest;
import kr.mafoo.user.controller.dto.request.TokenRefreshRequest;
import kr.mafoo.user.controller.dto.response.LoginResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import reactor.core.publisher.Mono;

@Tag(name = "인증(둜그인) κ΄€λ ¨ API", description = "토큰 λ°œν–‰, 둜그인 λ“± API")
@Validated
@RequestMapping("/v1/auth")
public interface AuthApi {
@Operation(summary = "카카였 둜그인", description = "카카였 인가 μ½”λ“œλ‘œ 둜그인(토큰 λ°œν–‰)ν•©λ‹ˆλ‹€.")
Expand Down
2 changes: 2 additions & 0 deletions user-service/src/main/java/kr/mafoo/user/api/MeApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import kr.mafoo.user.annotation.RequestMemberId;
import kr.mafoo.user.controller.dto.response.MemberResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import reactor.core.publisher.Mono;

@Tag(name = "둜그인 μ‚¬μš©μž 정보 API", description = "ν˜„μž¬ 토큰 주인의 정보λ₯Ό λ‹€λ£¨λŠ” API")
@Validated
@RequestMapping("/v1/me")
public interface MeApi {
@Operation(summary = "λ‚΄ 정보 쑰회", description = "ν˜„μž¬ 토큰 주인의 정보λ₯Ό μ‘°νšŒν•©λ‹ˆλ‹€.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package kr.mafoo.user.config;

import jakarta.validation.ConstraintViolationException;
import kr.mafoo.user.controller.dto.response.ErrorResponse;
import kr.mafoo.user.exception.DomainException;
import kr.mafoo.user.exception.ErrorCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.support.WebExchangeBindException;

@ControllerAdvice
public class WebExceptionHandler {
Expand All @@ -14,4 +18,28 @@ public ResponseEntity<ErrorResponse> handleDomainException(DomainException excep
.badRequest()
.body(ErrorResponse.fromErrorCode(exception.getErrorCode()));
}

@ExceptionHandler({MethodArgumentNotValidException.class,
ConstraintViolationException.class,
WebExchangeBindException.class})
public ResponseEntity<ErrorResponse> validException(Exception ex) {
String errorMessage = "μž…λ ₯κ°’ 검증 였λ₯˜: ";
if (ex instanceof MethodArgumentNotValidException mex) {
errorMessage += mex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
} else if (ex instanceof ConstraintViolationException cvex) {
errorMessage += cvex.getConstraintViolations().iterator().next().getMessage();
} else if (ex instanceof WebExchangeBindException wex) {
errorMessage += wex.getAllErrors().get(0).getDefaultMessage();
} else {
errorMessage += "μ•Œ 수 μ—†λŠ” 였λ₯˜";
}
ErrorResponse response = new ErrorResponse(
ErrorCode.REQUEST_INPUT_NOT_VALID.getCode(),
errorMessage
);

return ResponseEntity
.badRequest()
.body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum ErrorCode {
MEMBER_NOT_FOUND("ME0001", "μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€"),
KAKAO_LOGIN_FAILED("EX0001", "카카였 λ‘œκ·ΈμΈμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€"),
REQUEST_INPUT_NOT_VALID("EX0002", "μž…λ ₯ 값이 μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),

TOKEN_TYPE_MISMATCH("AU0001", "토큰 νƒ€μž…μ΄ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. (μ•„λ§ˆ AccessToken?)"),
TOKEN_EXPIRED("AU0002", "토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€"),
Expand Down

0 comments on commit 4602744

Please sign in to comment.