Skip to content

Commit

Permalink
Merge pull request #44 from Tico-Corp/feature-be/TICO-230-enhance-exc…
Browse files Browse the repository at this point in the history
…eption-handling

[FEAT] 글로벌 예외 처리 기능 추가 및 개선 (TICO-230)
  • Loading branch information
bu119 authored Jul 22, 2024
2 parents 12a9cdc + 5027d25 commit 8d20813
Show file tree
Hide file tree
Showing 33 changed files with 654 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


import com.tico.pomoro_do.domain.user.entity.User;
import com.tico.pomoro_do.global.common.enums.CategoryType;
import com.tico.pomoro_do.global.common.enums.CategoryVisibility;
import com.tico.pomoro_do.global.enums.CategoryType;
import com.tico.pomoro_do.global.enums.CategoryVisibility;
import jakarta.persistence.*;

import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.tico.pomoro_do.domain.category.entity;

import com.tico.pomoro_do.domain.user.entity.User;
import com.tico.pomoro_do.global.common.enums.CategoryVisibility;
import com.tico.pomoro_do.global.common.enums.GroupInviteStatus;
import com.tico.pomoro_do.global.common.enums.GroupRole;
import com.tico.pomoro_do.global.enums.GroupInviteStatus;
import com.tico.pomoro_do.global.enums.GroupRole;
import jakarta.persistence.*;

import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import com.tico.pomoro_do.domain.user.entity.User;
import com.tico.pomoro_do.global.common.enums.TimerType;
import com.tico.pomoro_do.global.enums.TimerType;
import jakarta.persistence.*;

import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tico.pomoro_do.domain.timer.entity;

import com.tico.pomoro_do.domain.todo.entity.Todo;
import com.tico.pomoro_do.global.common.enums.TodoStatus;
import com.tico.pomoro_do.global.enums.TodoStatus;
import jakarta.persistence.*;

import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.tico.pomoro_do.domain.todo.entity;

import com.tico.pomoro_do.domain.category.entity.Category;
import com.tico.pomoro_do.global.common.enums.TodoStatus;
import com.tico.pomoro_do.global.enums.TodoStatus;
import jakarta.persistence.*;

import java.time.LocalDateTime;
import java.util.Date;

