Skip to content

Commit

Permalink
refactor: 패키지 구조 변경 (exception)
Browse files Browse the repository at this point in the history
- 각 도메인 별 ErrorAdvice를 common 패키지의 error 패키지로 이관
- 각 도메인 별 Exception은 도메인 패키지의 exception 패키지에 응집
  • Loading branch information
giibeom committed Nov 19, 2022
1 parent cd65f7a commit d16fb31
Show file tree
Hide file tree
Showing 47 changed files with 193 additions and 227 deletions.
@@ -1,7 +1,7 @@
package com.codesoom.assignment.common.auth;

import com.codesoom.assignment.common.util.JwtUtil;
import com.codesoom.assignment.domain.session.exception.TokenNotExistException;
import com.codesoom.assignment.session.exception.TokenNotExistException;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.common.exception;
package com.codesoom.assignment.common.error;

import lombok.Getter;
import org.springframework.context.support.DefaultMessageSourceResolvable;
Expand Down
@@ -1,19 +1,17 @@
package com.codesoom.assignment.common.exception;
package com.codesoom.assignment.common.error;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
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.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@RestControllerAdvice
public class GlobalErrorAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {Exception.class})
@ResponseStatus
public ErrorResponse handleException(final Exception exception) {
Expand Down
@@ -0,0 +1,23 @@
package com.codesoom.assignment.common.error;

import com.codesoom.assignment.product.exception.ProductNotFoundException;
import com.codesoom.assignment.product.presentation.ProductController;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice(basePackageClasses = {
ProductController.class
})
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ProductErrorAdvice {

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ProductNotFoundException.class)
public ErrorResponse handleProductNotFound() {
return new ErrorResponse("Product not found");
}
}
@@ -1,12 +1,20 @@
package com.codesoom.assignment.domain.session.exception;
package com.codesoom.assignment.common.error;

