Skip to content

Commit

Permalink
수거함 리뷰 작성 기능 예외 작성 (#41)
Browse files Browse the repository at this point in the history
* feat: 수거함 리뷰 작성 시 예외 처리

* chore: config 파일 최신 커밋으로 수정

* refactor: 리뷰 등록 시 내용 없을 경우 예외처리 리팩토링

* style: 불필요한 ErrorCode 삭제
  • Loading branch information
Doyeon04 authored Apr 21, 2024
1 parent ae53f6e commit ea5fb30
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;


@Getter
@AllArgsConstructor
public enum ErrorCode {
// 400
NOT_SELECTED_TAG(BAD_REQUEST, "수거함 태그는 반드시 한 개 이상 설정해야 합니다."),
INVALID_REVIEW_CONTENT(BAD_REQUEST, "올바르지 않는 리뷰 내용입니다."),
INVALID_BEAN(BAD_REQUEST, "유효하지 않은 데이터입니다."),
MISSING_REQUEST_PARAM(BAD_REQUEST, "필수 요청 파라미터가 존재하지 않습니다."),
MISMATCH_REQUEST_PARAM(BAD_REQUEST, "요청 파라미터가 유효하지 않습니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ErrorResponse {

private HttpStatus httpStatus;
private HttpStatus status;
private String message;

public static ErrorResponse from(ErrorCode errorCode) {
return new ErrorResponse(errorCode.getHttpStatus(), errorCode.getMessage());
}

public static ErrorResponse from(ErrorCode errorCode, String message){
return new ErrorResponse(errorCode.getHttpStatus(), message);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package contest.collectingbox.global.exception;



import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import static contest.collectingbox.global.exception.ErrorCode.INVALID_BEAN;
import static contest.collectingbox.global.exception.ErrorCode.MISMATCH_REQUEST_PARAM;
import static contest.collectingbox.global.exception.ErrorCode.MISSING_REQUEST_PARAM;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
Expand All @@ -19,9 +23,18 @@ public class GlobalExceptionHandler {

@ExceptionHandler(CollectingBoxException.class)
public ResponseEntity<ErrorResponse> handleCollectingBoxException(CollectingBoxException e) {
log.error("exception message = {}", e.getMessage());
ErrorResponse errorResponse = ErrorResponse.from(e.getErrorCode());
return ResponseEntity.status(errorResponse.getHttpStatus()).body(errorResponse);
log.error("exception message = {}", e.getMessage());
return ResponseEntity.status(errorResponse.getStatus()).body(errorResponse);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> methodValidException(MethodArgumentNotValidException e) {
String message = e.getBindingResult().getFieldError().getDefaultMessage();
log.error("exception message = {}", e.getMessage());
ErrorResponse errorResponse = ErrorResponse.from(INVALID_BEAN, message);
return ResponseEntity.status(errorResponse.getStatus()).body(errorResponse);

}

@ExceptionHandler(MissingServletRequestParameterException.class)
Expand All @@ -37,4 +50,5 @@ public ErrorResponse handleMissingParams(MethodArgumentTypeMismatchException e)
log.error("exception message = {}", e.getMessage());
return ErrorResponse.from(MISMATCH_REQUEST_PARAM);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package contest.collectingbox.module.review.application;

import static contest.collectingbox.global.exception.ErrorCode.NOT_FOUND_COLLECTING_BOX;

import contest.collectingbox.global.exception.CollectingBoxException;
import contest.collectingbox.module.collectingbox.domain.CollectingBox;
import contest.collectingbox.module.collectingbox.domain.CollectingBoxRepository;
import contest.collectingbox.module.review.domain.ReviewRepository;
Expand All @@ -17,7 +20,7 @@ public class ReviewService {

public Long postReview(ReviewRequest dto, Long collectionId) {
CollectingBox box = collectingBoxRepository.findById(collectionId)
.orElseThrow(() -> new IllegalArgumentException("해당 수거함이 없습니다."));
.orElseThrow(() -> new CollectingBoxException(NOT_FOUND_COLLECTING_BOX));
return reviewRepository.save(dto.toEntity(box)).getId();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package contest.collectingbox.module.review.dto;

import static contest.collectingbox.global.exception.ErrorCode.*;

import contest.collectingbox.global.exception.CollectingBoxException;
import contest.collectingbox.module.collectingbox.domain.CollectingBox;
import contest.collectingbox.module.review.domain.Review;
import contest.collectingbox.module.review.domain.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -12,12 +16,22 @@
@NoArgsConstructor
public class ReviewRequest {
@Schema(description = "리뷰 내용", example = "EXIST", allowableValues = {"EXIST", "DISAPPEAR"})
@NotNull(message = "리뷰 내용을 입력하세요.")
private String content;

public Review toEntity(CollectingBox collectingBox) {
isValidContent(content);
return Review.builder().
collectingBox(collectingBox)
.tag(Tag.valueOf(content))
.build();
}

private void isValidContent(String content) {
try {
Tag.valueOf(content);
} catch (IllegalArgumentException e) {
throw new CollectingBoxException(INVALID_REVIEW_CONTENT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import contest.collectingbox.global.common.ApiResponse;
import contest.collectingbox.module.review.application.ReviewService;
import contest.collectingbox.module.review.dto.ReviewRequest;
import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.util.StringUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -23,7 +26,7 @@ public class ReviewController {

@Operation(summary = "수거함 리뷰 등록", description = "수거함 ID에 해당되는 수거함에 리뷰를 등록합니다.")
@PostMapping("/{collectionId}/review")
public ApiResponse<Long> postReview(@RequestBody ReviewRequest dto, @PathVariable Long collectionId) {
public ApiResponse<Long> postReview(@RequestBody @Valid ReviewRequest dto, @PathVariable Long collectionId) {
log.info("collectionID for post review = {}", collectionId);
return ApiResponse.ok(reviewService.postReview(dto, collectionId));
}
Expand Down

0 comments on commit ea5fb30

Please sign in to comment.