-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: exception handling #34
Changes from all commits
3d1e4bb
cf4c105
b24421b
0ef924e
c120e8b
496e10b
464f99b
f9b56a7
704be5a
148dd72
8cc1265
3cdf2d6
92092e4
19fb017
ca82579
9c63448
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 |
---|---|---|
@@ -1,9 +1,6 @@ | ||
package com.gdschongik.gdsc.domain.auth.dao; | ||
|
||
import com.gdschongik.gdsc.domain.auth.domain.RefreshToken; | ||
import java.util.Optional; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> { | ||
Optional<RefreshToken> findByMemberId(Long aLong); | ||
} | ||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CustomException extends RuntimeException { | ||
|
||
private final ErrorCode errorCode; | ||
|
||
public CustomException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorCode { | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "์๋ฒ ์๋ฌ์ ๋๋ค."), | ||
|
||
// Jwt | ||
INVALID_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "์ ํจํ์ง ์์ JWT ํ ํฐ์ ๋๋ค."), | ||
EXPIRED_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "๋ง๋ฃ๋ JWT ํ ํฐ์ ๋๋ค."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
public record ErrorResponse(String errorCodeName, String errorMessage) { | ||
public static ErrorResponse of(ErrorCode errorCode) { | ||
return new ErrorResponse(errorCode.name(), errorCode.getMessage()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(CustomException.class) | ||
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) { | ||
log.error("CustomException : {}", e.getMessage(), e); | ||
return ResponseEntity.status(e.getErrorCode().getStatus()).body(ErrorResponse.of(e.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(Exception e) { | ||
log.error("INTERNAL_SERVER_ERROR : {}", e.getMessage(), e); | ||
return ResponseEntity.status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus()) | ||
.body(ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.gdschongik.gdsc.global.security; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gdschongik.gdsc.global.exception.CustomException; | ||
import com.gdschongik.gdsc.global.exception.ErrorResponse; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class JwtExceptionFilter extends OncePerRequestFilter { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (CustomException e) { | ||
log.error("JWTException : {}", e.getMessage(), e); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setContentType("application/json"); | ||
response.setStatus(e.getErrorCode().getStatus().value()); | ||
response.getWriter().write(objectMapper.writeValueAsString(ErrorResponse.of(e.getErrorCode()))); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import com.gdschongik.gdsc.domain.auth.dto.RefreshTokenDto; | ||
import com.gdschongik.gdsc.domain.member.domain.MemberRole; | ||
import com.gdschongik.gdsc.global.common.constant.JwtConstant; | ||
import com.gdschongik.gdsc.global.exception.CustomException; | ||
import com.gdschongik.gdsc.global.exception.ErrorCode; | ||
import com.gdschongik.gdsc.global.property.JwtProperty; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
|
@@ -83,7 +85,7 @@ public AccessTokenDto parseAccessToken(String accessTokenValue) throws ExpiredJw | |
} catch (ExpiredJwtException e) { | ||
throw e; | ||
} catch (Exception e) { | ||
return null; | ||
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. null๋ก ๋ฆฌํดํ๋๊ฑฐ ์๋ํ ๋ถ๋ถ์ธ๋ฐ ์์ ํ ์ด์ ๊ฐ ์๋์? 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. ๋ง๋ฃ ์ด์ธ์ ์์ธ๋ null์ ๋ฐํํด์ ์ฌ๋ฐ๊ธ ๊ณผ์ ์ ๊ฑฐ์น๊ธฐ๋ณด๋ค ์์ธ๋ฅผ ๋์ง๋ ๊ฒ์ด ์ ์ ํ ๊ฒ ๊ฐ์ ์์ ํ์ต๋๋ค! 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. ๋ง๋ฃ ์ด์ธ์ ์์ธ๊ฐ ๋ฐ์ํด์ ๋ก์ง ๋ค์ ํ๋ฒ ์ฒดํฌํด๋ณด์ค๋์? 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. ์กํ์ง ์๋ ์ด์ ๋ฅผ ํ์ธํด๋ณธ ๊ฒฐ๊ณผ, ์ด์ธ์๋ |
||
throw new CustomException(ErrorCode.INVALID_JWT_TOKEN); | ||
} | ||
} | ||
|
||
|
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.
์ ๊ฑฐ ์๋๊ฐ ๊ถ๊ธํฉ๋๋ค~
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.
์ด ๋ถ๋ถ์ ์ ๊ฐ ์ฐฉ๊ฐํ์ต๋๋ค.
์๋์ ๊ฐ์ด ์์ ํ๋ ๊ฒ ์ข๊ฒ ๋ค์