GlobalExceptionHandler 추가

main
박성영 1 month ago
parent 2f51a98956
commit 6ec43c353f

@ -0,0 +1,109 @@
package com.vmis.interfaceapp.config;
import com.vmis.interfaceapp.model.common.Envelope;
import com.vmis.interfaceapp.util.ExceptionDetailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
*
*
* <p> .</p>
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* RuntimeException
*
* <p> RuntimeException
* ExceptionDetailUtil .</p>
*
* @param e RuntimeException
* @return ResponseEntity
*/
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException e) {
// ExceptionDetailUtil을 사용하여 에러 메시지 포맷팅
String detail = ExceptionDetailUtil.buildForLog(e);
log.error("[GLOBAL-ERROR] RuntimeException 발생: {}", detail, e);
// 에러 응답 생성
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
"Internal Server Error",
detail
);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(errorResponse);
}
/**
*
*
* @param e Exception
* @return ResponseEntity
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
String detail = ExceptionDetailUtil.buildForLog(e);
log.error("[GLOBAL-ERROR] Exception 발생: {}", detail, e);
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
"Internal Server Error",
detail
);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(errorResponse);
}
/**
* DTO
*/
public static class ErrorResponse {
private int status;
private String error;
private String message;
public ErrorResponse(int status, String error, String message) {
this.status = status;
this.error = error;
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}
Loading…
Cancel
Save