-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from IxxP-Girls/modify
Feat: ExceptionHandler 이용해 예외처리
- Loading branch information
Showing
7 changed files
with
130 additions
and
35 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
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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ixxp/culpop/util/exception/RestApiException.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 |
---|---|---|
@@ -1,4 +1,17 @@ | ||
package com.ixxp.culpop.util.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class RestApiException { | ||
private final HttpStatus status; | ||
private final String errorCode; | ||
private final String errorMessage; | ||
|
||
public RestApiException(HttpStatus status, String errorCode, String errorMessage) { | ||
this.status = status; | ||
this.errorCode = errorCode; | ||
this.errorMessage = errorMessage; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/ixxp/culpop/util/exception/RestApiExceptionHandler.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 |
---|---|---|
@@ -1,4 +1,73 @@ | ||
package com.ixxp.culpop.util.exception; | ||
|
||
import org.springframework.dao.DuplicateKeyException; | ||
import org.springframework.dao.EmptyResultDataAccessException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
import java.util.NoSuchElementException; | ||
|
||
@ControllerAdvice | ||
public class RestApiExceptionHandler { | ||
// 회원가입 - email 중복 | ||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<?> handleIllegalArgumentException(IllegalArgumentException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.BAD_REQUEST, "400", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); | ||
} | ||
|
||
// 로그인 - email 없음 | ||
@ExceptionHandler(UsernameNotFoundException.class) | ||
public ResponseEntity<?> handleUsernameNotFoundException(UsernameNotFoundException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.UNAUTHORIZED, "401", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse); | ||
} | ||
|
||
// 로그인 - 비밀번호 불일치 | ||
@ExceptionHandler(BadCredentialsException.class) | ||
public ResponseEntity<?> handleBadCredentialsException(BadCredentialsException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.UNAUTHORIZED, "401", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse); | ||
} | ||
|
||
// 팝업의 time 을 json 으로 변환 실패 | ||
@ExceptionHandler(RuntimeException.class) | ||
public ResponseEntity<?> handleRuntimeException(RuntimeException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.INTERNAL_SERVER_ERROR, "500", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); | ||
} | ||
|
||
// 팝업, 게시글, 댓글 없음 | ||
@ExceptionHandler(EntityNotFoundException.class) | ||
public ResponseEntity<?> handleEntityNotFoundException(EntityNotFoundException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.NOT_FOUND, "404", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); | ||
} | ||
|
||
// 작성자만 수정 가능 | ||
@ExceptionHandler(AccessDeniedException.class) | ||
public ResponseEntity<?> handleAccessDeniedException(AccessDeniedException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.FORBIDDEN, "403", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse); | ||
} | ||
|
||
// 좋아요 중복 | ||
@ExceptionHandler(DuplicateKeyException.class) | ||
public ResponseEntity<?> handleDuplicateKeyException(DuplicateKeyException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.CONFLICT, "409", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(errorResponse); | ||
} | ||
|
||
// 좋아요 없음 | ||
@ExceptionHandler(NoSuchElementException.class) | ||
public ResponseEntity<?> handleNoSuchElementException(NoSuchElementException e) { | ||
RestApiException errorResponse = new RestApiException(HttpStatus.BAD_REQUEST, "400", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); | ||
} | ||
} |