Skip to content

Commit

Permalink
Merge pull request #304 from linglong67/feature/fix-badword-exceoption
Browse files Browse the repository at this point in the history
[fix] 제품/세차장 리뷰 도메인에 비속어 필터링 적용 및 글로벌 Exception 수정
  • Loading branch information
linglong67 committed Mar 15, 2024
2 parents 95a0d83 + ec6b528 commit 6e55dad
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kernel360.review.dto;

import com.kernel360.code.common.ValidationMessage;
import com.kernel360.global.annotation.BadWordFilter;
import com.kernel360.member.entity.Member;
import com.kernel360.product.entity.Product;
Expand All @@ -16,10 +17,8 @@ public record ReviewRequestDto(Long reviewNo,
Long productNo,
Long memberNo,
BigDecimal starRating,
@BadWordFilter
String title,
@BadWordFilter
String contents,
@BadWordFilter(message = ValidationMessage.INVALID_WORD_PARAMETER) String title,
@BadWordFilter(message = ValidationMessage.INVALID_WORD_PARAMETER) String contents,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.kernel360.washzonereview.dto.WashzoneReviewRequestDto;
import com.kernel360.washzonereview.dto.WashzoneReviewResponseDto;
import com.kernel360.washzonereview.service.WashzoneReviewService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -47,7 +48,7 @@ public ResponseEntity<ApiResponse<WashzoneReviewResponseDto>> getWashzoneReview(

@PostMapping
public <T> ResponseEntity<ApiResponse<T>> createWashzoneReview(
@RequestPart WashzoneReviewRequestDto washzoneReview,
@Valid @RequestPart WashzoneReviewRequestDto washzoneReview,
@RequestPart(required = false) List<MultipartFile> files,
@RequestHeader("Id") String id) {
washzoneReviewService.createWashzoneReview(washzoneReview, files, id);
Expand All @@ -57,7 +58,7 @@ public <T> ResponseEntity<ApiResponse<T>> createWashzoneReview(

@PatchMapping
public <T> ResponseEntity<ApiResponse<T>> updateWashzoneReview(
@RequestPart WashzoneReviewRequestDto washzoneReview,
@Valid @RequestPart WashzoneReviewRequestDto washzoneReview,
@RequestPart(required = false) List<MultipartFile> files,
@RequestHeader("Id") String id) {
washzoneReviewService.updateWashzoneReview(washzoneReview, files, id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kernel360.washzonereview.dto;

import com.kernel360.code.common.ValidationMessage;
import com.kernel360.global.annotation.BadWordFilter;
import com.kernel360.member.entity.Member;
import com.kernel360.washzone.entity.WashZone;
import com.kernel360.washzonereview.entity.WashzoneReview;
Expand All @@ -15,8 +17,8 @@ public record WashzoneReviewRequestDto(Long washzoneReviewNo,
Long washzoneNo,
Long memberNo,
BigDecimal starRating,
String title,
String contents,
@BadWordFilter(message = ValidationMessage.INVALID_WORD_PARAMETER) String title,
@BadWordFilter(message = ValidationMessage.INVALID_WORD_PARAMETER) String contents,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
Expand Down
3 changes: 3 additions & 0 deletions module-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ dependencies {

// aws s3
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.625'

// commons-lang3
implementation 'org.apache.commons:commons-lang3:3.12.0'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum CommonErrorCode implements ErrorCode {
INVALID_ARGUMENT(HttpStatus.BAD_REQUEST.value(), "E006", "요청 파라미터가 없거나 비어있거나, 요청 파라미터의 이름이 메서드 인수의 이름과 일치하지 않습니다"),
INVALID_HTTP_REQUEST_METHOD(HttpStatus.BAD_REQUEST.value(), "E007", "요청 URL 에서 지원하지 않는 HTTP Method 입니다."),
INVALID_REQUEST_PARAMETER(HttpStatus.BAD_REQUEST.value(), "E008", "요청한 파라미터가 존재하지 않음"),
INVALID_WORD_PARAMETER(HttpStatus.BAD_REQUEST.value(), "E009", "비속어를 포함할 수 없습니다.");
ARGUMENT_VALIDATION_FAILED(HttpStatus.BAD_REQUEST.value(), "E009", "유효하지 않은 데이터가 존재함");

private final int status;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kernel360.code.common;

public class ValidationMessage {
public static final String INVALID_WORD_PARAMETER = "비속어를 포함할 수 없습니다";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
Expand All @@ -16,7 +17,6 @@
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

@Slf4j
@RestControllerAdvice
Expand Down Expand Up @@ -77,21 +77,21 @@ protected ResponseEntity<ErrorResponse> handleMissingHeaderException(final Missi
}

@ExceptionHandler(IllegalArgumentException.class)
protected ResponseEntity<ErrorResponse> handleIllegalArgumentException(final IllegalArgumentException e){
log.error("handleIllegalArgumentException",e);
protected ResponseEntity<ErrorResponse> handleIllegalArgumentException(final IllegalArgumentException e) {
log.error("handleIllegalArgumentException", e);

final ErrorResponse response = ErrorResponse.of(CommonErrorCode.INVALID_ARGUMENT);

return new ResponseEntity<>(response,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e){
log.error("handleHttpRequestMethodNotSupportedException",e);
protected ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) {
log.error("handleHttpRequestMethodNotSupportedException", e);

final ErrorResponse response =ErrorResponse.of(CommonErrorCode.INVALID_HTTP_REQUEST_METHOD);
final ErrorResponse response = ErrorResponse.of(CommonErrorCode.INVALID_HTTP_REQUEST_METHOD);

return new ResponseEntity<>(response,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(MissingServletRequestParameterException.class)
Expand All @@ -104,10 +104,16 @@ protected ResponseEntity<ErrorResponse> handleMissingParameterException(final Mi
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public final ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException e, WebRequest request) {
log.error("handleMethodArgumentNotValid", e);
ErrorResponse response = ErrorResponse.of(CommonErrorCode.INVALID_WORD_PARAMETER);
protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
log.error("handleMethodArgumentNotValidException", e);

String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
if (!StringUtils.isBlank(defaultMessage)) {
defaultMessage = String.format(" (%s)", defaultMessage);
}

final ErrorResponse response = ErrorResponse.of(CommonErrorCode.ARGUMENT_VALIDATION_FAILED, defaultMessage);

return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public static ErrorResponse of(ErrorCode code) {
code.getMessage()
);
}

public static ErrorResponse of(ErrorCode code, String detailMessage) {
return new ErrorResponse(
code.getStatus(),
code.getCode(),
code.getMessage() + detailMessage
);
}
}

0 comments on commit 6e55dad

Please sign in to comment.