Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Default Exception Handling Template #111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/main/java/com/classvar/error/ControllerAdvice.java

This file was deleted.

177 changes: 177 additions & 0 deletions src/main/java/com/classvar/error/GlobalApiExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.classvar.error;

import static com.classvar.error.exception.util.ErrorResponseUtil.build;
import static java.util.stream.Collectors.toList;

import com.classvar.error.exception.BusinessException;
import com.classvar.error.exception.dto.ErrorResponseDto;
import com.classvar.error.exception.dto.InvalidParameterDto;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import javax.validation.ConstraintViolationException;
import javax.validation.Path;
import javax.validation.Path.Node;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
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.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
@Slf4j
public class GlobalApiExceptionHandler extends ResponseEntityExceptionHandler {
seongbin9786 marked this conversation as resolved.
Show resolved Hide resolved

@ExceptionHandler(value = {Exception.class})
public ResponseEntity<Object> handleUncaughtException(final Exception ex,
final ServletWebRequest request) {
log(ex, request);
final ErrorResponseDto errorResponseDto = build(Exception.class.getSimpleName(),
"서버의 문제로 응답에 문제가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponseDto);
seongbin9786 marked this conversation as resolved.
Show resolved Hide resolved
}

@ExceptionHandler({BusinessException.class})
public ResponseEntity<Object> handleCustomUncaughtBusinessException(final BusinessException ex,
final ServletWebRequest request) {
log(ex, request);
final ErrorResponseDto errorResponseDto = build(ex.getCode(), ex.getMessage(),
ex.getHttpStatus());
return ResponseEntity.status(ex.getHttpStatus()).body(errorResponseDto);
}

@ExceptionHandler(value = {ConstraintViolationException.class})
public ResponseEntity<Object> handleConstraintViolationException(
final ConstraintViolationException ex,
final ServletWebRequest request) {
log(ex, request);

final List<InvalidParameterDto> invalidParameters = new ArrayList<>();
ex.getConstraintViolations().forEach(constraintViolation -> {
final Iterator<Node> it = constraintViolation.getPropertyPath().iterator();
if (it.hasNext()) {
try {
it.next();
final Path.Node n = it.next();
final InvalidParameterDto invalidParameter = new InvalidParameterDto();
invalidParameter.setParameter(n.getName());
invalidParameter.setMessage(constraintViolation.getMessage());
invalidParameters.add(invalidParameter);
} catch (final Exception e) {
log.warn("Can't extract the information about constraint violation");
}
}
});

final ErrorResponseDto errorResponseDto = build(
ConstraintViolationException.class.getSimpleName(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST, invalidParameters);

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponseDto);
}

//메시지 컨버터에서 변환할 수 없는 경우
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(
final HttpMessageNotReadableException ex,
final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
log(ex, (ServletWebRequest) request);
final ErrorResponseDto errorResponseDto = build(
HttpMessageNotReadableException.class.getSimpleName(), ex.getMessage(),
HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponseDto);
}

@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
final HttpRequestMethodNotSupportedException ex, final HttpHeaders headers,
final HttpStatus status,
final WebRequest request) {
log(ex, (ServletWebRequest) request);
final ErrorResponseDto errorResponseDto = build(
HttpRequestMethodNotSupportedException.class.getSimpleName(), ex.getMessage(),
HttpStatus.METHOD_NOT_ALLOWED);
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(errorResponseDto);
}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
final MethodArgumentNotValidException ex,
final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
log(ex, (ServletWebRequest) request);
final List<InvalidParameterDto> invalidParameters = ex.getBindingResult().getFieldErrors()
.stream()
.map(fieldError -> InvalidParameterDto.builder()
.parameter(fieldError.getField())
.message(fieldError.getDefaultMessage())
.build()).collect(toList());

final ErrorResponseDto errorResponseDto = build(
MethodArgumentNotValidException.class.getSimpleName(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST, invalidParameters);

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponseDto);
}

@Override
protected ResponseEntity<Object> handleMissingPathVariable(final MissingPathVariableException ex,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

얘는 참조가 없는 걸로 나옵니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 어떻게 확인하신건가요? 포스트맨으로 path variable 일부러 빼거나 문자열로 넣어서 테스트해보니 handleMissingServletRequestParameter로 예외가 발생하긴 하네요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깃헙 file change 화면에서 검색만 해봤습니다 =)

final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
final ErrorResponseDto errorResponseDto = build(
MissingPathVariableException.class.getSimpleName(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponseDto);
}

@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(
final MissingServletRequestParameterException ex, final HttpHeaders headers,
final HttpStatus status,
final WebRequest request) {
log(ex, (ServletWebRequest) request);
final ErrorResponseDto errorResponseDto = build(
MissingServletRequestParameterException.class.getSimpleName(),
HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponseDto);
}

