Skip to content

Commit

Permalink
feat: Exception 핸들러 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed Jun 4, 2024
1 parent f0f17bc commit 2e30237
Showing 1 changed file with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
package com.tukorea.planding.global.error;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionController {
@ExceptionHandler(value = { BusinessException.class })

/*
잘못된 URL 예외
*/
@ExceptionHandler(NoHandlerFoundException.class)
public CommonResponse<?> handleNoHandlerFoundException() {
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_URL);
return CommonUtils.fail(errorResponse);
}

/*
NULL 값
*/
@ExceptionHandler(NullPointerException.class)
public CommonResponse<?> handleNoHandlerNullPointException() {
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_URL);
return CommonUtils.fail(errorResponse);
}

/*
@Valid 핸들러
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error ->
errors.put(error.getField(), error.getDefaultMessage()));
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}

/*
ErrorCode 핸들러
*/
@ExceptionHandler(value = {BusinessException.class})
protected CommonResponse<?> handleBusinessException(BusinessException ex) {
final ErrorCode errorCode = ex.getErrorCode();
final ErrorResponse errorResponse = ErrorResponse.of(errorCode);
return CommonUtils.fail(errorResponse);
}

@ExceptionHandler(InvalidFormatException.class)
public ResponseEntity<ErrorResponse> handleInvalidFormatException(InvalidFormatException ex) {
ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_REQUEST);
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
}

0 comments on commit 2e30237

Please sign in to comment.