-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
23 deletions.
There are no files selected for viewing
124 changes: 104 additions & 20 deletions
124
main/src/main/java/org/sopt/makers/crew/main/common/advice/ControllerExceptionAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,119 @@ | ||
package org.sopt.makers.crew.main.common.advice; | ||
|
||
import org.sopt.makers.crew.main.common.exception.BaseException; | ||
import org.sopt.makers.crew.main.common.exception.CommonResponseDto; | ||
import org.sopt.makers.crew.main.common.exception.ExceptionResponse; | ||
import org.sopt.makers.crew.main.common.exception.ErrorStatus; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.MissingPathVariableException; | ||
import org.springframework.web.bind.MissingServletRequestParameterException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class ControllerExceptionAdvice { | ||
|
||
@ExceptionHandler(BaseException.class) | ||
public ResponseEntity<CommonResponseDto> handleGlobalException(BaseException ex) { | ||
return ResponseEntity.status(ex.getStatusCode()) | ||
.body(CommonResponseDto.fail(ex.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(MissingServletRequestParameterException.class) | ||
public ResponseEntity<CommonResponseDto> handleMissingParameter( | ||
MissingServletRequestParameterException ex) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(CommonResponseDto.fail( | ||
ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<CommonResponseDto> handleIllegalArgument(IllegalArgumentException ex) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(CommonResponseDto.fail(ex.getMessage())); | ||
} | ||
@ExceptionHandler(BaseException.class) | ||
public ResponseEntity<ExceptionResponse> handleGlobalException(BaseException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(e.getStatusCode()) | ||
.body(ExceptionResponse.fail(e.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(MissingServletRequestParameterException.class) | ||
public ResponseEntity<ExceptionResponse> handleMissingParameter( | ||
MissingServletRequestParameterException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<ExceptionResponse> handleIllegalArgument(IllegalArgumentException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) // @Notnull 오류 | ||
public ResponseEntity<ExceptionResponse> handleConstraintViolationException(ConstraintViolationException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) // null value 오류 | ||
public ResponseEntity<ExceptionResponse> handleDataIntegrityViolationException(DataIntegrityViolationException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(HttpMessageNotReadableException.class) // null value 오류 | ||
public ResponseEntity<ExceptionResponse> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ExceptionResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
/** | ||
* path variable errors | ||
*/ | ||
@ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
public ResponseEntity<ExceptionResponse> handleMethodArgumentTypeMismatchException( | ||
MethodArgumentTypeMismatchException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(MissingPathVariableException.class) | ||
public ResponseEntity<ExceptionResponse> handleMissingPathVariableException( | ||
MissingPathVariableException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
public ResponseEntity<ExceptionResponse> handleHttpRequestMethodNotSupportedException( | ||
HttpRequestMethodNotSupportedException e) { | ||
log.warn("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INVALID_INPUT_VALUE.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ExceptionResponse> handleException(Exception e) { | ||
log.error("{}", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body(ExceptionResponse.fail( | ||
ErrorStatus.INTERNAL_SERVER_ERROR.getErrorCode())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters