Skip to content

Commit

Permalink
[BE] 예외 상황시 예외 코드를 반환 (#297)
Browse files Browse the repository at this point in the history
* refactor: 예외 응답값에 예외 코드 추가

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>

* refactor: 예외 클래스 명 수정

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>

Co-authored-by: hamcheeseburger <[email protected]>
Co-authored-by: yangdongjue5510 <[email protected]>
  • Loading branch information
3 people authored Jul 29, 2022
1 parent dfea906 commit 18e6863
Show file tree
Hide file tree
Showing 45 changed files with 236 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.woowacourse.f12.dto.request.inventoryproduct.ProfileProductRequest;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductsResponse;
import com.woowacourse.f12.exception.badrequest.InvalidProfileProductException;
import com.woowacourse.f12.exception.notfound.InventoryItemNotFoundException;
import com.woowacourse.f12.exception.notfound.InventoryProductNotFoundException;
import com.woowacourse.f12.exception.notfound.MemberNotFoundException;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -51,7 +51,7 @@ private void updateProfileProduct(final ProfileProductRequest profileProductRequ

private void updateProfileProduct(final Long inventoryItemId, final boolean selected) {
final InventoryProduct inventoryProduct = inventoryProductRepository.findById(inventoryItemId)
.orElseThrow(InventoryItemNotFoundException::new);
.orElseThrow(InventoryProductNotFoundException::new);
inventoryProduct.updateSelected(selected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.woowacourse.f12.domain.product.ProductRepository;
import com.woowacourse.f12.dto.response.product.ProductPageResponse;
import com.woowacourse.f12.dto.response.product.ProductResponse;
import com.woowacourse.f12.exception.notfound.KeyboardNotFoundException;
import com.woowacourse.f12.exception.notfound.ProductNotFoundException;
import com.woowacourse.f12.presentation.product.CategoryConstant;
import java.util.Objects;
import org.springframework.data.domain.Pageable;
Expand All @@ -24,7 +24,7 @@ public ProductService(final ProductRepository productRepository) {

public ProductResponse findById(final Long id) {
final Product product = productRepository.findById(id)
.orElseThrow(KeyboardNotFoundException::new);
.orElseThrow(ProductNotFoundException::new);
return ProductResponse.from(product);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.woowacourse.f12.dto.response.review.ReviewWithProductPageResponse;
import com.woowacourse.f12.exception.badrequest.AlreadyWrittenReviewException;
import com.woowacourse.f12.exception.forbidden.NotAuthorException;
import com.woowacourse.f12.exception.notfound.KeyboardNotFoundException;
import com.woowacourse.f12.exception.notfound.MemberNotFoundException;
import com.woowacourse.f12.exception.notfound.ProductNotFoundException;
import com.woowacourse.f12.exception.notfound.ReviewNotFoundException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
Expand Down Expand Up @@ -45,7 +45,7 @@ public Long saveReviewAndInventoryProduct(final Long productId, final Long membe
final Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
final Product product = productRepository.findById(productId)
.orElseThrow(KeyboardNotFoundException::new);
.orElseThrow(ProductNotFoundException::new);
final Long reviewId = saveReview(reviewRequest, member, product);
saveInventoryProduct(member, product);
return reviewId;
Expand Down Expand Up @@ -83,7 +83,7 @@ public ReviewPageResponse findPageByProductId(final Long productId, final Pageab

private void validateKeyboardExists(final Long productId) {
if (!productRepository.existsById(productId)) {
throw new KeyboardNotFoundException();
throw new ProductNotFoundException();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.woowacourse.f12.dto.response;

import com.woowacourse.f12.exception.CustomException;
import com.woowacourse.f12.exception.ErrorCode;
import lombok.Getter;

@Getter
public class ExceptionResponse {

private String message;
private ErrorCode errorCode;

private ExceptionResponse() {
}

private ExceptionResponse(final String message) {
private ExceptionResponse(final String message, final ErrorCode errorCode) {
this.message = message;
this.errorCode = errorCode;
}

public static ExceptionResponse from(final Exception e) {
return new ExceptionResponse(e.getMessage());
public static ExceptionResponse from(final CustomException e) {
return new ExceptionResponse(e.getMessage(), e.getErrorCode());
}

public static ExceptionResponse from(final String message) {
return new ExceptionResponse(message);
public static ExceptionResponse from(final String message, final ErrorCode errorCode) {
return new ExceptionResponse(message, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.woowacourse.f12.exception;

public class CustomException extends RuntimeException {

private final ErrorCode errorCode;

public CustomException(final String message, final ErrorCode errorCode) {
super(message);
this.errorCode = errorCode;
}

public ErrorCode getErrorCode() {
return errorCode;
}
}
35 changes: 35 additions & 0 deletions backend/src/main/java/com/woowacourse/f12/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.woowacourse.f12.exception;

import com.fasterxml.jackson.annotation.JsonValue;

public enum ErrorCode {

INVALID_SEARCH_PARAM("40000"),
INVALID_REQUEST_BODY("40001"),
INVALID_TOKEN("40002"),
DUPLICATED_CONTENT("40003"),
BLANK_CONTENT("40004"),
NOT_ENOUGH_DATA("40005"),

TOKEN_NOT_EXISTS("40100"),
TOKEN_EXPIRED("400101"),
TOKEN_INVALID("400102"),

PERMISSION_DENIED("40300"),

DATA_NOT_FOUND("40400"),

INTERNAL_SERVER_ERROR("50000"),
EXTERNAL_SERVER_ERROR("50001");

private final String value;

ErrorCode(final String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.DUPLICATED_CONTENT;

public class AlreadyWrittenReviewException extends InvalidValueException {

public AlreadyWrittenReviewException() {
super("해당 제품에 대해 이미 리뷰가 작성되어 있습니다.");
super("해당 제품에 대해 이미 리뷰가 작성되어 있습니다.", DUPLICATED_CONTENT);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.BLANK_CONTENT;

public class BlankContentException extends InvalidValueException {

public BlankContentException() {
super("리뷰 내용은 공백이 될 수 없습니다.");
super("리뷰 내용은 공백이 될 수 없습니다.", BLANK_CONTENT);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_SEARCH_PARAM;

public class InvalidCareerLevelException extends InvalidValueException {

public InvalidCareerLevelException() {
super("올바르지 않은 연차 입력입니다.");
super("올바르지 않은 연차 입력입니다.", INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_SEARCH_PARAM;

public class InvalidCategoryValueException extends InvalidValueException {

public InvalidCategoryValueException() {
super("잘못된 카테고리입니다.");
super("잘못된 카테고리입니다.", INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_REQUEST_BODY;

public class InvalidContentLengthException extends InvalidValueException {

public InvalidContentLengthException(final int maxLength) {
super("내용의 길이는 " + maxLength + "자 이하 여야 합니다.");
super("내용의 길이는 " + maxLength + "자 이하 여야 합니다.", INVALID_REQUEST_BODY);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_SEARCH_PARAM;

public class InvalidGitHubLoginException extends InvalidValueException {

public InvalidGitHubLoginException() {
super("잘못된 GitHub 로그인 요청입니다.");
super("잘못된 GitHub 로그인 요청입니다.", INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import com.woowacourse.f12.exception.ErrorCode;

public class InvalidJobTypeException extends InvalidValueException {

public InvalidJobTypeException() {
super("올바르지 않은 직군 입력입니다.");
super("올바르지 않은 직군 입력입니다.", ErrorCode.INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_SEARCH_PARAM;

public class InvalidPageNumberFormatException extends InvalidValueException {

public InvalidPageNumberFormatException() {
super("페이지 번호는 숫자 형식이여야 합니다.");
super("페이지 번호는 숫자 형식이여야 합니다.", INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_SEARCH_PARAM;

public class InvalidPageSizeException extends InvalidValueException {

public InvalidPageSizeException(int maxSize) {
super("페이지의 크기는" + maxSize + "이하여야 합니다.");
super("페이지의 크기는" + maxSize + "이하여야 합니다.", INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_SEARCH_PARAM;

public class InvalidPageSizeFormatException extends InvalidValueException {

public InvalidPageSizeFormatException() {
super("패이지 크기는 숫자 형식이어야 합니다.");
super("패이지 크기는 숫자 형식이어야 합니다.", INVALID_SEARCH_PARAM);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.woowacourse.f12.exception.badrequest;


import static com.woowacourse.f12.exception.ErrorCode.NOT_ENOUGH_DATA;

public class InvalidProfileArgumentException extends InvalidValueException {

public InvalidProfileArgumentException() {
super("추가 정보가 등록되지 않았습니다.");
super("추가 정보가 등록되지 않았습니다.", NOT_ENOUGH_DATA);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_REQUEST_BODY;

public class InvalidProfileProductException extends InvalidValueException {

public InvalidProfileProductException() {
super("유효하지 않은 대표 장비 수정 요청입니다.");
super("유효하지 않은 대표 장비 수정 요청입니다.", INVALID_REQUEST_BODY);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.badrequest;

import static com.woowacourse.f12.exception.ErrorCode.INVALID_REQUEST_BODY;

public class InvalidRatingValueException extends InvalidValueException {

public InvalidRatingValueException() {
super("평점은 1에서 5 사이여야 합니다.");
super("평점은 1에서 5 사이여야 합니다.", INVALID_REQUEST_BODY);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.woowacourse.f12.exception.badrequest;

public class InvalidValueException extends RuntimeException {
import com.woowacourse.f12.exception.CustomException;
import com.woowacourse.f12.exception.ErrorCode;

public InvalidValueException(final String message) {
super(message);
public class InvalidValueException extends CustomException {

public InvalidValueException(final String message, final ErrorCode errorCode) {
super(message, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.woowacourse.f12.exception.forbidden;

public class ForbiddenMemberException extends RuntimeException {
import com.woowacourse.f12.exception.CustomException;
import com.woowacourse.f12.exception.ErrorCode;

public ForbiddenMemberException(final String message) {
super(message);
public class ForbiddenMemberException extends CustomException {

public ForbiddenMemberException(final String message, final ErrorCode errorCode) {
super(message, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.forbidden;

import static com.woowacourse.f12.exception.ErrorCode.PERMISSION_DENIED;

public class NotAuthorException extends ForbiddenMemberException {

public NotAuthorException() {
super("리뷰의 작성자가 아닙니다.");
super("리뷰의 작성자가 아닙니다.", PERMISSION_DENIED);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.woowacourse.f12.exception.internalserver;

public class ExternalServerException extends RuntimeException {
import com.woowacourse.f12.exception.CustomException;
import com.woowacourse.f12.exception.ErrorCode;

public ExternalServerException(final String message) {
super(message);
public class ExternalServerException extends CustomException {

public ExternalServerException(final String message, final ErrorCode errorCode) {
super(message, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.exception.internalserver;

import static com.woowacourse.f12.exception.ErrorCode.EXTERNAL_SERVER_ERROR;

public class GitHubServerException extends ExternalServerException {

public GitHubServerException() {
super("GitHub 서버에 문제가 있습니다.");
super("GitHub 서버에 문제가 있습니다.", EXTERNAL_SERVER_ERROR);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.f12.exception.notfound;

import static com.woowacourse.f12.exception.ErrorCode.DATA_NOT_FOUND;

public class InventoryProductNotFoundException extends NotFoundException {

public InventoryProductNotFoundException() {
super("등록 장비를 찾을 수 없습니다.", DATA_NOT_FOUND);
}
}
Loading

0 comments on commit 18e6863

Please sign in to comment.