Skip to content

Commit

Permalink
[10] failResponse , success response 를 분리
Browse files Browse the repository at this point in the history
- 성공시 불필요한 code, message 제거
  • Loading branch information
ohsuha committed Sep 1, 2024
1 parent 3b5db25 commit 8175f94
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.commerce_site.common.exception;

import org.example.commerce_site.common.response.CommonResponse;
import org.example.commerce_site.common.response.ApiFailResponse;
import org.example.commerce_site.common.response.ApiSuccessResponse;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.ResponseEntity;
Expand All @@ -22,26 +23,26 @@ public class CommonExceptionHandler {
@ExceptionHandler({MissingServletRequestParameterException.class, ServletRequestBindingException.class,
TypeMismatchException.class, HttpMessageNotReadableException.class, MissingServletRequestPartException.class,
HttpMessageNotReadableException.class, MethodArgumentNotValidException.class})
protected ResponseEntity<CommonResponse> handleBadRequestException(final Exception e) {
protected ResponseEntity<ApiSuccessResponse> handleBadRequestException(final Exception e) {
ErrorCode errorCode = ErrorCode.BAD_REQUEST;
log.warn("[BadRequestException] error code : {}, error message : {}", errorCode,
NestedExceptionUtils.getMostSpecificCause(e).getMessage());
return CommonResponse.fail(errorCode);
return ApiFailResponse.fail(errorCode);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<CommonResponse> handleHttpRequestMethodNotSupportedException(
protected ResponseEntity<ApiSuccessResponse> handleHttpRequestMethodNotSupportedException(
final HttpRequestMethodNotSupportedException e) {
log.error("[HttpRequestMethodNotSupportedException]", e);
ErrorCode errorCode = ErrorCode.METHOD_NOT_ALLOWED;
return CommonResponse.fail(errorCode);
return ApiFailResponse.fail(errorCode);
}

@ExceptionHandler(CustomException.class)
protected ResponseEntity<CommonResponse> handleCustomException(CustomException e) {
protected ResponseEntity<ApiSuccessResponse> handleCustomException(CustomException e) {
ErrorCode errorCode = e.getErrorCode();
log.warn("[CustomException] error code : {}, error message : {}", errorCode,
NestedExceptionUtils.getMostSpecificCause(e).getMessage());
return CommonResponse.fail(errorCode);
return ApiFailResponse.fail(errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,17 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommonResponse {
private static final String MESSAGE_SUCCESS = "SUCCESS";
public class ApiFailResponse {
private int code;
private String message;

public static <T> CommonResponse.CommonData<T> success(T data) {
return (CommonResponse.CommonData<T>)CommonData.builder()
.message(MESSAGE_SUCCESS)
.data(data)
.build();
}

//for void api
public static CommonResponse success() {
return CommonResponse.builder()
.message(MESSAGE_SUCCESS)
.build();
}

public static <T> ResponseEntity<T> fail(ErrorCode errorCode) {
return (ResponseEntity<T>)ResponseEntity
.status(errorCode.getHttpStatus())
.body(CommonResponse.builder()
.body(ApiFailResponse.builder()
.code(errorCode.getCode())
.message(errorCode.getMessage())
.build()
);
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class CommonData<T> {
private int code;
private String message;
private T data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.commerce_site.common.response;

import org.example.commerce_site.common.exception.ErrorCode;
import org.springframework.http.ResponseEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ApiSuccessResponse<T> {
private T data;

public static <T> ApiSuccessResponse<T> success(T data) {
return (ApiSuccessResponse<T>)ApiSuccessResponse.builder()
.data(data)
.build();
}

//for void api
public static ApiSuccessResponse success() {
return ApiSuccessResponse.builder()
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.commerce_site.representation.partner;

import org.example.commerce_site.application.partner.PartnerService;
import org.example.commerce_site.common.response.CommonResponse;
import org.example.commerce_site.common.response.ApiSuccessResponse;
import org.example.commerce_site.representation.partner.request.PartnerRequest;
import org.example.commerce_site.representation.partner.response.PartnerResponse;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -21,9 +21,9 @@ public class PartnerController {
private final PartnerService partnerService;

@PostMapping()
public CommonResponse.CommonData<PartnerResponse.Create> createPartner(
public ApiSuccessResponse<PartnerResponse.Create> createPartner(
@Valid @RequestBody PartnerRequest.Create request) {
return CommonResponse.success(
return ApiSuccessResponse.success(
PartnerResponse.Create.of(partnerService.create(PartnerRequest.Create.toDTO(request))));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.commerce_site.representation.product;

import org.example.commerce_site.application.product.ProductFacade;
import org.example.commerce_site.common.response.CommonResponse;
import org.example.commerce_site.common.response.ApiSuccessResponse;
import org.example.commerce_site.representation.product.request.ProductRequest;
import org.example.commerce_site.representation.product.response.ProductResponse;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -19,10 +19,10 @@ public class ProductController {
private final ProductFacade productFacade;

@PostMapping()
public CommonResponse.CommonData<ProductResponse.Create> createProduct(
public ApiSuccessResponse<ProductResponse.Create> createProduct(
@Valid @RequestBody ProductRequest.Create request) {
//TODO Partner 회원 외에는 접근할 수 없는 API 임
return CommonResponse.success(
return ApiSuccessResponse.success(
ProductResponse.Create.of(productFacade.createProduct(ProductRequest.Create.toDTO(request))));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.commerce_site.representation.user;

import org.example.commerce_site.application.user.UserService;
import org.example.commerce_site.common.response.CommonResponse;
import org.example.commerce_site.common.response.ApiSuccessResponse;
import org.example.commerce_site.representation.user.request.UserRequest;
import org.example.commerce_site.representation.user.response.UserResponse;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -21,7 +21,7 @@ public class UserController {
private final UserService userService;

@PostMapping()
public CommonResponse.CommonData<UserResponse.Create> createUser(@Valid @RequestBody UserRequest.Create request) {
return CommonResponse.success(UserResponse.Create.of(userService.create(UserRequest.Create.toDTO(request))));
public ApiSuccessResponse<UserResponse.Create> createUser(@Valid @RequestBody UserRequest.Create request) {
return ApiSuccessResponse.success(UserResponse.Create.of(userService.create(UserRequest.Create.toDTO(request))));
}
}

0 comments on commit 8175f94

Please sign in to comment.