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/#20 예외처리 추가 #29

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 48 additions & 0 deletions src/main/java/play/pluv/ControllerAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package play.pluv;

import java.net.BindException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import play.pluv.base.BaseException;
import play.pluv.base.BaseExceptionType;
import play.pluv.base.BaseResponse;

@RestControllerAdvice
@Slf4j
public class ControllerAdvice {

@ExceptionHandler(BaseException.class)
public BaseResponse<String> handleCustomException(final BaseException exception) {
final BaseExceptionType type = exception.getExceptionType();
loggingClientException(exception, type.getMessage());
return BaseResponse.exception(type);
}

@ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentNotValidException.class})
public BaseResponse<String> handleHttpMessageNotReadableException(final Exception e) {
final String message = "입력 형식이 올바르지 않습니다.";
loggingClientException(e, message);
return BaseResponse.badRequest(message);
}

@ExceptionHandler(BindException.class)
public BaseResponse<String> handleBindExceptionHandler(final Exception e) {
final String message = "요청 파라미터가 올바르지 않습니다.";
loggingClientException(e, message);
return BaseResponse.badRequest(message);
}

private void loggingClientException(final Exception e, final String message) {
log.warn("[WARN] MESSAGE: {}", message);
log.debug("stackTrace : ", e);
}

@ExceptionHandler(Exception.class)
public BaseResponse<String> handleUnExpectedException(final Exception exception) {
log.error("[ERROR] MESSAGE : ", exception);
return BaseResponse.serverError("잠시 후 다시 시도해주시고 동일한 에러가 반복되는 경우 문의 부탁드립니다.");
}
}
13 changes: 8 additions & 5 deletions src/main/java/play/pluv/base/BaseException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package play.pluv.base;

public class BaseException extends RuntimeException {
import lombok.Getter;

private final BaseExceptionType baseExceptionType;
@Getter
public abstract class BaseException extends RuntimeException {

public BaseException(final BaseExceptionType baseExceptionType) {
super(baseExceptionType.getMessage());
this.baseExceptionType = baseExceptionType;
private final BaseExceptionType exceptionType;

public BaseException(final BaseExceptionType exceptionType) {
super(exceptionType.getMessage());
this.exceptionType = exceptionType;
}
}
26 changes: 22 additions & 4 deletions src/main/java/play/pluv/base/BaseResponse.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package play.pluv.base;

import org.springframework.http.HttpStatus;

public record BaseResponse<T>(
int code,
String msg,
T data
) {

private static final String EXCEPTION_DATA = "";

public static <T> BaseResponse<T> ok(final T data) {
return new BaseResponse<>(200, "Ok", data);
}

public static <T> BaseResponse<T> badRequest(final T data, final String msg) {
return new BaseResponse<>(400, msg, data);
public static BaseResponse<String> badRequest(final String msg) {
return new BaseResponse<>(400, msg, EXCEPTION_DATA);
}

public static BaseResponse<String> notFound(final String msg) {
return new BaseResponse<>(404, msg, EXCEPTION_DATA);
}

public static BaseResponse<String> serverError(final String msg) {
return new BaseResponse<>(500, msg, EXCEPTION_DATA);
}

public static BaseResponse<String> exception(final BaseExceptionType exceptionType) {
return new BaseResponse<>(
exceptionType.getHttpStatus().value(), exceptionType.getMessage(), EXCEPTION_DATA
);
}

public static <T> BaseResponse<T> notFound(final T data, final String msg) {
return new BaseResponse<>(404, msg, data);
public static BaseResponse<String> of(final HttpStatus httpStatus, final String msg) {
return new BaseResponse<>(httpStatus.value(), msg, EXCEPTION_DATA);
}
}
Loading