Skip to content

Commit

Permalink
Merge pull request #38 from dnd-side-project/Fix/#36
Browse files Browse the repository at this point in the history
예외 반환 로직 수정
  • Loading branch information
chaewon-io authored Aug 11, 2024
2 parents 534270b + 38019c1 commit f153604
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/dnd/snappy/common/dto/ErrorResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.dnd.snappy.common.error.ErrorCodeInterface;
import org.springframework.http.ResponseEntity;

public record ErrorResponseDto(int status, String code, String message) {
public record ErrorResponseDto(String code, String message) {

public static ErrorResponseDto from(ErrorCodeInterface errorCode) {
return new ErrorResponseDto(errorCode.getStatus().value(), errorCode.getErrorResponseCode(), errorCode.getMessage());
return new ErrorResponseDto(errorCode.getErrorResponseCode(), errorCode.getMessage());
}

public static ResponseEntity<ErrorResponseDto> toResponseEntity(ErrorCodeInterface errorCode) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/dnd/snappy/common/dto/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package com.dnd.snappy.common.dto;

import com.dnd.snappy.common.error.ErrorCodeInterface;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public record ResponseDto<T>(
boolean success,
int status,
T data,
ErrorResponseDto error
@JsonInclude(JsonInclude.Include.NON_NULL) ErrorResponseDto error
) {

public static <T> ResponseEntity<ResponseDto<T>> ok(T data) {
ResponseDto<T> responseDto = new ResponseDto<>(true, data, null);
return ResponseEntity
.ok(responseDto);
.ok(new ResponseDto<>(HttpStatus.OK.value(), data, null));
}

public static <T> ResponseEntity<ResponseDto<?>> ok() {
ResponseDto<T> responseDto = new ResponseDto<>(true, null, null);
return ResponseEntity
.ok(responseDto);
.ok(new ResponseDto<>(HttpStatus.OK.value(), null, null));
}

public static <T> ResponseEntity<ResponseDto<T>> created(T data) {
ResponseDto<T> responseDto = new ResponseDto<>(true, data, null);
ResponseDto<T> responseDto = new ResponseDto<>(HttpStatus.CREATED.value(), data, null);
return ResponseEntity
.status(HttpStatus.CREATED)
.body(responseDto);
}

public static ResponseEntity<ResponseDto<?>> fail(ErrorCodeInterface errorCode) {
ResponseDto<?> responseDto = new ResponseDto<>(false, null, ErrorResponseDto.from(errorCode));
ResponseDto<?> responseDto = new ResponseDto<>(errorCode.getStatus().value(), null, ErrorResponseDto.from(errorCode));
return ResponseEntity
.status(errorCode.getStatus())
.body(responseDto);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -42,7 +43,7 @@ public ResponseEntity<ResponseDto<ParticipationResponse>> participateMeeting(
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie)
.body(new ResponseDto<>(
true,
HttpStatus.OK.value(),
new ParticipationResponse(response.participantId(), response.accessToken()),
null
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void findByMeetingLink() throws Exception {
parameterWithName("meetingLink").description("모임 링크")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.OBJECT).description("모임"),
fieldWithPath("data.meetingId").type(JsonFieldType.NUMBER).description("모임 id"),
fieldWithPath("data.name").type(JsonFieldType.STRING).description("모임 이름"),
Expand All @@ -76,8 +76,7 @@ void findByMeetingLink() throws Exception {
fieldWithPath("data.symbolColor").type(JsonFieldType.STRING).description("모임 상징 색"),
fieldWithPath("data.startDate").type(JsonFieldType.STRING).attributes(getDateTimeFormat()).description("모임 시작일"),
fieldWithPath("data.endDate").type(JsonFieldType.STRING).attributes(getDateTimeFormat()).description("모임 종료일"),
fieldWithPath("data.status").type(JsonFieldType.STRING).description("모임 링크 상태"),
fieldWithPath("error").type(JsonFieldType.NULL).description("에러")
fieldWithPath("data.status").type(JsonFieldType.STRING).description("모임 링크 상태")
)
)
);
Expand All @@ -97,10 +96,9 @@ void findByMeetingLink_notFound() throws Exception {
parameterWithName("meetingLink").description("모임 링크")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("모임"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("에러"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("상태코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).description("에러 메세지")
)
Expand Down Expand Up @@ -146,9 +144,8 @@ void validateMeetingPassword() throws Exception {
fieldWithPath("password").type(JsonFieldType.STRING).description("모임 비밀번호")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터"),
fieldWithPath("error").type(JsonFieldType.NULL).description("에러")
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터")
)
)
);
Expand Down Expand Up @@ -191,10 +188,9 @@ void validateMeetingPassword_invalidatePassword() throws Exception {
fieldWithPath("password").type(JsonFieldType.STRING).description("모임 비밀번호")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("에러"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("상태코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).description("에러 메세지")
)
Expand Down Expand Up @@ -242,9 +238,8 @@ void validateMeetingLeaderAuthKey() throws Exception {
fieldWithPath("leaderAuthKey").type(JsonFieldType.STRING).description("관리자 인증키")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터"),
fieldWithPath("error").type(JsonFieldType.NULL).description("에러")
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터")
)
)
);
Expand Down Expand Up @@ -289,10 +284,9 @@ void validateMeetingLeaderAuthKey_invalidatePassword() throws Exception {
fieldWithPath("leaderAuthKey").type(JsonFieldType.STRING).description("관리자 인증키")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("에러"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("상태코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).description("에러 메세지")
)
Expand Down Expand Up @@ -339,10 +333,9 @@ void validateMeetingLeaderAuthKey_invalidateLeaderAuthKey() throws Exception {
fieldWithPath("leaderAuthKey").type(JsonFieldType.STRING).description("관리자 인증키")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("데이터"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("에러"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("상태코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).description("에러 메세지")
)
Expand Down Expand Up @@ -383,10 +376,9 @@ void createMeeting() throws Exception {
partWithName("thumbnail").optional().description("모임 썸네일 (선택)")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.OBJECT).description("생성된 모임 정보"),
fieldWithPath("data.meetingLink").type(JsonFieldType.STRING).description("생성된 모임 링크"),
fieldWithPath("error").type(JsonFieldType.NULL).description("에러")
fieldWithPath("data.meetingLink").type(JsonFieldType.STRING).description("생성된 모임 링크")
)
)
);
Expand Down Expand Up @@ -425,10 +417,9 @@ void createMeeting_BAD_REQUEST_startDate() throws Exception {
partWithName("thumbnail").optional().description("모임 썸네일 (선택)")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("생성된 모임 정보 (실패 시 null)"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("오류 정보"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러 코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).optional().description("에러 메시지")
)
Expand Down Expand Up @@ -469,10 +460,9 @@ void createMeeting_BAD_REQUEST_endDate() throws Exception {
partWithName("thumbnail").optional().description("모임 썸네일 (선택)")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("생성된 모임 정보 (실패 시 null)"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("오류 정보"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러 코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).optional().description("에러 메시지")
)
Expand Down Expand Up @@ -509,10 +499,9 @@ void createMeeting_VALIDATION_ERROR() throws Exception {
partWithName("thumbnail").optional().description("모임 썸네일 (선택)")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("요청 성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("응답 데이터 (실패 시 null)"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("에러 정보"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러 코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).optional().description("에러 메시지")
)
Expand Down Expand Up @@ -550,9 +539,8 @@ void getShareableMeetingLink_Success() throws Exception {
restDocs.document(
pathParameters(parameterWithName("meetingId").description("모임 ID")),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("data.meetingLink").type(JsonFieldType.STRING).description("모임 링크"),
fieldWithPath("error").type(JsonFieldType.NULL).description("에러")
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data.meetingLink").type(JsonFieldType.STRING).description("모임 링크")
)
)
);
Expand All @@ -575,10 +563,9 @@ void findByMeetingIdOrThrow_MEETING_NOT_FOUND() throws Exception {
parameterWithName("meetingId").description("모임 ID")
),
responseFields(
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("status").type(JsonFieldType.NUMBER).description("HTTP 상태 코드"),
fieldWithPath("data").type(JsonFieldType.NULL).description("공유 가능한 모임 링크"),
fieldWithPath("error").type(JsonFieldType.OBJECT).description("에러"),
fieldWithPath("error.status").type(JsonFieldType.NUMBER).description("상태코드"),
fieldWithPath("error.code").type(JsonFieldType.STRING).description("에러코드"),
fieldWithPath("error.message").type(JsonFieldType.STRING).description("에러 메세지")
)
Expand Down
Loading

0 comments on commit f153604

Please sign in to comment.