refactor(order, store, user): 전역 에러 핸들링 리팩토링
- Result 클래스를 통해 성공, 에러 반환 처리 - ErrorEnum 삭제
This commit is contained in:
@@ -39,7 +39,7 @@ public class OrderController {
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Result<>("OK", orderMainResponses));
|
||||
.body(Result.createSuccessResult(orderMainResponses));
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.justpickup.orderservice.global.client.exception;
|
||||
|
||||
import com.justpickup.orderservice.global.exception.CustomException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class FeignClientException extends CustomException {
|
||||
|
||||
public FeignClientException(HttpStatus status, String message) {
|
||||
super(status, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.justpickup.orderservice.global.client.exception;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.justpickup.orderservice.global.dto.Result;
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class FeignClientExceptionErrorDecoder implements ErrorDecoder {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
String message = null;
|
||||
if (response.body() != null) {
|
||||
try {
|
||||
Result result = objectMapper.readValue(response.body().asInputStream(), Result.class);
|
||||
message = result.getMessage();
|
||||
} catch (IOException e) {
|
||||
String catchErrorMessage = "Error Deserializing response body from failed feign request response.";
|
||||
log.warn(methodKey + catchErrorMessage, e);
|
||||
|
||||
return new FeignClientException(HttpStatus.INTERNAL_SERVER_ERROR, "고객센터로 문의해주세요.");
|
||||
}
|
||||
}
|
||||
|
||||
return new FeignClientException(HttpStatus.INTERNAL_SERVER_ERROR, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.justpickup.orderservice.global.dto;
|
||||
|
||||
public enum Code {
|
||||
SUCCESS, ERROR
|
||||
}
|
||||
@@ -1,11 +1,36 @@
|
||||
package com.justpickup.orderservice.global.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
@Data @NoArgsConstructor
|
||||
public class Result<T> {
|
||||
private Code code;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
@Builder
|
||||
public Result(Code code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static Result createErrorResult(String message) {
|
||||
return Result.builder()
|
||||
.code(Code.ERROR)
|
||||
.message(message)
|
||||
.data(null)
|
||||
.build();
|
||||
}
|
||||
|
||||
// 해당 <T> 는 클래스의 T와 다름
|
||||
public static <T> Result createSuccessResult(T data) {
|
||||
return Result.builder()
|
||||
.code(Code.SUCCESS)
|
||||
.message("")
|
||||
.data(data)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.justpickup.orderservice.global.exception;
|
||||
|
||||
import com.justpickup.orderservice.global.dto.Result;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Getter
|
||||
public class CustomException extends RuntimeException {
|
||||
|
||||
private ErrorEnum errorEnum;
|
||||
private HttpStatus status;
|
||||
private Result errorResult;
|
||||
|
||||
protected CustomException(ErrorEnum errorEnum) {
|
||||
super(errorEnum.getMessage());
|
||||
this.errorEnum = errorEnum;
|
||||
protected CustomException(HttpStatus status, String message) {
|
||||
this.status = status;
|
||||
this.errorResult = Result.createErrorResult(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.justpickup.orderservice.global.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum ErrorEnum {
|
||||
|
||||
NOT_EXIST_ORDER(HttpStatus.CONFLICT, "존재하지 않은 주문입니다.");
|
||||
|
||||
private final HttpStatus httpStatus;
|
||||
private final String message;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.justpickup.orderservice.global.exception;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.justpickup.orderservice.global.dto.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -17,30 +15,25 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(CustomException.class)
|
||||
public ResponseEntity<ErrorBody> customExceptionHandler(CustomException ce) {
|
||||
ErrorEnum errorEnum = ce.getErrorEnum();
|
||||
public ResponseEntity customExceptionHandler(CustomException ce) {
|
||||
HttpStatus status = ce.getStatus();
|
||||
Result errorResult = ce.getErrorResult();
|
||||
|
||||
log.warn("##################################################");
|
||||
log.warn("## CustomException = {}", errorEnum);
|
||||
log.warn("##################################################");
|
||||
|
||||
HttpStatus errorHttpStatus = errorEnum.getHttpStatus();
|
||||
|
||||
return ResponseEntity.status(errorHttpStatus)
|
||||
.body(new ErrorBody(errorEnum.getMessage(), errorHttpStatus.getReasonPhrase()));
|
||||
return ResponseEntity.status(status)
|
||||
.body(errorResult);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BindException.class)
|
||||
public ResponseEntity<ErrorBody> bindExceptionHandler(BindException exception) {
|
||||
public ResponseEntity bindExceptionHandler(BindException exception) {
|
||||
return getValidationErrorBody(exception);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ErrorBody> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
public ResponseEntity methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
return getValidationErrorBody(exception);
|
||||
}
|
||||
|
||||
private ResponseEntity<ErrorBody> getValidationErrorBody(BindException exception) {
|
||||
private ResponseEntity getValidationErrorBody(BindException exception) {
|
||||
BindingResult bindingResult = exception.getBindingResult();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
@@ -55,19 +48,8 @@ public class GlobalExceptionHandler {
|
||||
builder.append("]");
|
||||
});
|
||||
|
||||
ErrorBody errorBody = new ErrorBody(builder.toString(), HttpStatus.BAD_REQUEST.getReasonPhrase());
|
||||
|
||||
log.warn("##################################################");
|
||||
log.warn("## getValidationErrorBody = {}", errorBody);
|
||||
log.warn("##################################################");
|
||||
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
.body(errorBody);
|
||||
.body(Result.createErrorResult(builder.toString()));
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
static class ErrorBody {
|
||||
private String message;
|
||||
private String httpStatus;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.justpickup.storeservice.domain.item.web;
|
||||
|
||||
import com.justpickup.storeservice.domain.item.dto.ItemDto;
|
||||
import com.justpickup.storeservice.domain.item.service.ItemService;
|
||||
import com.justpickup.storeservice.global.dto.Result;
|
||||
import com.justpickup.storeservice.global.entity.Yn;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -27,13 +28,7 @@ public class ItemController {
|
||||
|
||||
GetItemResponse getItemResponse = new GetItemResponse(itemByItemId);
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Result<>("OK", getItemResponse));
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
static class Result<T> {
|
||||
private String message;
|
||||
private T data;
|
||||
.body(Result.createSuccessResult(getItemResponse));
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.justpickup.storeservice.global.dto;
|
||||
|
||||
public enum Code {
|
||||
SUCCESS, ERROR
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.justpickup.storeservice.global.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data @NoArgsConstructor
|
||||
public class Result<T> {
|
||||
private Code code;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
@Builder
|
||||
public Result(Code code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static Result createErrorResult(String message) {
|
||||
return Result.builder()
|
||||
.code(Code.ERROR)
|
||||
.message(message)
|
||||
.data(null)
|
||||
.build();
|
||||
}
|
||||
|
||||
// 해당 <T> 는 클래스의 T와 다름
|
||||
public static <T> Result createSuccessResult(T data) {
|
||||
return Result.builder()
|
||||
.code(Code.SUCCESS)
|
||||
.message("")
|
||||
.data(data)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.justpickup.storeservice.global.exception;
|
||||
|
||||
import com.justpickup.storeservice.global.dto.Result;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Getter
|
||||
public class CustomException extends RuntimeException {
|
||||
|
||||
private ErrorEnum errorEnum;
|
||||
private HttpStatus status;
|
||||
private Result errorResult;
|
||||
|
||||
protected CustomException(ErrorEnum errorEnum) {
|
||||
super(errorEnum.getMessage());
|
||||
this.errorEnum = errorEnum;
|
||||
protected CustomException(HttpStatus status, String message) {
|
||||
this.status = status;
|
||||
this.errorResult = Result.createErrorResult(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.justpickup.storeservice.global.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum ErrorEnum {
|
||||
|
||||
NOT_EXIST_ITEM(HttpStatus.CONFLICT, "존재하지 않은 상품입니다.");
|
||||
|
||||
private final HttpStatus httpStatus;
|
||||
private final String message;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.justpickup.storeservice.global.exception;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.justpickup.storeservice.global.dto.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -17,30 +15,25 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(CustomException.class)
|
||||
public ResponseEntity<ErrorBody> customExceptionHandler(CustomException ce) {
|
||||
ErrorEnum errorEnum = ce.getErrorEnum();
|
||||
public ResponseEntity customExceptionHandler(CustomException ce) {
|
||||
HttpStatus status = ce.getStatus();
|
||||
Result errorResult = ce.getErrorResult();
|
||||
|
||||
log.warn("##################################################");
|
||||
log.warn("## CustomException = {}", errorEnum);
|
||||
log.warn("##################################################");
|
||||
|
||||
HttpStatus errorHttpStatus = errorEnum.getHttpStatus();
|
||||
|
||||
return ResponseEntity.status(errorHttpStatus)
|
||||
.body(new ErrorBody(errorEnum.getMessage(), errorHttpStatus.getReasonPhrase()));
|
||||
return ResponseEntity.status(status)
|
||||
.body(errorResult);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BindException.class)
|
||||
public ResponseEntity<ErrorBody> bindExceptionHandler(BindException exception) {
|
||||
public ResponseEntity bindExceptionHandler(BindException exception) {
|
||||
return getValidationErrorBody(exception);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ErrorBody> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
public ResponseEntity methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
return getValidationErrorBody(exception);
|
||||
}
|
||||
|
||||
private ResponseEntity<ErrorBody> getValidationErrorBody(BindException exception) {
|
||||
private ResponseEntity getValidationErrorBody(BindException exception) {
|
||||
BindingResult bindingResult = exception.getBindingResult();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
@@ -55,20 +48,8 @@ public class GlobalExceptionHandler {
|
||||
builder.append("]");
|
||||
});
|
||||
|
||||
ErrorBody errorBody = new ErrorBody(builder.toString(), HttpStatus.BAD_REQUEST.getReasonPhrase());
|
||||
|
||||
log.warn("##################################################");
|
||||
log.warn("## getValidationErrorBody = {}", errorBody);
|
||||
log.warn("##################################################");
|
||||
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
.body(errorBody);
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
static class ErrorBody {
|
||||
private String message;
|
||||
private String httpStatus;
|
||||
.body(Result.createErrorResult(builder.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.justpickup.userservice.domain.user.exception;
|
||||
|
||||
import com.justpickup.userservice.global.exception.CustomException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class NotExistUserException extends CustomException {
|
||||
|
||||
public NotExistUserException(String message) {
|
||||
super(HttpStatus.CONFLICT, message);
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,13 @@ package com.justpickup.userservice.domain.user.service;
|
||||
|
||||
import com.justpickup.userservice.domain.user.dto.CustomerDto;
|
||||
import com.justpickup.userservice.domain.user.entity.Customer;
|
||||
import com.justpickup.userservice.domain.user.exception.NotExistUserException;
|
||||
import com.justpickup.userservice.domain.user.repository.CustomerRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
@@ -21,7 +20,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public CustomerDto findCustomerByUserId(Long userId) {
|
||||
Customer customer = customerRepository.findById(userId)
|
||||
.orElseThrow(NoSuchElementException::new);
|
||||
.orElseThrow(() -> new NotExistUserException("존재하지 않는 사용자 입니다."));
|
||||
|
||||
return new CustomerDto(customer);
|
||||
}
|
||||
|
||||
@@ -26,8 +26,10 @@ public class UserController {
|
||||
|
||||
CustomerDto customerDto = userService.findCustomerByUserId(userId);
|
||||
|
||||
GetCustomerResponse getCustomerResponse = new GetCustomerResponse(customerDto);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Result<>("OK", new GetCustomerResponse(customerDto)));
|
||||
.body(Result.createSuccessResult(getCustomerResponse));
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.justpickup.userservice.global.dto;
|
||||
|
||||
public enum Code {
|
||||
SUCCESS, ERROR
|
||||
}
|
||||
@@ -1,11 +1,36 @@
|
||||
package com.justpickup.userservice.global.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
@Data @NoArgsConstructor
|
||||
public class Result<T> {
|
||||
public String message;
|
||||
public T data;
|
||||
private Code code;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
@Builder
|
||||
public Result(Code code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static Result createErrorResult(String message) {
|
||||
return Result.builder()
|
||||
.code(Code.ERROR)
|
||||
.message(message)
|
||||
.data(null)
|
||||
.build();
|
||||
}
|
||||
|
||||
// 해당 <T> 는 클래스의 T와 다름
|
||||
public static <T> Result createSuccessResult(T data) {
|
||||
return Result.builder()
|
||||
.code(Code.SUCCESS)
|
||||
.message("")
|
||||
.data(data)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.justpickup.userservice.global.exception;
|
||||
|
||||
import com.justpickup.userservice.global.dto.Result;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Getter
|
||||
public class CustomException extends RuntimeException {
|
||||
|
||||
private ErrorEnum errorEnum;
|
||||
private HttpStatus status;
|
||||
private Result errorResult;
|
||||
|
||||
protected CustomException(ErrorEnum errorEnum) {
|
||||
super(errorEnum.getMessage());
|
||||
this.errorEnum = errorEnum;
|
||||
protected CustomException(HttpStatus status, String message) {
|
||||
this.status = status;
|
||||
this.errorResult = Result.createErrorResult(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.justpickup.userservice.global.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum ErrorEnum {
|
||||
|
||||
NOT_EXIST_USER(HttpStatus.CONFLICT, "존재하지 않은 사용자입니다.");
|
||||
|
||||
private final HttpStatus httpStatus;
|
||||
private final String message;
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
package com.justpickup.userservice.global.exception;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.justpickup.userservice.global.dto.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
@@ -18,30 +15,25 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(CustomException.class)
|
||||
public ResponseEntity<ErrorBody> customExceptionHandler(CustomException ce) {
|
||||
ErrorEnum errorEnum = ce.getErrorEnum();
|
||||
public ResponseEntity customExceptionHandler(CustomException ce) {
|
||||
HttpStatus status = ce.getStatus();
|
||||
Result errorResult = ce.getErrorResult();
|
||||
|
||||
log.warn("##################################################");
|
||||
log.warn("## CustomException = {}", errorEnum);
|
||||
log.warn("##################################################");
|
||||
|
||||
HttpStatus errorHttpStatus = errorEnum.getHttpStatus();
|
||||
|
||||
return ResponseEntity.status(errorHttpStatus)
|
||||
.body(new ErrorBody(errorEnum.getMessage(), errorHttpStatus.getReasonPhrase()));
|
||||
return ResponseEntity.status(status)
|
||||
.body(errorResult);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BindException.class)
|
||||
public ResponseEntity<ErrorBody> bindExceptionHandler(BindException exception) {
|
||||
public ResponseEntity bindExceptionHandler(BindException exception) {
|
||||
return getValidationErrorBody(exception);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ErrorBody> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
public ResponseEntity methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
return getValidationErrorBody(exception);
|
||||
}
|
||||
|
||||
private ResponseEntity<ErrorBody> getValidationErrorBody(BindException exception) {
|
||||
private ResponseEntity getValidationErrorBody(BindException exception) {
|
||||
BindingResult bindingResult = exception.getBindingResult();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
@@ -56,19 +48,8 @@ public class GlobalExceptionHandler {
|
||||
builder.append("]");
|
||||
});
|
||||
|
||||
ErrorBody errorBody = new ErrorBody(builder.toString(), HttpStatus.BAD_REQUEST.getReasonPhrase());
|
||||
|
||||
log.warn("##################################################");
|
||||
log.warn("## getValidationErrorBody = {}", errorBody);
|
||||
log.warn("##################################################");
|
||||
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
.body(errorBody);
|
||||
.body(Result.createErrorResult(builder.toString()));
|
||||
}
|
||||
|
||||
@Data @NoArgsConstructor @AllArgsConstructor
|
||||
static class ErrorBody {
|
||||
private String message;
|
||||
private String httpStatus;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user