Skip to content

Commit

Permalink
[#132] feat: error 수정
Browse files Browse the repository at this point in the history
산발적인 EntityNotFoundException 정리
추후 18in,,?? message를 사용해봐도 좋을듯
  • Loading branch information
102092 committed Jun 24, 2020
1 parent a70b8d0 commit 6afa8f0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import static com.codesquad.issue.global.api.ApiResult.ERROR;

import com.codesquad.issue.global.api.ApiResult;
import com.codesquad.issue.global.error.exception.CommentNotFoundException;
import com.codesquad.issue.global.error.exception.IssueNotFoundException;
import com.codesquad.issue.global.error.exception.NotFoundException;
import com.codesquad.issue.global.error.exception.ServiceRuntimeException;
import com.codesquad.issue.global.error.exception.UnauthorizedException;
import java.util.Arrays;
import com.codesquad.issue.global.error.exception.MilestoneNotFoundException;
import com.codesquad.issue.global.error.exception.UserNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.TypeMismatchException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -32,6 +33,15 @@ private ResponseEntity<ApiResult> newResponseWithString(String message, HttpStat
return new ResponseEntity<>(ERROR(message, status), headers, status);
}

@ExceptionHandler(value = {
IllegalStateException.class, IllegalArgumentException.class,
TypeMismatchException.class, HttpMessageNotReadableException.class
})
public ResponseEntity<?> handleBadRequestException(Exception e) {
log.debug("Bad request exception occurred: {}", e.getMessage(), e);
return newResponse(e, HttpStatus.BAD_REQUEST);
}

//@Validation Exception handler
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleConstraintViolationException(MethodArgumentNotValidException e) {
Expand All @@ -42,34 +52,23 @@ public ResponseEntity<?> handleConstraintViolationException(MethodArgumentNotVal
HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(ServiceRuntimeException.class)
public ResponseEntity<?> handleServiceRuntimeException(ServiceRuntimeException e) {
if (e instanceof NotFoundException) {
return newResponse(e, HttpStatus.NOT_FOUND);
}
if (e instanceof UnauthorizedException) {
return newResponse(e, HttpStatus.UNAUTHORIZED);
}

log.warn("Unexpected service exception occurred: {}", e.getMessage(), e);
return newResponse(e, HttpStatus.INTERNAL_SERVER_ERROR);
}

//예상하지 못했던 오류
@ExceptionHandler({Exception.class, RuntimeException.class})
public ResponseEntity<?> handleException(Exception e) {
log.error("Unexpected exception occurred: {}", e.getMessage(), e);
return newResponse(e, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<String> userNotFoundExceptionHandler(UserNotFoundException e) {
log.error("Handle UserNotFoundException: ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(IssueNotFoundException.class)
public ResponseEntity<String> issueNotFoundExceptionHandler(IssueNotFoundException e) {
log.error("Handle IssueNotFoundException: ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(CommentNotFoundException.class)
public ResponseEntity<String> commentNotFoundExceptionHandler(CommentNotFoundException e) {
log.error("Handle CommentNotFoundException: ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(MilestoneNotFoundException.class)
public ResponseEntity<String> mileStoneNotFoundExceptionHandler(MilestoneNotFoundException e) {
log.error("Handle MilestoneNotFoundException: ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codesquad.issue.global.error.exception;

public class NotFoundException extends ServiceRuntimeException {

public NotFoundException() {
}

public NotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codesquad.issue.global.error.exception;

public abstract class ServiceRuntimeException extends RuntimeException {

public ServiceRuntimeException() {
}

public ServiceRuntimeException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.codesquad.issue.global.error.exception;

public class UnauthorizedException extends ServiceRuntimeException {

public UnauthorizedException() {
}


public UnauthorizedException(String message) {
super(message);
}
}

This file was deleted.

0 comments on commit 6afa8f0

Please sign in to comment.