-
Notifications
You must be signed in to change notification settings - Fork 3
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
[Resolves #7] 예외 처리 구현 #9
Open
JeongA-Shin
wants to merge
6
commits into
KHUCapston-concoder:develop
Choose a base branch
from
JeongA-Shin:feature/7/exception-handle
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1904d0
feat: 엔티티 작성
binimini 9d4fe17
feat: Repository 작성
binimini 2b61f1c
refactor: 필요 없는 gitkeep 삭제
binimini c065c6c
Merge pull request #3 from KHUCapston-concoder/feat/2/basic-entity
JeongA-Shin a4b4b56
[Resolves #6] redis config 설정 및 테스트 코드 (#5)
JeongA-Shin 8b581f7
feature : custom global exception handler 구현
JeongA-Shin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,62 @@ | ||
package oncoding.concoder.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnection; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.redis.port}") | ||
private int port; | ||
|
||
/* | ||
Spring Data Redis는 Redis에 두 가지 접근 방식을 제공합니다. | ||
하나는 RedisTemplate을 이용한 방식이며, 다른 하나는 RedisRepository를 이용한 방식 | ||
|
||
두 방식 모두 Redis에 접근하기 위해서는 Redis 저장소와 연결하는 과정이 필요 | ||
이 과정을 위해 RedisConnectionFactory 인터페이스(LettuceConnectionFactory )를 사용 | ||
* */ | ||
@Bean //redisConnectionFactory 이름의 빈(역할)은 LettuceConnectionFactory(host, port)를 구현체로 선택한 것 | ||
public LettuceConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
/* | ||
redisTemplate을 사용하여 Redis 저장소에 접근하기로 한다 | ||
RedisTemplate은 Redis 저장소에 오브젝트를 저장할 때 기본값으로 정의된 JdkSerializationRedisSerializer을 이용 | ||
*/ | ||
@Bean | ||
public RedisTemplate<String,Object> redisTemplate(){ | ||
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
|
||
/* | ||
대부분 레디스 key-value 는 문자열 위주이기 때문에 문자열에 특화된 템플릿을 제공 | ||
RedisTemplate 을 상속받은 클래스임. | ||
StringRedisSerializer로 직렬화함 | ||
*/ | ||
@Bean | ||
public StringRedisTemplate stringRedisTemplate(){ | ||
final StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); | ||
stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
stringRedisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
stringRedisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return stringRedisTemplate; | ||
} | ||
|
||
|
||
|
||
} |
Empty file.
21 changes: 21 additions & 0 deletions
21
src/main/java/oncoding/concoder/exception/CustomGlobalException.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,21 @@ | ||
package oncoding.concoder.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class CustomGlobalException extends RuntimeException { | ||
|
||
private final ErrorCode errorCode; | ||
private final String message; | ||
private final HttpStatus status; | ||
|
||
|
||
public CustomGlobalException(ErrorCode errorCode){ | ||
this.errorCode = errorCode; | ||
this.message = errorCode.getMessage(); | ||
this.status = errorCode.getStatus(); | ||
} | ||
|
||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/oncoding/concoder/exception/CustomGlobalExceptionHandler.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,43 @@ | ||
package oncoding.concoder.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class CustomGlobalExceptionHandler { | ||
|
||
@ExceptionHandler(CustomGlobalException.class) | ||
public ResponseEntity<ErrorResponse> handleCustomException(final CustomGlobalException globalException){ | ||
log.error("Custom Exception!", globalException); | ||
return ResponseEntity.status(globalException.getErrorCode().getStatus().value()) | ||
.body(new ErrorResponse(globalException.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
protected ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) { | ||
log.error("Method Not Supported!", e); | ||
return ResponseEntity.status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus().value()) | ||
.body(new ErrorResponse(ErrorCode.METHOD_NOT_ALLOWED)); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
protected ResponseEntity<ErrorResponse> handleNotBlankValid(MethodArgumentNotValidException e){ | ||
log.error("NotValid!", e); | ||
Comment on lines
+30
to
+31
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. 메서드 이름만 바꿔주세용~ |
||
return ResponseEntity.status(HttpStatus.BAD_REQUEST.value()) | ||
.body(new ErrorResponse(e.getFieldError().getDefaultMessage())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
protected ResponseEntity<ErrorResponse> handleException(Exception e) { | ||
log.error("Error!", e); | ||
return ResponseEntity.status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus().value()) | ||
.body(new ErrorResponse(ErrorCode.INTERNAL_SERVER_ERROR)); | ||
} | ||
|
||
} |
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,19 @@ | ||
package oncoding.concoder.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorCode { | ||
|
||
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
POSTS_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 요청의 리소스를 찾을 수 없습니다."), | ||
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않은 메서드입니다."), | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "내부 서버 오류입니다."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/oncoding/concoder/exception/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,35 @@ | ||
package oncoding.concoder.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ErrorResponse { | ||
|
||
private int status; | ||
private String error; | ||
private String code; | ||
private String message; | ||
|
||
|
||
@Builder | ||
public ErrorResponse(ErrorCode errorCode){ | ||
this.status = errorCode.getStatus().value(); | ||
this.error= errorCode.getStatus().name(); | ||
this.code = errorCode.name(); | ||
this.message = errorCode.getMessage(); | ||
} | ||
|
||
@Builder | ||
public ErrorResponse(int status, String error, String code, String message) { | ||
this.status = status; | ||
this.error = error; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public ErrorResponse(String defaultMessage) { | ||
this.message = defaultMessage; | ||
} | ||
|
||
} | ||
Comment on lines
+15
to
+35
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. 빌더랑 생성자 둘 중 하나 패턴으로만 통일하는게 이후 코드가 깔끔해질듯! |
Empty file.
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,17 @@ | ||
package oncoding.concoder.model; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Category extends JpaBaseEntity { | ||
@Column | ||
@NotNull | ||
private String name; | ||
} |
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,15 @@ | ||
package oncoding.concoder.model; | ||
|
||
import java.util.UUID; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
public class JpaBaseEntity { | ||
@Id | ||
@GeneratedValue | ||
private UUID id; | ||
} |
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,17 @@ | ||
package oncoding.concoder.model; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Level extends JpaBaseEntity { | ||
@Column | ||
@NotNull | ||
private String name; | ||
} |
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,41 @@ | ||
package oncoding.concoder.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.OneToMany; | ||
import javax.validation.constraints.NotNull; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Problem extends JpaBaseEntity { | ||
@Column(unique = true) | ||
@NotNull | ||
private Integer number; | ||
|
||
@Column | ||
@NotNull | ||
private String title; | ||
|
||
@Column | ||
private Float rate; | ||
|
||
@Column | ||
@NotNull | ||
private String content; | ||
|
||
@ManyToOne(fetch = FetchType.EAGER) | ||
private Level level; | ||
|
||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "problem") | ||
private List<ProblemCategory> categories = new ArrayList<>(); | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/oncoding/concoder/model/ProblemCategory.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,18 @@ | ||
package oncoding.concoder.model; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ProblemCategory extends JpaBaseEntity { | ||
@ManyToOne | ||
private Problem problem; | ||
|
||
@ManyToOne | ||
private Category category; | ||
} |
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,35 @@ | ||
package oncoding.concoder.model; | ||
|
||
import java.time.LocalDateTime; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EntityListeners; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@EntityListeners(AuditingEntityListener.class) | ||
public class Snapshot extends JpaBaseEntity { | ||
|
||
@CreatedDate | ||
@Column(updatable=false, nullable = false) | ||
private LocalDateTime createdDate; | ||
|
||
@Column | ||
@NotNull | ||
private String memo; | ||
|
||
@Column | ||
@NotNull | ||
private String content; | ||
} |
Empty file.
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/CategoryRepository.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,9 @@ | ||
package oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, UUID> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/LevelRepository.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,9 @@ | ||
package oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.Level; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface LevelRepository extends JpaRepository<Level, UUID> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/ProblemCategoryRepository.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,9 @@ | ||
package oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.ProblemCategory; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ProblemCategoryRepository extends JpaRepository<ProblemCategory, UUID> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/ProblemRepository.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,9 @@ | ||
package oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.Problem; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ProblemRepository extends JpaRepository<Problem, UUID> { | ||
|
||
} |
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
|
||
spring: | ||
redis: | ||
host: localhost | ||
port: 6379 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
중복 필드 삭제해야할것 같습니당~