-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
src/main/java/com/noplanb/global/payload/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |