Skip to content

Commit

Permalink
[#86] 대략적인 apiResponse 객체 구조 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseul106 committed Dec 2, 2023
1 parent 5eaa814 commit 01347c7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.sopt.makers.crew.main.common.advice;

import java.util.Objects;
import org.sopt.makers.crew.main.common.exception.BaseException;
import org.sopt.makers.crew.main.common.response.ApiResponse;
import org.sopt.makers.crew.main.common.response.ErrorStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
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.RestControllerAdvice;
Expand All @@ -18,27 +15,19 @@ public class ControllerExceptionAdvice {
@ExceptionHandler(BaseException.class)
public ResponseEntity<ApiResponse> handleGlobalException(BaseException ex) {
return ResponseEntity.status(ex.getStatusCode())
.body(ApiResponse.fail(ex.getStatusCode(), ex.getResponseMessage()));
.body(ApiResponse.fail(ex.getErrorCode()));
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<ApiResponse> handleMissingParameter(MissingServletRequestParameterException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.fail(HttpStatus.BAD_REQUEST.value(),
ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getMessage()));
.body(ApiResponse.fail(ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getErrorCode()));
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ApiResponse> handleIllegalArgument(IllegalArgumentException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.fail(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ApiResponse> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
FieldError fieldError = Objects.requireNonNull(e.getFieldError());
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.fail(HttpStatus.BAD_REQUEST.value(),String.format("%s. (%s)", fieldError.getDefaultMessage(), fieldError.getField())));
.body(ApiResponse.fail(ex.getMessage()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BaseException extends RuntimeException {

HttpStatus statusCode;
String responseMessage;
HttpStatus httpStatus;
String errorCode;

public BaseException(HttpStatus statusCode) {
public BaseException(HttpStatus httpStatus) {
super();
this.statusCode = statusCode;
this.httpStatus = httpStatus;
}

public BaseException(HttpStatus statusCode, String responseMessage) {
super(responseMessage);
this.statusCode = statusCode;
this.responseMessage = responseMessage;
public BaseException(HttpStatus httpStatus, String errorCode) {
super(errorCode);
this.httpStatus = httpStatus;
this.errorCode = errorCode;
}

public int getStatusCode() {
return this.statusCode.value();
return this.httpStatus.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse<T> {

private final int status;
private final boolean success;
private final String message;
private final String errorCode;
private T data;

public static ApiResponse success(HttpStatus status, Object data) {
public static ApiResponse success(Object data) {
return ApiResponse.builder()
.status(status.value())
.success(true)
.data(data)
.build();
}


public static ApiResponse fail(int status, String message) {
public static ApiResponse fail(String errorCode) {
return ApiResponse.builder()
.status(status)
.success(false)
.message(message)
.errorCode(errorCode)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ public enum ErrorStatus {
/**
* 400 BAD_REQUEST
*/
VALIDATION_EXCEPTION("잘못된 요청입니다."),
VALIDATION_EXCEPTION("CR-001"), // errorCode는 예시, 추후 변경 예정 -> 잘못된 요청입니다.
VALIDATION_REQUEST_MISSING_EXCEPTION("요청값이 입력되지 않았습니다."),

/**
* 500 SERVER_ERROR
*/
INTERNAL_SERVER_ERROR("예상치 못한 서버 에러가 발생했습니다."),
BAD_GATEWAY_EXCEPTION("일시적인 에러가 발생하였습니다.\n잠시 후 다시 시도해주세요!");
INTERNAL_SERVER_ERROR("예상치 못한 서버 에러가 발생했습니다.");

private final String message;
private final String errorCode;

}

0 comments on commit 01347c7

Please sign in to comment.