-
Notifications
You must be signed in to change notification settings - Fork 13
[Spring Core] 박지빈 과제 제출합니다. #8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.example.demo.controller.Error; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public enum ErrorCode { | ||
|
|
||
| NOT_EXIST_ELEMENT(HttpStatus.NOT_FOUND, "존재하지 않는 요소입니다."), | ||
| EMAIL_DUPLICATION(HttpStatus.CONFLICT, "이미 존재하는 이메일입니다."), | ||
|
|
||
| NULL_ELEMENT(HttpStatus.BAD_REQUEST, "null인 값이 존재합니다. 다시 입력해주세요."), | ||
|
|
||
| NOT_EXIST_USER(HttpStatus.BAD_REQUEST, "존재하지 않는 사용자 또는 게시판을 참조하고 있습니다."), | ||
|
|
||
| ARTICLE_EXIST(HttpStatus.BAD_REQUEST, "작성한 게시물이 존재합니다."), | ||
| BOARD_EXIST(HttpStatus.BAD_REQUEST, "해당 게시판에 작성된 게시물이 존재합니다."); | ||
|
|
||
|
|
||
| ErrorCode(HttpStatus status, String message) { | ||
| this.message = message; | ||
| this.status = status; | ||
|
|
||
| } | ||
|
|
||
| public HttpStatus getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| private final HttpStatus status; | ||
|
|
||
| private final String message; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.example.demo.controller.Error; | ||
|
|
||
|
|
||
| import org.springframework.dao.DataIntegrityViolationException; | ||
| import org.springframework.dao.EmptyResultDataAccessException; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.validation.BindingResult; | ||
| import org.springframework.validation.FieldError; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
| import java.sql.SQLIntegrityConstraintViolationException; | ||
|
|
||
| @RestControllerAdvice | ||
| public class ErrorExceptionHandler { | ||
|
|
||
|
|
||
| @ExceptionHandler(EmptyResultDataAccessException.class) | ||
| public ResponseEntity<ErrorResponse> EmptyResultDataAccessException(EmptyResultDataAccessException e) { | ||
| final ErrorResponse response = ErrorResponse.from(ErrorCode.NOT_EXIST_ELEMENT); | ||
| return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); | ||
| } | ||
|
|
||
|
|
||
| @ExceptionHandler(NullPointerException.class) | ||
| public ResponseEntity<ErrorResponse> NullPointerException(NullPointerException e) { | ||
| final ErrorResponse response = ErrorResponse.from(ErrorCode.NULL_ELEMENT); | ||
| return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); | ||
| } | ||
|
|
||
| @ExceptionHandler(DataIntegrityViolationException.class) | ||
| public ResponseEntity<ErrorResponse> DataIntegrityViolationException(DataIntegrityViolationException e) { | ||
| final ErrorResponse response = ErrorResponse.from(ErrorCode.NOT_EXIST_USER); | ||
| return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); | ||
| } | ||
|
|
||
|
|
||
| @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| public String methodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
| BindingResult bindingResult = e.getBindingResult(); | ||
|
|
||
| StringBuilder stringBuilder = new StringBuilder(); | ||
| for (FieldError fieldError : bindingResult.getFieldErrors()) { | ||
| stringBuilder.append("["); | ||
| stringBuilder.append(fieldError.getField()); | ||
| stringBuilder.append("](은)는 "); | ||
| stringBuilder.append(fieldError.getDefaultMessage()); | ||
| stringBuilder.append(" 입력된 값: ["); | ||
| stringBuilder.append(fieldError.getRejectedValue()); | ||
| stringBuilder.append("]\n"); | ||
| } | ||
|
|
||
| return stringBuilder.toString(); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.example.demo.controller.Error; | ||
|
|
||
| import org.springframework.validation.FieldError; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ErrorResponse { | ||
| private String message; | ||
| private int status; | ||
|
|
||
| public ErrorResponse(final ErrorCode errorCode) { | ||
| this.status = errorCode.getStatus().value(); | ||
| this.message = errorCode.getMessage(); | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| public int getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
|
|
||
| public ErrorResponse(final ErrorCode errorCode, final List<FieldError> errors) { | ||
| this.status = errorCode.getStatus().value(); | ||
| this.message = errorCode.getMessage(); | ||
| } | ||
|
|
||
| public static ErrorResponse from(final ErrorCode errorCode) { | ||
| return new ErrorResponse(errorCode); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,16 @@ | ||||||||
| package com.example.demo.controller.dto.request; | ||||||||
|
|
||||||||
| import jakarta.validation.constraints.NotNull; | ||||||||
|
|
||||||||
| public record ArticleCreateRequest( | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JsonNaming 어노테이션을 활용하면 굳이 코드레벨에서 스네이크 케이스의 코드가 돌아다닐 필요가 없습니다.
Suggested change
참고자료: https://www.baeldung.com/jackson-deserialize-snake-to-camel-case#use-jsonnaming-annotation
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JsonNaming이라는 어노테이션을 처음 보는데, 공부해서 코드에 실제 적용해 볼수 있도록 하겠습니다. |
||||||||
| Long authorId, | ||||||||
| Long boardId, | ||||||||
| String title, | ||||||||
| String description | ||||||||
| @NotNull | ||||||||
| Long author_id, | ||||||||
| @NotNull | ||||||||
| Long board_id, | ||||||||
| @NotNull | ||||||||
| String title, | ||||||||
| @NotNull | ||||||||
| String content | ||||||||
| ) { | ||||||||
|
|
||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ArticleUpdateRequest( | ||
| Long boardId, | ||
| String title, | ||
| String description | ||
| @NotNull | ||
| Long boardId, | ||
| @NotNull | ||
| String title, | ||
| @NotNull | ||
| String description | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record BoardCreateRequest( | ||
| String name | ||
| @NotNull | ||
| String name | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| public record BoardUpdateRequest( | ||
| String name | ||
| String name | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record MemberCreateRequest( | ||
| String name, | ||
| String email, | ||
| String password | ||
| @NotNull | ||
| String name, | ||
| @NotNull | ||
| String email, | ||
| @NotNull | ||
| String password | ||
| ) { | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Service 레이어에서 해당 예외를 catch한 뒤 내가 핸들링하고싶은 상황에 대한 새로운 예외를 만들어서 그 예외를 반환해볼 수 있을 것 같아요
try-catch는 Controller의 책임인가? 도 한번 생각해보시면 좋을 것 같아요 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
보통 controller는 최대한 간단하고 역할이 없게 만들고, Service 에서 try-catch를 사용한다고 들었습니다.