global exception handler

This commit is contained in:
jinho jeong
2022-05-03 11:48:11 +09:00
parent 6ba0833794
commit ea49bfd80f
3 changed files with 12 additions and 25 deletions

View File

@@ -1,6 +1,5 @@
package com.example.oneul.domain.user.api;
import javax.servlet.http.HttpSession;
import com.example.oneul.domain.user.domain.UserEntity;
@@ -15,10 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class UserApi {
@@ -34,17 +29,20 @@ public class UserApi {
public UserEntity signUp(@RequestBody SignUpDTO signUpDTO) {
// TODO: password1, password2 같은지 검사
UserEntity user = userService.signUp(signUpDTO.toEntity());
log.info("signUp: " + user.toString());
return user;
}
@RequestMapping(value="/login/", method=RequestMethod.POST)
public UserEntity login(HttpSession httpSession, @RequestBody LoginDTO loginDTO) {
UserEntity user = userService.login(loginDTO.toEntity(), httpSession);
log.info("login: " + user.toString());
return user;
}
@RequestMapping(value="/logout/", method=RequestMethod.POST)
public String logout(HttpSession httpSession) {
log.info("logout: " + httpSession.toString());
userService.logout(httpSession);
return "logout";
}

View File

@@ -1,13 +0,0 @@
package com.example.oneul.domain.user.dao;
import java.util.Optional;
import com.example.oneul.domain.user.domain.UserEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserCommandRepository extends CrudRepository<UserEntity, Long> {
Optional<UserEntity> findByUsername(String username);
}

View File

@@ -5,6 +5,8 @@ import com.example.oneul.domain.user.exception.WrongUsernameAndPasswordException
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -13,20 +15,20 @@ public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
protected String handleException(Exception e){
protected ResponseEntity<String> handleException(Exception e){
log.info(e.getMessage());
return e.toString();
return new ResponseEntity<>("invalid request", HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(UserAlreadyExistException.class)
protected String handleUserAlreadyExistException(UserAlreadyExistException e){
protected ResponseEntity<String> handleUserAlreadyExistException(UserAlreadyExistException e){
log.info(e.getMessage());
return e.toString();
return new ResponseEntity<>("user is already exists", HttpStatus.CONFLICT);
}
@ExceptionHandler(WrongUsernameAndPasswordException.class)
protected String handleWrongUsernameAndPasswordException(WrongUsernameAndPasswordException e){
log.info(e.getMessage());
return e.toString();
protected ResponseEntity<String> handleWrongUsernameAndPasswordException(WrongUsernameAndPasswordException e){
log.info("wrong username and password");
return new ResponseEntity<>("wrong username and password", HttpStatus.NOT_FOUND);
}
}