From 46fe69545fe86ff332edaddb8de25d2cf0d76c0f Mon Sep 17 00:00:00 2001 From: lsn5963 <77337977+lsn5963@users.noreply.github.com> Date: Mon, 29 Jul 2024 02:25:19 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20ApiResponse=20=EC=84=A4=EC=A0=95,=20Er?= =?UTF-8?q?ror=EC=BD=94=EB=93=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../noplanb/global/payload/ApiResponse.java | 32 ++++++++ .../com/noplanb/global/payload/ErrorCode.java | 24 ++++++ .../noplanb/global/payload/ErrorResponse.java | 77 +++++++++++++++++++ .../com/noplanb/global/payload/Message.java | 21 +++++ 4 files changed, 154 insertions(+) create mode 100644 src/main/java/com/noplanb/global/payload/ApiResponse.java create mode 100644 src/main/java/com/noplanb/global/payload/ErrorCode.java create mode 100644 src/main/java/com/noplanb/global/payload/ErrorResponse.java create mode 100644 src/main/java/com/noplanb/global/payload/Message.java diff --git a/src/main/java/com/noplanb/global/payload/ApiResponse.java b/src/main/java/com/noplanb/global/payload/ApiResponse.java new file mode 100644 index 0000000..a58bc88 --- /dev/null +++ b/src/main/java/com/noplanb/global/payload/ApiResponse.java @@ -0,0 +1,32 @@ +package com.noplanb.global.payload; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; +import lombok.ToString; + +@ToString +@Data +public class ApiResponse { + + @Schema( type = "boolean", example = "true", description="올바르게 로직을 처리했으면 True, 아니면 False를 반환합니다.") + private boolean check; + + @Schema( type = "object", example = "information", description="restful의 정보를 감싸 표현합니다. object형식으로 표현합니다.") + private Object information; + + public ApiResponse(){}; + + @Builder + public ApiResponse(boolean check, Object information) { + this.check = check; + this.information = information; + } + + public static ApiResponse toApiResponse(Object response) { + return ApiResponse.builder() + .check(true) + .information(response) + .build(); + } +} diff --git a/src/main/java/com/noplanb/global/payload/ErrorCode.java b/src/main/java/com/noplanb/global/payload/ErrorCode.java new file mode 100644 index 0000000..91ab2cc --- /dev/null +++ b/src/main/java/com/noplanb/global/payload/ErrorCode.java @@ -0,0 +1,24 @@ +package com.noplanb.global.payload; + +import lombok.Getter; + +@Getter +public enum ErrorCode { + + INVALID_PARAMETER(400, null, "잘못된 요청 데이터 입니다."), + INVALID_REPRESENTATION(400, null, "잘못된 표현 입니다."), + INVALID_FILE_PATH(400, null, "잘못된 파일 경로 입니다."), + INVALID_OPTIONAL_ISPRESENT(400, null, "해당 값이 존재하지 않습니다."), + INVALID_CHECK(400, null, "해당 값이 유효하지 않습니다."), + INVALID_AUTHENTICATION(400, null, "잘못된 인증입니다."); + + private final String code; + private final String message; + private final int status; + + ErrorCode(final int status, final String code, final String message) { + this.status = status; + this.message = message; + this.code = code; + } +} diff --git a/src/main/java/com/noplanb/global/payload/ErrorResponse.java b/src/main/java/com/noplanb/global/payload/ErrorResponse.java new file mode 100644 index 0000000..67e315f --- /dev/null +++ b/src/main/java/com/noplanb/global/payload/ErrorResponse.java @@ -0,0 +1,77 @@ +package com.noplanb.global.payload; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Data; +import org.springframework.validation.FieldError; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@Data +public class ErrorResponse { + private LocalDateTime timestamp = LocalDateTime.now(); + + private String message; + + private String code; + + @JsonProperty("class") + private String clazz; + + private int status; + + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonProperty("errors") + private List customFieldErrors = new ArrayList<>(); + + public ErrorResponse() {} + + @Builder + public ErrorResponse(String code, int status, String message, String clazz, List fieldErrors){ + this.code = code; + this.status = status; + this.message = message; + this.clazz = clazz; + setFieldErrors(fieldErrors); + } + + public void setFieldErrors(List fieldErrors) { + if(fieldErrors != null){ + fieldErrors.forEach(error -> { + customFieldErrors.add(new CustomFieldError( + error.getField(), + error.getRejectedValue(), + error.getDefaultMessage() + )); + }); + } + } + + public static class CustomFieldError { + + private String field; + private Object value; + private String reason; + + public CustomFieldError(String field, Object value, String reason) { + this.field = field; + this.value = value; + this.reason = reason; + } + + public String getField() { + return field; + } + + public Object getValue() { + return value; + } + + public String getReason() { + return reason; + } + } +} diff --git a/src/main/java/com/noplanb/global/payload/Message.java b/src/main/java/com/noplanb/global/payload/Message.java new file mode 100644 index 0000000..74fdc9a --- /dev/null +++ b/src/main/java/com/noplanb/global/payload/Message.java @@ -0,0 +1,21 @@ +package com.noplanb.global.payload; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; +import lombok.ToString; + +@ToString +@Data +public class Message { + + @Schema( type = "string", example = "메시지 문구를 출력합니다.", description="메시지 입니다.") + private String message; + + public Message(){}; + + @Builder + public Message(String message) { + this.message = message; + } +}