Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature/BE] 예외 상황시 예외 코드를 반환 #298

Merged
merged 6 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에러 코드가 메시지보다 더 중요해 보이는데 둘의 순서를 바꾸는 건 어떤가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자 매개변수 순서를 바꾸겠습니다.


public ErrorCode getErrorCode() {
return errorCode;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

롬복의 @Getter를 써도 될 것 같네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

적용했습니다

}
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"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 두 곳의 에러코드만 6자리군요!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TOKEN_EXPIRED("400101"),
TOKEN_INVALID("400102"),
TOKEN_EXPIRED("40101"),
TOKEN_INVALID("40102"),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다.


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