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

[FIX] API 형식에 맞게 변경 #47

Merged
merged 2 commits into from
Oct 8, 2023
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
Expand Up @@ -19,7 +19,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BindException.class)
protected ApiResponse<FailureBody> handleBindException(BindException e) {
log.error("handleBindException", e);
return ApiResponseGenerator.fail(e.getMessage(), HttpStatus.BAD_REQUEST);
return ApiResponseGenerator.fail(e.getBindingResult(), HttpStatus.BAD_REQUEST);
}

/** 주로 @RequestParam enum으로 binding 못했을 경우 발생 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ApiResponseBody {
@AllArgsConstructor
public static class FailureBody implements Serializable {

private String code;
private String status;
private String message;
}

Expand All @@ -18,6 +18,6 @@ public static class FailureBody implements Serializable {
public static class SuccessBody<D> implements Serializable {
private D data;
private String message;
private String code;
private String status;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.kakao.golajuma.common.support.respnose;

import java.util.List;
import lombok.experimental.UtilityClass;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

@UtilityClass
public class ApiResponseGenerator {
Expand Down Expand Up @@ -52,6 +55,34 @@ public static ApiResponse<ApiResponseBody.FailureBody> fail(
return new ApiResponse<>(new ApiResponseBody.FailureBody(code, message), status);
}

public static ApiResponse<ApiResponseBody.FailureBody> fail(
BindingResult bindingResult, final HttpStatus status) {
return new ApiResponse<>(
new ApiResponseBody.FailureBody(
String.valueOf(status.value()), createErrorMessage(bindingResult)),
status);
}

private static String createErrorMessage(BindingResult bindingResult) {
StringBuilder sb = new StringBuilder();
boolean isFirst = true;

List<FieldError> fieldErrors = bindingResult.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
if (!isFirst) {
sb.append(", ");
} else {
isFirst = false;
}
sb.append("[");
sb.append(fieldError.getField());
sb.append("] ");
sb.append(fieldError.getDefaultMessage());
}

return sb.toString();
}

private MultiValueMap<String, String> setCookie(String cookieValue) {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("Set-Cookie", cookieValue);
Expand Down
Loading