diff --git a/src/main/java/com/ImageTrip/exception/GlobalExceptionAdvice.java b/src/main/java/com/ImageTrip/exception/GlobalExceptionAdvice.java index 3db68fb..9b1e670 100644 --- a/src/main/java/com/ImageTrip/exception/GlobalExceptionAdvice.java +++ b/src/main/java/com/ImageTrip/exception/GlobalExceptionAdvice.java @@ -1,2 +1,17 @@ -package com.ImageTrip.exception;public class GlobalExceptionAdvice { +package com.ImageTrip.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionAdvice { + + @ExceptionHandler + public ResponseEntity handleBusinessLogicException(BusinessLogicException e) { + String message = e.getMessage(); + + return new ResponseEntity<>(message, HttpStatus.valueOf(e.getExceptionCode().getStatus())); + } } diff --git a/src/main/java/com/ImageTrip/member/controller/MemberController.java b/src/main/java/com/ImageTrip/member/controller/MemberController.java index 9d2b2a6..9b36501 100644 --- a/src/main/java/com/ImageTrip/member/controller/MemberController.java +++ b/src/main/java/com/ImageTrip/member/controller/MemberController.java @@ -34,7 +34,7 @@ public MemberController(MemberService memberService, @PostMapping("/checkUsableEmail") public ResponseEntity checkUseableEmail(@RequestBody @Valid MemberDto.CheckEmailDto checkEmailDto){ - memberService.verifyExistsName(checkEmailDto.getEmail()); + memberService.verifyExistsEmail(checkEmailDto.getEmail()); return new ResponseEntity<>(HttpStatus.OK); } diff --git a/src/main/java/com/ImageTrip/member/service/MemberService.java b/src/main/java/com/ImageTrip/member/service/MemberService.java index 99bda98..2207a99 100644 --- a/src/main/java/com/ImageTrip/member/service/MemberService.java +++ b/src/main/java/com/ImageTrip/member/service/MemberService.java @@ -100,7 +100,7 @@ public void checkUserPassword(String token, String password) { Member member = findMemberByToken(token); if (!passwordEncoder.encode(password).equals(member.getPassword())) - new BusinessLogicException(ExceptionCode.UNMATCHED_PASSWORD); + throw new BusinessLogicException(ExceptionCode.UNMATCHED_PASSWORD); } @@ -108,13 +108,14 @@ public void checkUserPassword(String token, String password) { public void verifyExistsEmail(String email) { Optional optionalMember = memberRepository.findByEmail(email); - if(optionalMember.isPresent()) new BusinessLogicException(ExceptionCode.MEMBER_EXISTS); + System.out.println(optionalMember); + if(optionalMember.isPresent()) throw new BusinessLogicException(ExceptionCode.MEMBER_EXISTS); } public void verifyExistsName(String name) { - Optional optionalMember = memberRepository.findByEmail(name); - if(optionalMember.isPresent()) new BusinessLogicException(ExceptionCode.MEMBER_EXISTS); + Optional optionalMember = memberRepository.findByName(name); + if(optionalMember.isPresent()) throw new BusinessLogicException(ExceptionCode.MEMBER_EXISTS); }