exception : spring - @ControllerAdvice
This commit is contained in:
@@ -1,39 +1,17 @@
|
||||
package hello.exception.api;
|
||||
|
||||
import hello.exception.exception.UserException;
|
||||
import hello.exception.exhandler.ErrorResult;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class ApiExceptionV2Controller {
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public ErrorResult illegalExHandler(IllegalArgumentException e) {
|
||||
log.error("[exceptionHandler] ex", e);
|
||||
return new ErrorResult("BAD", e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
public ResponseEntity<ErrorResult> userExHandler(UserException e) {
|
||||
log.error("[exceptionHandler] ex", e);
|
||||
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
|
||||
return new ResponseEntity<>(errorResult, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler
|
||||
public ErrorResult exHandler(Exception e) {
|
||||
log.error("[exceptionHandler] ex", e);
|
||||
return new ErrorResult("EX", "내부 오류");
|
||||
}
|
||||
|
||||
@GetMapping("/api2/members/{id}")
|
||||
public MemberDto getMember(@PathVariable("id") String id) {
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package hello.exception.exhandler.advice;
|
||||
|
||||
import hello.exception.api.ApiExceptionController;
|
||||
import hello.exception.exception.UserException;
|
||||
import hello.exception.exhandler.ErrorResult;
|
||||
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.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@Slf4j
|
||||
//@RestControllerAdvice(annotations = RestController.class)
|
||||
@RestControllerAdvice("hello.exception.api")
|
||||
//@RestControllerAdvice(assignableTypes = {ApiExceptionController.class})
|
||||
public class ExControllerAdvice {
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public ErrorResult illegalExHandler(IllegalArgumentException e) {
|
||||
log.error("[exceptionHandler] ex", e);
|
||||
return new ErrorResult("BAD", e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
public ResponseEntity<ErrorResult> userExHandler(UserException e) {
|
||||
log.error("[exceptionHandler] ex", e);
|
||||
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
|
||||
return new ResponseEntity<>(errorResult, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler
|
||||
public ErrorResult exHandler(Exception e) {
|
||||
log.error("[exceptionHandler] ex", e);
|
||||
return new ErrorResult("EX", "내부 오류");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user