Skip to content

Commit

Permalink
feat : Global Handler 세팅 #114
Browse files Browse the repository at this point in the history
- Exception Super Class(Client Side, Server Side) 제작
- BindException, ServletException, 커스텀 Super Class 두개 핸들러에
  메서드 추가
- 테스트 및 예시용 Service 패키지에 NoMatchIdException 추가
- ExceptionResponse Dto 추가
- ExceptionCode enums 추가 : 내용은 Exception 생성 시마다 추가 권장
  • Loading branch information
tlgur committed Feb 8, 2024
1 parent dd4655e commit f3c04ae
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/main/java/com/skklub/admin/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.skklub.admin;

import com.skklub.admin.exception.ClientSideException;
import com.skklub.admin.exception.ExceptionResponse;
import com.skklub.admin.exception.ServerSideException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 입력값이 잘못되어 예외가 터진 경우
*/
@ExceptionHandler(ClientSideException.class)
public ResponseEntity<ExceptionResponse> clientSideException(ClientSideException clientSideException, HttpServletRequest request) {
ExceptionResponse exceptionResponse = new ExceptionResponse(clientSideException);
return ResponseEntity
.badRequest()
.body(exceptionResponse);
}

/**
* 서버 내부 문제로 예외가 터진 경우
*/
@ExceptionHandler(ServerSideException.class)
public ResponseEntity<ExceptionResponse> serverSideException(ServerSideException serverSideException, HttpServletRequest request) {
ExceptionResponse exceptionResponse = new ExceptionResponse(serverSideException);
return ResponseEntity
.internalServerError()
.body(exceptionResponse);
}

/**
* Spring Bean Validation 관련 예외
*/
@ExceptionHandler(BindException.class)
public ResponseEntity<ExceptionResponse> bindException(BindException bindException, HttpServletRequest request) {
return ResponseEntity
.badRequest()
.body(null);
}

/**
* Spring MVC Controller 파라미터 매칭 예외
*/
@ExceptionHandler(ServletException.class)
public ResponseEntity<ExceptionResponse> servletException(ServletException servletException, HttpServletRequest request) {
return ResponseEntity
.badRequest()
.body(null);
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/skklub/admin/exception/ClientSideException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.skklub.admin.exception;

public class ClientSideException extends RuntimeException {
public ClientSideException() {
}

public ClientSideException(String message) {
super(message);
}

public ClientSideException(String message, Throwable cause) {
super(message, cause);
}

public ClientSideException(Throwable cause) {
super(cause);
}

public ClientSideException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/skklub/admin/exception/ExceptionCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.skklub.admin.exception;

public enum ExceptionCode {
NO_MATCH_ID()
}
26 changes: 26 additions & 0 deletions src/main/java/com/skklub/admin/exception/ExceptionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.skklub.admin.exception;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExceptionResponse {
private ExceptionCode exceptionCode;
private LocalDateTime timeStamp;
private String detailMessage;
private Class exceptionClassName;

public ExceptionResponse(RuntimeException exception) {
this.exceptionCode = null;
this.timeStamp = LocalDateTime.now();
this.detailMessage = exception.getMessage();
this.exceptionClassName = exception.getCause().getClass();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/skklub/admin/exception/ServerSideException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.skklub.admin.exception;

public class ServerSideException extends RuntimeException {
public ServerSideException() {
}

public ServerSideException(String message) {
super(message);
}

public ServerSideException(String message, Throwable cause) {
super(message, cause);
}

public ServerSideException(Throwable cause) {
super(cause);
}

public ServerSideException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.skklub.admin.controller.NoticeController;
import com.skklub.admin.controller.PendingClubController;
import com.skklub.admin.controller.RecruitController;
import com.skklub.admin.error.exception.*;
import com.skklub.admin.exception.deprecated.error.handler.dto.BindingErrorResponse;
import com.skklub.admin.exception.deprecated.error.handler.dto.ErrorDetail;
import com.skklub.admin.exception.deprecated.error.handler.dto.ErrorResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.skklub.admin.service.exception;

import com.skklub.admin.exception.ClientSideException;

public class NoMatchIdException extends ClientSideException {
public NoMatchIdException(Long invalidId) {
super("제공된 ID값(" + invalidId + ")과 일치하는 데이터가 존재하지 않습니다");
}
}

0 comments on commit f3c04ae

Please sign in to comment.