Skip to content

Commit

Permalink
Merge pull request #98 from KUIT-Space/feat/#97httpmessagenotreadable…
Browse files Browse the repository at this point in the history
…exception

HttpMessageNotReadable 예외 처리
  • Loading branch information
drbug2000 authored Aug 14, 2024
2 parents c4fc2a1 + 62867b6 commit c4bda32
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package space.space_spring.config.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;

import java.io.IOException;
@Component
public class RequestCachingFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
filterChain.doFilter(wrappedRequest, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package space.space_spring.exceptionHandler;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;
import space.space_spring.exception.BadRequestException;
import space.space_spring.exception.InternalServerErrorException;
import space.space_spring.response.BaseErrorResponse;

import java.io.BufferedReader;
import java.io.IOException;

import static space.space_spring.response.status.BaseExceptionResponseStatus.*;

@Slf4j
Expand Down Expand Up @@ -62,4 +70,36 @@ public BaseErrorResponse handle_RuntimeException(Exception e) {
log.error("[handle_RuntimeException]", e);
return new BaseErrorResponse(SERVER_ERROR);
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({HttpMessageNotReadableException.class, HttpMessageConversionException.class})
public BaseErrorResponse HttpNotReadableException(Exception e, HttpServletRequest request) {
log.error("[handle_RuntimeException]", e);

String requestBody = getRequestBody(request);


return new BaseErrorResponse(HTTP_MESSAGE_NOT_READABLE,HTTP_MESSAGE_NOT_READABLE.getMessage()+" : request body content : "+requestBody + " :content end");
}

private String getRequestBody(HttpServletRequest request){
String requestBody = "";
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
try {
requestBody = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
} catch (IOException e) {
// 예외 처리
return "IO exception in parsing request body";
}
}
}


return requestBody.toString();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
BAD_REQUEST(2000, HttpStatus.BAD_REQUEST, "유효하지 않은 요청입니다."),
URL_NOT_FOUND(2001, HttpStatus.BAD_REQUEST, "유효하지 않은 URL 입니다."),
METHOD_NOT_ALLOWED(2002, HttpStatus.METHOD_NOT_ALLOWED, "해당 URL에서는 지원하지 않는 HTTP Method 입니다."),
HTTP_MESSAGE_NOT_READABLE(2003, HttpStatus.BAD_REQUEST,"request body 양식에 문제가 있습니다"),

/**
* 3000: Server, Database 오류 (INTERNAL_SERVER_ERROR)
Expand Down

0 comments on commit c4bda32

Please sign in to comment.