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

선택지 통계 조회 기능 구현 #83

Merged
merged 12 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,9 @@
package com.votogether.exception;

public class BadRequestException extends BaseException {

public BadRequestException(final ExceptionType exceptionType) {
super(exceptionType);
}

}
14 changes: 14 additions & 0 deletions backend/src/main/java/com/votogether/exception/BaseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.votogether.exception;

import lombok.Getter;

@Getter
public class BaseException extends RuntimeException {

private final ExceptionType exceptionType;

public BaseException(final ExceptionType exceptionType) {
this.exceptionType = exceptionType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.votogether.exception;

public record ExceptionResponse(int code, String message) {

public static ExceptionResponse from(final BaseException e) {
final ExceptionType exceptionType = e.getExceptionType();
return new ExceptionResponse(exceptionType.getCode(), exceptionType.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.votogether.exception;

public interface ExceptionType {

int getCode();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q : 에러코드는 주로 어쩔때 사용하고 에러코드를 지정하는 기준은 무엇인가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후에 에러를 로그로 남기게 되었을 때, 여러 로그 중 에러 메시지로 에러를 찾기보다는 특정 에러 코드값으로 에러를 찾는 것이 더 용이하다고 판단되어 에러 코드도 추가해 보았습니다!
에러 코드를 지정하는 기준은 아직 정하지 않아서 다 같이 얘기해보면 좋을 것 같아요 :)


String getMessage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.votogether.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleException(final Exception e) {
return ResponseEntity.internalServerError()
.body(new ExceptionResponse(-9999, "알 수 없는 서버 에러가 발생했습니다."));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q : 전 알 수 없는 에러의 경우 500 을 보내주었었는데, -9999로 하신 이유가 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP 상태코드는 internalServerError를 통해 500으로 전송되고 있습니다!
해당 코드에서 -9999는 저희가 임의로 설정한 코드값으로 이 부분에 대해서도 다같이 얘기해보면 좋을 것 같아요 😀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 제가 잘못 파악했었네요--ㅋ

}

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleBadRequestException(final BadRequestException e) {
return ResponseEntity.badRequest()
.body(ExceptionResponse.from(e));
}

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleNotFoundException(final NotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ExceptionResponse.from(e));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.votogether.exception;

public class NotFoundException extends BaseException {

public NotFoundException(final ExceptionType exceptionType) {
super(exceptionType);
}
Comment on lines +3 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: NotFoundExcption는 항상 핸들러에서 NOT_FOUND를 반환할거같은데
NotFoundExcption에 필드로 상태코드 NOT_FOUND를 가지고 있는건 어떤가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpStatus는 Presentation 계층에 속한다고 생각합니다! 예외는 Business 계층에서 사용되어지는데 상태코드를 가지고 있으면 상위 계층의 정보가 하위 계층에 노출된다고 생각합니다. 따라서 상태코드는 Presentation 계층에 속하는 ExceptionHandler에서 지정하도록 개발을 진행하였습니다 😀


}