-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: ClientRequestException 일반 클래스로 변경 * refactor: ErrorResponse 에서 httpStatus 제거 * refactor: RunnerPostUpdateRequest record로 변경 * refactor: ClientErrorCode 내용 추가 * refactor: record로 생긴 변화 적용 * feat: NotNullValid 어노테이션 추가 * refactor: ErrorResponse 에 getter 추가 * feat: Controller valid 추가 * feat: ValidFuture 어노테이션 추가 * feat: 어노테이션 이름 변경 * feat: Max validator 추가 * feat: request dto에 validation 적용 * refactor: 어노테이션 target 조정 * refactor: tag 값이 없을 때 Bad Request 보내도록 수정
- Loading branch information
Showing
15 changed files
with
242 additions
and
103 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
backend/baton/src/main/java/touch/baton/domain/common/GlobalControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package touch.baton.domain.common; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import touch.baton.domain.common.exception.ClientRequestException; | ||
import touch.baton.domain.common.response.ErrorResponse; | ||
|
||
@RestControllerAdvice | ||
public class GlobalControllerAdvice { | ||
|
||
@ExceptionHandler(ClientRequestException.class) | ||
public ResponseEntity<ErrorResponse> handleClientRequest(ClientRequestException e) { | ||
return ResponseEntity.status(e.getHttpStatus().value()).body(ErrorResponse.from(e)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
backend/baton/src/main/java/touch/baton/domain/common/exception/ClientRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...d/baton/src/main/java/touch/baton/domain/common/exception/validator/NotNullValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package touch.baton.domain.common.exception.validator; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
import touch.baton.domain.common.exception.ClientRequestException; | ||
|
||
public class NotNullValidator implements ConstraintValidator<ValidNotNull, Object> { | ||
|
||
private ClientErrorCode errorCode; | ||
|
||
@Override | ||
public void initialize(final ValidNotNull constraintAnnotation) { | ||
errorCode = constraintAnnotation.clientErrorCode(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(final Object value, final ConstraintValidatorContext context) { | ||
if (value == null) { | ||
throw new ClientRequestException(errorCode); | ||
} | ||
return true; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/baton/src/main/java/touch/baton/domain/common/exception/validator/ValidNotNull.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package touch.baton.domain.common.exception.validator; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ FIELD, PARAMETER }) | ||
@Retention(RUNTIME) | ||
@Repeatable(ValidNotNull.List.class) | ||
@Documented | ||
@Constraint(validatedBy = NotNullValidator.class) | ||
public @interface ValidNotNull { | ||
|
||
String message() default "null 값이 존재합니다."; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
ClientErrorCode clientErrorCode(); | ||
|
||
@Target({ FIELD, PARAMETER }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
ValidNotNull[] value(); | ||
} | ||
} |
9 changes: 4 additions & 5 deletions
9
backend/baton/src/main/java/touch/baton/domain/common/response/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
package touch.baton.domain.common.response; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import lombok.Getter; | ||
import touch.baton.domain.common.exception.BaseException; | ||
|
||
@Getter | ||
public class ErrorResponse { | ||
|
||
private final HttpStatus status; | ||
private final String errorCode; | ||
private final String message; | ||
|
||
private ErrorResponse(final HttpStatus status, final String errorCode, final String message) { | ||
this.status = status; | ||
private ErrorResponse(final String errorCode, final String message) { | ||
this.errorCode = errorCode; | ||
this.message = message; | ||
} | ||
|
||
public static ErrorResponse from(final BaseException e) { | ||
return new ErrorResponse(e.getHttpStatus(), e.getErrorCode(), e.getMessage()); | ||
return new ErrorResponse(e.getErrorCode(), e.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...aton/src/main/java/touch/baton/domain/runnerpost/exception/validator/FutureValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package touch.baton.domain.runnerpost.exception.validator; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
import touch.baton.domain.common.exception.ClientRequestException; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class FutureValidator implements ConstraintValidator<ValidFuture, LocalDateTime> { | ||
|
||
private ClientErrorCode errorCode; | ||
|
||
@Override | ||
public void initialize(final ValidFuture constraintAnnotation) { | ||
errorCode = constraintAnnotation.clientErrorCode(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(final LocalDateTime value, final ConstraintValidatorContext context) { | ||
if (value.isBefore(LocalDateTime.now())) { | ||
throw new ClientRequestException(errorCode); | ||
} | ||
return true; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...n/src/main/java/touch/baton/domain/runnerpost/exception/validator/MaxLengthValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package touch.baton.domain.runnerpost.exception.validator; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
import touch.baton.domain.common.exception.ClientRequestException; | ||
|
||
public class MaxLengthValidator implements ConstraintValidator<ValidMaxLength, String> { | ||
|
||
private ClientErrorCode errorCode; | ||
private int max; | ||
|
||
@Override | ||
public void initialize(final ValidMaxLength constraintAnnotation) { | ||
errorCode = constraintAnnotation.clientErrorCode(); | ||
max = constraintAnnotation.max(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(final String value, final ConstraintValidatorContext context) { | ||
if (value.length() > max) { | ||
throw new ClientRequestException(errorCode); | ||
} | ||
return true; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...nd/baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/ValidFuture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package touch.baton.domain.runnerpost.exception.validator; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ FIELD, PARAMETER }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = FutureValidator.class) | ||
public @interface ValidFuture { | ||
|
||
String message() default "마감일은 오늘보다 과거일 수 없습니다."; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
ClientErrorCode clientErrorCode(); | ||
} |
30 changes: 30 additions & 0 deletions
30
...baton/src/main/java/touch/baton/domain/runnerpost/exception/validator/ValidMaxLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package touch.baton.domain.runnerpost.exception.validator; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ FIELD, PARAMETER}) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = MaxLengthValidator.class) | ||
public @interface ValidMaxLength { | ||
|
||
String message() default "길이가 잘못되었습니다."; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
ClientErrorCode clientErrorCode(); | ||
|
||
int max(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 12 additions & 3 deletions
15
...aton/src/main/java/touch/baton/domain/runnerpost/service/dto/RunnerPostCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
package touch.baton.domain.runnerpost.service.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import touch.baton.domain.common.exception.ClientErrorCode; | ||
import touch.baton.domain.common.exception.validator.ValidNotNull; | ||
import touch.baton.domain.runnerpost.exception.validator.ValidFuture; | ||
import touch.baton.domain.runnerpost.exception.validator.ValidMaxLength; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public record RunnerPostCreateRequest(String title, | ||
public record RunnerPostCreateRequest(@ValidNotNull(clientErrorCode = ClientErrorCode.TITLE_IS_NULL) | ||
String title, | ||
@ValidNotNull(clientErrorCode = ClientErrorCode.TAGS_ARE_NULL) | ||
List<String> tags, | ||
@ValidNotNull(clientErrorCode = ClientErrorCode.PULL_REQUEST_URL_IS_NULL) | ||
String pullRequestUrl, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm", timezone = "Asia/Seoul") | ||
@ValidNotNull(clientErrorCode = ClientErrorCode.DEADLINE_IS_NULL) | ||
@ValidFuture(clientErrorCode = ClientErrorCode.PAST_DEADLINE) | ||
LocalDateTime deadline, | ||
@ValidNotNull(clientErrorCode = ClientErrorCode.CONTENTS_ARE_NULL) | ||
@ValidMaxLength(clientErrorCode = ClientErrorCode.CONTENTS_OVERFLOW, max = 1000) | ||
String contents | ||
) { | ||
} |
Oops, something went wrong.