private void log(final Exception ex, final ServletWebRequest request) {
final Optional<HttpMethod> httpMethod;
final Optional<String> requestUrl;

final Optional<ServletWebRequest> possibleIncomingNullRequest = Optional.ofNullable(request);
if (possibleIncomingNullRequest.isPresent()) {
// get the HTTP Method
httpMethod = Optional.ofNullable(possibleIncomingNullRequest.get().getHttpMethod());
if (Optional.ofNullable(possibleIncomingNullRequest.get().getRequest()).isPresent()) {
// get the Request URL
requestUrl = Optional.of(
possibleIncomingNullRequest.get().getRequest().getRequestURL().toString());
} else {
requestUrl = Optional.empty();
}
} else {
httpMethod = Optional.empty();
requestUrl = Optional.empty();
}

log.error("Request {} {} failed with exception reason: {}",
(httpMethod.isPresent() ? httpMethod.get() : "'null'"),
(requestUrl.orElse("'null'")), ex.getMessage(), ex);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.classvar.error.exception;

import com.classvar.error.exception.policy.BusinessExceptionPolicy;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class BusinessException extends RuntimeException implements BusinessExceptionPolicy {

protected final String code;
protected final String message;
protected final HttpStatus httpStatus;

public BusinessException(final BusinessExceptionReason reason) {
this.code = reason.getCode();
this.message = reason.getMessage();
this.httpStatus = reason.getHttpStatus();
}

public BusinessException(final BusinessExceptionReason reason,
final HttpStatus overridingHttpStatus) {
this.code = reason.getCode();
this.message = reason.getMessage();
this.httpStatus = overridingHttpStatus;
}

public BusinessException(final BusinessExceptionReason reason, final Object... parameters) {
if (parameters != null) {
this.message = String.format(reason.getMessage(), parameters);
} else {
this.message = reason.getMessage();
}
this.code = reason.getCode();
this.httpStatus = reason.getHttpStatus();
}

@Override
public String getLocalizedMessage() {
return getMessage();
}

public String toString() {
return String.format("BusinessException(code=%s, message=%s, httpStatus=%s)", this.getCode(),
this.getMessage(),
this.getHttpStatus().value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.classvar.error.exception;

import com.classvar.error.exception.policy.BusinessExceptionPolicy;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum BusinessExceptionReason implements BusinessExceptionPolicy {
NO_SUCH_ID("No such id: %s", HttpStatus.BAD_REQUEST);

private final String code = BusinessExceptionReason.class.getSimpleName();
private final String message;
private final HttpStatus httpStatus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.classvar.error.exception.dto;

import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Getter
public class ErrorResponseDto {

private String code;
private String message;
private Integer status;
private LocalDateTime timestamp;
private List<InvalidParameterDto> invalidParameters;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.classvar.error.exception.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
public class InvalidParameterDto {
seongbin9786 marked this conversation as resolved.
Show resolved Hide resolved

private String parameter;
private String message;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.classvar.error.exception.policy;

import org.springframework.http.HttpStatus;

public interface BusinessExceptionPolicy extends ExceptionPolicy{
HttpStatus getHttpStatus();
seongbin9786 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.classvar.error.exception.policy;

public interface ExceptionPolicy {
String getCode();
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.classvar.error.exception.util;

import com.classvar.error.exception.dto.ErrorResponseDto;
import com.classvar.error.exception.dto.InvalidParameterDto;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;

public final class ErrorResponseUtil {

private ErrorResponseUtil() {
}

public static ErrorResponseDto build(final String code, final String message,
final HttpStatus status) {
return buildDetails(code, message, status);
}

public static ErrorResponseDto build(final String code, final String message,
final HttpStatus status,
final List<InvalidParameterDto> invalidParameters) {
return buildDetails(code, message, status, invalidParameters);
}

private static ErrorResponseDto buildDetails(final String code, final String message,
final HttpStatus status) {
ErrorResponseDto errorResponseDto = new ErrorResponseDto(code, message, status.value(),
LocalDateTime.now(), new ArrayList<>());
return errorResponseDto;
}

private static ErrorResponseDto buildDetails(final String code, final String message,
final HttpStatus status,
final List<InvalidParameterDto> invalidParameters) {
ErrorResponseDto errorResponseDetails = new ErrorResponseDto(code, message,
status.value(), LocalDateTime.now(), new ArrayList<>());

if (!CollectionUtils.isEmpty(invalidParameters)) {
errorResponseDetails = new ErrorResponseDto(code, message,
status.value(), LocalDateTime.now(), invalidParameters);
}
return errorResponseDetails;
}

}