@Entity
@Table(name = "todo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.tico.pomoro_do.domain.user.dto.request.AdminLoginDTO;
import com.tico.pomoro_do.domain.user.dto.response.JwtDTO;
import com.tico.pomoro_do.domain.user.service.AdminService;
import com.tico.pomoro_do.global.base.CustomSuccessCode;
import com.tico.pomoro_do.global.base.SuccessResponseDTO;
import com.tico.pomoro_do.global.code.SuccessCode;
import com.tico.pomoro_do.global.response.SuccessResponseDTO;
import com.tico.pomoro_do.global.exception.ErrorResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -19,9 +19,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.GeneralSecurityException;

@Tag(name = "admin: 관리자", description = "백엔드를 테스트를 위한 API")
@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -63,8 +60,8 @@ public ResponseEntity<SuccessResponseDTO<JwtDTO>> adminJoin(@RequestBody AdminJo
log.info("관리자 회원가입 요청: {}", request.getUsername());
JwtDTO jwtResponse = adminService.adminJoin(request);
SuccessResponseDTO<JwtDTO> response = SuccessResponseDTO.<JwtDTO>builder()
.status(CustomSuccessCode.ADMIN_SIGNUP_SUCCESS.getHttpStatus().value())
.message(CustomSuccessCode.ADMIN_SIGNUP_SUCCESS.getMessage())
.status(SuccessCode.ADMIN_SIGNUP_SUCCESS.getHttpStatus().value())
.message(SuccessCode.ADMIN_SIGNUP_SUCCESS.getMessage())
.data(jwtResponse)
.build();
log.info("관리자 회원가입 성공: {}", request.getUsername());
Expand Down Expand Up @@ -101,8 +98,8 @@ public ResponseEntity<SuccessResponseDTO<JwtDTO>> adminLogin(@RequestBody AdminL
log.info("관리자 로그인 요청: {}", request.getUsername());
JwtDTO jwtResponse = adminService.adminLogin(request);
SuccessResponseDTO<JwtDTO> response = SuccessResponseDTO.<JwtDTO>builder()
.status(CustomSuccessCode.ADMIN_LOGIN_SUCCESS.getHttpStatus().value())
.message(CustomSuccessCode.ADMIN_LOGIN_SUCCESS.getMessage())
.status(SuccessCode.ADMIN_LOGIN_SUCCESS.getHttpStatus().value())
.message(SuccessCode.ADMIN_LOGIN_SUCCESS.getMessage())
.data(jwtResponse)
.build();
log.info("관리자 로그인 성공: {}", request.getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.tico.pomoro_do.domain.user.dto.request.GoogleJoinDTO;
import com.tico.pomoro_do.domain.user.dto.response.JwtDTO;
import com.tico.pomoro_do.domain.user.service.AuthService;
import com.tico.pomoro_do.global.base.CustomSuccessCode;
import com.tico.pomoro_do.global.base.SuccessResponseDTO;
import com.tico.pomoro_do.global.exception.CustomErrorCode;
import com.tico.pomoro_do.global.code.SuccessCode;
import com.tico.pomoro_do.global.response.SuccessResponseDTO;
import com.tico.pomoro_do.global.code.ErrorCode;
import com.tico.pomoro_do.global.exception.CustomException;
import com.tico.pomoro_do.global.exception.ErrorResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -72,14 +72,14 @@ public ResponseEntity<SuccessResponseDTO<JwtDTO>> googleLogin(@RequestHeader("Go
try {
JwtDTO jwtResponse = authService.googleLogin(googleIdTokenHeader);
SuccessResponseDTO<JwtDTO> response = SuccessResponseDTO.<JwtDTO>builder()
.status(CustomSuccessCode.GOOGLE_LOGIN_SUCCESS.getHttpStatus().value())
.message(CustomSuccessCode.GOOGLE_LOGIN_SUCCESS.getMessage())
.status(SuccessCode.GOOGLE_LOGIN_SUCCESS.getHttpStatus().value())
.message(SuccessCode.GOOGLE_LOGIN_SUCCESS.getMessage())
.data(jwtResponse)
.build();
return ResponseEntity.ok(response);
} catch (GeneralSecurityException | IOException | IllegalArgumentException e) {
log.error("Google ID Token verification failed: {}", e.getMessage(), e);
throw new CustomException(CustomErrorCode.GOOGLE_TOKEN_VERIFICATION_FAILED);
throw new CustomException(ErrorCode.GOOGLE_TOKEN_VERIFICATION_FAILED);
}
}

Expand Down Expand Up @@ -118,14 +118,14 @@ public ResponseEntity<SuccessResponseDTO<JwtDTO>> googleJoin(@RequestHeader("Goo
try {
JwtDTO jwtResponse = authService.googleJoin(googleIdTokenHeader, requestUserInfo);
SuccessResponseDTO<JwtDTO> response = SuccessResponseDTO.<JwtDTO>builder()
.status(CustomSuccessCode.GOOGLE_SIGNUP_SUCCESS.getHttpStatus().value())
.message(CustomSuccessCode.GOOGLE_SIGNUP_SUCCESS.getMessage())
.status(SuccessCode.GOOGLE_SIGNUP_SUCCESS.getHttpStatus().value())
.message(SuccessCode.GOOGLE_SIGNUP_SUCCESS.getMessage())
.data(jwtResponse)
.build();
return ResponseEntity.status(HttpStatus.CREATED).body(response);
} catch (GeneralSecurityException | IOException | IllegalArgumentException e) {
log.error("Google ID Token verification failed: {}", e.getMessage(), e);
throw new CustomException(CustomErrorCode.GOOGLE_TOKEN_VERIFICATION_FAILED);
throw new CustomException(ErrorCode.GOOGLE_TOKEN_VERIFICATION_FAILED);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.tico.pomoro_do.domain.user.entity;

import com.tico.pomoro_do.global.common.enums.SocialProvider;
import com.tico.pomoro_do.global.enums.SocialProvider;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Entity
@Table(name = "social_login")
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.tico.pomoro_do.domain.user.entity;

import com.tico.pomoro_do.global.common.enums.UserRole;
import com.tico.pomoro_do.global.common.enums.UserStatus;
import com.tico.pomoro_do.global.enums.UserRole;
import com.tico.pomoro_do.global.enums.UserStatus;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.tico.pomoro_do.domain.user.dto.response.JwtDTO;
import com.tico.pomoro_do.domain.user.entity.User;
import com.tico.pomoro_do.domain.user.repository.UserRepository;
import com.tico.pomoro_do.global.common.enums.UserRole;
import com.tico.pomoro_do.global.exception.CustomErrorCode;
import com.tico.pomoro_do.global.enums.UserRole;
import com.tico.pomoro_do.global.code.ErrorCode;
import com.tico.pomoro_do.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -92,7 +92,7 @@ private String getEmailDomain(String email) {
private void validateAdminEmailDomain(String domain) {
if (!ADMIN_EMAIL_DOMAIN.equals(domain)) {
log.error("유효하지 않은 이메일 도메인: {}", domain);
throw new CustomException(CustomErrorCode.ADMIN_EMAIL_ONLY);
throw new CustomException(ErrorCode.ADMIN_EMAIL_ONLY);
}
}

Expand All @@ -105,7 +105,7 @@ private void validateAdminEmailDomain(String domain) {
private void checkUserExistence(String username) {
if (userRepository.existsByUsername(username)) {
log.error("이미 등록된 사용자: {}", username);
throw new CustomException(CustomErrorCode.USER_ALREADY_REGISTERED);
throw new CustomException(ErrorCode.USER_ALREADY_REGISTERED);
}
}

Expand All @@ -120,16 +120,16 @@ private void validateAdminUser(String username, String nickname) {
Optional<User> userData = userRepository.findByUsername(username);
if (userData.isEmpty()) {
log.error("사용자를 찾을 수 없음: {}", username);
throw new CustomException(CustomErrorCode.USER_NOT_FOUND);
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}
User admin = userData.get();
if (!admin.getRole().equals(UserRole.ADMIN)) {
log.error("관리자 권한 없음: {}", username);
throw new CustomException(CustomErrorCode.NOT_AN_ADMIN);
throw new CustomException(ErrorCode.NOT_AN_ADMIN);
}
if (!admin.getNickname().equals(nickname)) {
log.error("닉네임 불일치: {}", username);
throw new CustomException(CustomErrorCode.ADMIN_LOGIN_FAILED);
throw new CustomException(ErrorCode.ADMIN_LOGIN_FAILED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.tico.pomoro_do.domain.user.dto.request.GoogleJoinDTO;
import com.tico.pomoro_do.domain.user.dto.response.JwtDTO;
import com.tico.pomoro_do.domain.user.entity.User;
import com.tico.pomoro_do.global.common.enums.TokenType;
import com.tico.pomoro_do.global.common.enums.UserRole;
import com.tico.pomoro_do.global.enums.TokenType;
import com.tico.pomoro_do.global.enums.UserRole;

import java.io.IOException;
import java.security.GeneralSecurityException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import com.tico.pomoro_do.domain.user.repository.SocialLoginRepository;
import com.tico.pomoro_do.domain.user.repository.UserRepository;
import com.tico.pomoro_do.global.auth.jwt.JWTUtil;
import com.tico.pomoro_do.global.common.enums.SocialProvider;
import com.tico.pomoro_do.global.common.enums.TokenType;
import com.tico.pomoro_do.global.common.enums.UserRole;
import com.tico.pomoro_do.global.exception.CustomErrorCode;
import com.tico.pomoro_do.global.enums.SocialProvider;
import com.tico.pomoro_do.global.enums.TokenType;
import com.tico.pomoro_do.global.enums.UserRole;
import com.tico.pomoro_do.global.code.ErrorCode;
import com.tico.pomoro_do.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -74,7 +74,7 @@ public GoogleUserInfoDTO verifyGoogleIdToken(String idToken) throws GeneralSecur
.pictureUrl((String) payload.get("picture"))
.build();
} else {
throw new CustomException(CustomErrorCode.GOOGLE_TOKEN_VERIFICATION_FAILED);
throw new CustomException(ErrorCode.GOOGLE_TOKEN_VERIFICATION_FAILED);
}
}

Expand Down Expand Up @@ -108,9 +108,9 @@ public JwtDTO createJwtTokens(String email, String role) {
public String extractToken(String header, TokenType tokenType) {

if (header == null || header.isEmpty() || !header.startsWith("Bearer ")) {
CustomErrorCode errorCode = tokenType.equals(TokenType.GOOGLE)
? CustomErrorCode.INVALID_GOOGLE_TOKEN_HEADER
: CustomErrorCode.INVALID_AUTHORIZATION_HEADER;
ErrorCode errorCode = tokenType.equals(TokenType.GOOGLE)
? ErrorCode.INVALID_GOOGLE_TOKEN_HEADER
: ErrorCode.INVALID_AUTHORIZATION_HEADER;
throw new CustomException(errorCode);
}

Expand All @@ -134,7 +134,7 @@ public JwtDTO googleLogin(String idTokenHeader) throws GeneralSecurityException,
GoogleUserInfoDTO userInfo = verifyGoogleIdToken(idToken);

if (!userRepository.existsByUsername(userInfo.getEmail())) {
throw new CustomException(CustomErrorCode.USER_NOT_FOUND);
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}

return createJwtTokens(userInfo.getEmail(), String.valueOf(UserRole.USER));
Expand All @@ -157,7 +157,7 @@ public JwtDTO googleJoin(String idTokenHeader, GoogleJoinDTO requestUserInfo) th
GoogleUserInfoDTO userInfo = verifyGoogleIdToken(idToken);

if (userRepository.existsByUsername(userInfo.getEmail())) {
throw new CustomException(CustomErrorCode.USER_ALREADY_REGISTERED);
throw new CustomException(ErrorCode.USER_ALREADY_REGISTERED);
}

// 사용자 정보 저장
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.gson.Gson;
import com.tico.pomoro_do.domain.user.dto.UserDTO;
import com.tico.pomoro_do.global.auth.CustomUserDetails;
import com.tico.pomoro_do.global.exception.CustomErrorCode;
import com.tico.pomoro_do.global.code.ErrorCode;
import com.tico.pomoro_do.global.exception.ErrorResponseEntity;
import io.jsonwebtoken.ExpiredJwtException;
import jakarta.servlet.FilterChain;
Expand Down Expand Up @@ -106,7 +106,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
// -> 토큰 만료시 refesh토큰으로 access토큰을 재발급해야하기 때문이다.

//response Error
sendErrorResponse(response, CustomErrorCode.ACCESS_TOKEN_EXPIRED);
sendErrorResponse(response, ErrorCode.ACCESS_TOKEN_EXPIRED);
return;
}

Expand All @@ -119,7 +119,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
log.info("유효하지 않은 Access 토큰입니다. Access 토큰이 아닙니다.");

//response Error
sendErrorResponse(response, CustomErrorCode.INVALID_ACCESS_TOKEN);
sendErrorResponse(response, ErrorCode.INVALID_ACCESS_TOKEN);
return;
}

Expand Down Expand Up @@ -163,15 +163,15 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
filterChain.doFilter(request, response);
}

private void sendErrorResponse(HttpServletResponse response, CustomErrorCode errorCode) throws IOException {
private void sendErrorResponse(HttpServletResponse response, ErrorCode errorCode) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

//response body
ErrorResponseEntity errorResponse = ErrorResponseEntity.builder()
.status(errorCode.getHttpStatus().value())
.name(errorCode.name())
// .name(errorCode.name())
.code(errorCode.getCode())
.message(errorCode.getMessage())
.build();
Expand Down

This file was deleted.

Loading

0 comments on commit 8d20813

Please sign in to comment.