package com.cos.photogramstart.handler;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import com.cos.photogramstart.handler.ex.CustomApiException;
import com.cos.photogramstart.handler.ex.CustomValidationApiException;
import com.cos.photogramstart.handler.ex.CustomValidationException;
import com.cos.photogramstart.util.Script;
import com.cos.photogramstart.web.dto.CMRespDto;
@RestController
@ControllerAdvice // 발생한 모든 exception을 낚아챔
public class ControllerExceptionHandler {
@ExceptionHandler(CustomValidationException.class)
public String validationException(CustomValidationException e) {
// CMRespDto, Script 비교
// 1. 클라이언트에게 응답할때는 script 좋음
// 2. Ajax 통신 - CMRespDto
// 3. Android 통신 - CMRespDto
return Script.back(e.getErrorMap().toString());
}
@ExceptionHandler(CustomValidationApiException.class)
public ResponseEntity<?> validationApiException(CustomValidationApiException e) {
// ResponseEntity 응답 -> ajax에 fail이 실행되어서 메시지, 에러맵이 날아감
return new ResponseEntity<>(new CMRespDto<>(-1, e.getMessage(), e.getErrorMap()), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(CustomApiException.class)
public ResponseEntity<?> apiException(CustomApiException e) {
// ResponseEntity 응답 -> ajax에 fail이 실행되어서 메시지, 에러맵이 날아감
return new ResponseEntity<>(new CMRespDto<>(-1, e.getMessage(), null), HttpStatus.BAD_REQUEST);
}
}
위 3개의 exception으로 일어날 수 있는 프로젝트 예외 처리 가능!ㅎㅎ