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

예외 처리 응답 포맷 수정 #100

Merged
merged 1 commit into from
May 20, 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package contest.collectingbox.global.common;

import contest.collectingbox.global.exception.ErrorCode;
import lombok.Getter;
import lombok.ToString;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.OK;
Expand All @@ -23,10 +23,20 @@ private ApiResponse(HttpStatus status, String message, T data) {
this.data = data;
}

private ApiResponse(HttpStatus status, String message) {
this.code = status.value();
this.status = status;
this.message = message;
}

public static <T> ApiResponse<T> ok(T data) {
return new ApiResponse<>(OK, SUCCESS, data);
}

public static <T> ApiResponse<T> error(ErrorCode errorCode, String message) {
return new ApiResponse<>(errorCode.getHttpStatus(), message);
}

@Override
public String toString() {
return "ApiResponse{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package contest.collectingbox.global.exception;

import lombok.Getter;

@Getter
public class CollectingBoxException extends RuntimeException {
private final ErrorCode errorCode;

public CollectingBoxException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public ErrorCode errorCode() {
return errorCode;
}

public String message() {
return errorCode.getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;


@Getter
@AllArgsConstructor
public enum ErrorCode {
Expand All @@ -23,6 +22,7 @@ public enum ErrorCode {

// 임시
NOT_FOUND_TAG(NOT_FOUND, "해당 이름과 일치하는 수거함 태그가 없습니다.");

// 409

// 500
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
package contest.collectingbox.global.exception;



import contest.collectingbox.global.common.ApiResponse;
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.MissingServletRequestParameterException;
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 contest.collectingbox.global.exception.ErrorCode.*;
import static org.springframework.http.HttpStatus.BAD_REQUEST;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(CollectingBoxException.class)
public ResponseEntity<ErrorResponse> handleCollectingBoxException(CollectingBoxException e) {
ErrorResponse errorResponse = ErrorResponse.from(e.getErrorCode());
return ResponseEntity.status(errorResponse.getStatus()).body(errorResponse);
public ApiResponse<Object> handleCollectingBoxException(CollectingBoxException e) {
return ApiResponse.error(e.errorCode(), e.message());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> methodValidException(MethodArgumentNotValidException e) {
public ApiResponse<Object> methodValidException(MethodArgumentNotValidException e) {
String message = e.getBindingResult().getFieldError().getDefaultMessage();
ErrorResponse errorResponse = ErrorResponse.from(INVALID_BEAN, message);
return ResponseEntity.status(errorResponse.getStatus()).body(errorResponse);

return ApiResponse.error(INVALID_BEAN, message);
}

@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorResponse handleMissingParams(MissingServletRequestParameterException e) {
return ErrorResponse.from(MISSING_REQUEST_PARAM);
public ApiResponse<Object> handleMissingParams(MissingServletRequestParameterException e) {
return ApiResponse.error(MISSING_REQUEST_PARAM, MISSING_REQUEST_PARAM.getMessage());
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorResponse handleMissingParams(MethodArgumentTypeMismatchException e) {
return ErrorResponse.from(MISMATCH_REQUEST_PARAM);
public ApiResponse<Object> handleMissingParams(MethodArgumentTypeMismatchException e) {
return ApiResponse.error(MISMATCH_REQUEST_PARAM, MISSING_REQUEST_PARAM.getMessage());
}

}
Loading