import com.codesoom.assignment.common.exception.ErrorResponse;
import com.codesoom.assignment.product.presentation.ProductController;
import com.codesoom.assignment.session.exception.InvalidTokenException;
import com.codesoom.assignment.session.exception.TokenNotExistException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@SessionErrorAdvice
public class SessionExceptionHandler {
@RestControllerAdvice(basePackageClasses = {
ProductController.class
})
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SessionErrorAdvice {

@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(InvalidTokenException.class)
Expand Down
@@ -1,12 +1,23 @@
package com.codesoom.assignment.domain.user.exception;
package com.codesoom.assignment.common.error;

import com.codesoom.assignment.common.exception.ErrorResponse;
import com.codesoom.assignment.session.controller.SessionController;
import com.codesoom.assignment.user.exception.UserEmailDuplicationException;
import com.codesoom.assignment.user.exception.UserInvalidPasswordException;
import com.codesoom.assignment.user.exception.UserNotFoundException;
import com.codesoom.assignment.user.presentation.UserController;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@UserErrorAdvice
public class UserExceptionHandler {
@RestControllerAdvice(basePackageClasses = {
UserController.class,
SessionController.class
})
@Order(Ordered.HIGHEST_PRECEDENCE)
public class UserErrorAdvice {

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(UserNotFoundException.class)
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/com/codesoom/assignment/common/util/JwtUtil.java
@@ -1,7 +1,7 @@
package com.codesoom.assignment.common.util;

import com.codesoom.assignment.domain.session.exception.InvalidTokenException;
import com.codesoom.assignment.domain.session.exception.TokenNotExistException;
import com.codesoom.assignment.session.exception.InvalidTokenException;
import com.codesoom.assignment.session.exception.TokenNotExistException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
Expand All @@ -20,6 +20,18 @@ public JwtUtil(@Value("${jwt.secret}") String secretKey) {
this.secretKey = Keys.hmacShaKeyFor(secretKey.getBytes());
}

public void validateAccessToken(String authHeader) {
if (authHeader == null) {
throw new TokenNotExistException();
}

if (!authHeader.startsWith(ACCESS_TOKEN_TYPE)) {
throw new InvalidTokenException();
}

decode(authHeader.substring(ACCESS_TOKEN_TYPE.length()));
}

public String encode(Long id) {
return Jwts.builder()
.claim("userId", id)
Expand All @@ -42,16 +54,4 @@ public Claims decode(String token) {
throw new InvalidTokenException(token);
}
}

public void validateAccessToken(String authHeader) {
if (authHeader == null) {
throw new TokenNotExistException();
}

if (!authHeader.startsWith(ACCESS_TOKEN_TYPE)) {
throw new InvalidTokenException();
}

decode(authHeader.substring(ACCESS_TOKEN_TYPE.length()));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,9 +1,9 @@
package com.codesoom.assignment.domain.product.application;
package com.codesoom.assignment.product.application;

import com.codesoom.assignment.domain.product.domain.Product;
import com.codesoom.assignment.domain.product.domain.ProductRepository;
import com.codesoom.assignment.domain.product.exception.ProductNotFoundException;
import com.codesoom.assignment.domain.product.presentation.dto.ProductData;
import com.codesoom.assignment.product.domain.Product;
import com.codesoom.assignment.product.domain.ProductRepository;
import com.codesoom.assignment.product.exception.ProductNotFoundException;
import com.codesoom.assignment.product.presentation.dto.ProductData;
import com.github.dozermapper.core.Mapper;
import org.springframework.stereotype.Service;

Expand Down
Expand Up @@ -14,7 +14,7 @@
// 3. 가격 - 5,000원 (판매가)
// 4. 이미지 - static, CDN => image URL

package com.codesoom.assignment.domain.product.domain;
package com.codesoom.assignment.product.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.domain.product.domain;
package com.codesoom.assignment.product.domain;

import java.util.List;
import java.util.Optional;
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.domain.product.exception;
package com.codesoom.assignment.product.exception;

public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(Long id) {
Expand Down
@@ -1,7 +1,7 @@
package com.codesoom.assignment.domain.product.infra;
package com.codesoom.assignment.product.infra;

import com.codesoom.assignment.domain.product.domain.Product;
import com.codesoom.assignment.domain.product.domain.ProductRepository;
import com.codesoom.assignment.product.domain.Product;
import com.codesoom.assignment.product.domain.ProductRepository;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
Expand Down
@@ -1,9 +1,9 @@
package com.codesoom.assignment.domain.product.presentation;
package com.codesoom.assignment.product.presentation;

import com.codesoom.assignment.common.auth.Login;
import com.codesoom.assignment.domain.product.application.ProductService;
import com.codesoom.assignment.domain.product.domain.Product;
import com.codesoom.assignment.domain.product.presentation.dto.ProductData;
import com.codesoom.assignment.product.application.ProductService;
import com.codesoom.assignment.product.domain.Product;
import com.codesoom.assignment.product.presentation.dto.ProductData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand Down
@@ -1,7 +1,11 @@
package com.codesoom.assignment.domain.product.presentation.dto;
package com.codesoom.assignment.product.presentation.dto;

import com.github.dozermapper.core.Mapping;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
Expand Down
@@ -1,11 +1,11 @@
package com.codesoom.assignment.domain.session.application;
package com.codesoom.assignment.session.application;

import com.codesoom.assignment.common.util.JwtUtil;
import com.codesoom.assignment.domain.session.controller.dto.SessionRequestDto;
import com.codesoom.assignment.domain.user.domain.User;
import com.codesoom.assignment.domain.user.domain.UserRepository;
import com.codesoom.assignment.domain.user.exception.UserInvalidPasswordException;
import com.codesoom.assignment.domain.user.exception.UserNotFoundException;
import com.codesoom.assignment.session.controller.dto.SessionRequestDto;
import com.codesoom.assignment.user.domain.User;
import com.codesoom.assignment.user.domain.UserRepository;
import com.codesoom.assignment.user.exception.UserInvalidPasswordException;
import com.codesoom.assignment.user.exception.UserNotFoundException;
import org.springframework.stereotype.Service;

@Service
Expand Down
@@ -1,9 +1,9 @@
package com.codesoom.assignment.domain.session.controller;
package com.codesoom.assignment.session.controller;

import com.codesoom.assignment.common.auth.Login;
import com.codesoom.assignment.domain.session.application.AuthenticationService;
import com.codesoom.assignment.domain.session.controller.dto.SessionRequestDto;
import com.codesoom.assignment.domain.session.controller.dto.SessionResponseDto;
import com.codesoom.assignment.session.application.AuthenticationService;
import com.codesoom.assignment.session.controller.dto.SessionRequestDto;
import com.codesoom.assignment.session.controller.dto.SessionResponseDto;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.domain.session.controller.dto;
package com.codesoom.assignment.session.controller.dto;

import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.domain.session.controller.dto;
package com.codesoom.assignment.session.controller.dto;

import lombok.Builder;
import lombok.Getter;
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.domain.session.exception;
package com.codesoom.assignment.session.exception;

public class InvalidTokenException extends RuntimeException {
public InvalidTokenException() {
Expand Down
@@ -1,4 +1,4 @@
package com.codesoom.assignment.domain.session.exception;
package com.codesoom.assignment.session.exception;

public class TokenNotExistException extends RuntimeException {
public TokenNotExistException() {
Expand Down
@@ -1,11 +1,11 @@
package com.codesoom.assignment.domain.user.application;

import com.codesoom.assignment.domain.user.domain.User;
import com.codesoom.assignment.domain.user.domain.UserRepository;
import com.codesoom.assignment.domain.user.exception.UserEmailDuplicationException;
import com.codesoom.assignment.domain.user.exception.UserNotFoundException;
import com.codesoom.assignment.domain.user.presentation.dto.UserModificationData;
import com.codesoom.assignment.domain.user.presentation.dto.UserRegistrationData;
package com.codesoom.assignment.user.application;

import com.codesoom.assignment.user.domain.User;
import com.codesoom.assignment.user.domain.UserRepository;
import com.codesoom.assignment.user.exception.UserEmailDuplicationException;
import com.codesoom.assignment.user.exception.UserNotFoundException;
import com.codesoom.assignment.user.presentation.dto.UserModificationData;
import com.codesoom.assignment.user.presentation.dto.UserRegistrationData;
import com.github.dozermapper.core.Mapper;
import org.springframework.stereotype.Service;

Expand Down

0 comments on commit d16fb31

Please sign in to comment.