Skip to content

Commit

Permalink
[FEAT] ApiResponse 설정, Error코드 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
lsn5963 committed Jul 28, 2024
1 parent e6dab19 commit 46fe695
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/noplanb/global/payload/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/noplanb/global/payload/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
77 changes: 77 additions & 0 deletions src/main/java/com/noplanb/global/payload/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -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<CustomFieldError> customFieldErrors = new ArrayList<>();

public ErrorResponse() {}

@Builder
public ErrorResponse(String code, int status, String message, String clazz, List<FieldError> fieldErrors){
this.code = code;
this.status = status;
this.message = message;
this.clazz = clazz;
setFieldErrors(fieldErrors);
}

public void setFieldErrors(List<FieldError> 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;
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/noplanb/global/payload/Message.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 46fe695

Please sign in to comment.