package kr.wisestone.owl.web.resolver;
|
|
import kr.wisestone.owl.common.MessageAccessor;
|
import kr.wisestone.owl.constant.Constants;
|
import kr.wisestone.owl.constant.MsgConstants;
|
import kr.wisestone.owl.exception.ApiAuthException;
|
import kr.wisestone.owl.exception.ApiParameterException;
|
import kr.wisestone.owl.exception.OwlRuntimeException;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
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.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.context.request.WebRequest;
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* Created by jeong on 2017-08-03.
|
*/
|
@ControllerAdvice
|
public class OwlResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
|
private static final Logger LOGGER = LoggerFactory.getLogger(OwlResponseEntityExceptionHandler.class);
|
|
@Autowired
|
private MessageAccessor messageAccessor;
|
|
public OwlResponseEntityExceptionHandler() {
|
super();
|
}
|
|
@ExceptionHandler({ApiParameterException.class })
|
public ResponseEntity<Object> handleBadRequest(final ApiParameterException ex, final WebRequest request) {
|
Map<String, Object> resJsonData = new HashMap<String, Object>();
|
resJsonData.put(Constants.RES_KEY_MESSAGE, this.messageAccessor.getResMessage(ex, Constants.RES_KEY_MSG_FAIL));
|
|
return this.handleExceptionInternal(ex, resJsonData, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
}
|
|
@ExceptionHandler({ApiAuthException.class })
|
public ResponseEntity<Object> handleBadRequest(final ApiAuthException ex, final WebRequest request) {
|
Map<String, Object> resJsonData = new HashMap<String, Object>();
|
resJsonData.put(Constants.RES_KEY_MESSAGE, this.messageAccessor.getResMessage(ex, Constants.RES_KEY_MSG_FAIL));
|
|
return this.handleExceptionInternal(ex, resJsonData, new HttpHeaders(), HttpStatus.UNAUTHORIZED, request);
|
}
|
|
@ExceptionHandler({OwlRuntimeException.class })
|
public ResponseEntity<Object> handleBadRequest(final OwlRuntimeException ex, final WebRequest request) {
|
Map<String, Object> resJsonData = new HashMap<String, Object>();
|
resJsonData.put(Constants.RES_KEY_MESSAGE, this.messageAccessor.getResMessage(ex, Constants.RES_KEY_MSG_FAIL));
|
|
return this.handleExceptionInternal(ex, resJsonData, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
}
|
|
@ExceptionHandler({ StackOverflowError.class,
|
NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
|
public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
|
Map<String, Object> resJsonData = new HashMap<String, Object>();
|
resJsonData.put(Constants.RES_KEY_MESSAGE, this.messageAccessor.getResMessage(MsgConstants.NOT_READABLE_JSON_DATA, Constants.RES_KEY_MSG_FAIL));
|
|
return this.handleExceptionInternal(ex, resJsonData, new HttpHeaders(), HttpStatus.REQUEST_TIMEOUT, request);
|
}
|
|
@ExceptionHandler({ Exception.class })
|
public ResponseEntity<Object> handleInternal(final Exception ex, final WebRequest request) {
|
Map<String, Object> resJsonData = new HashMap<String, Object>();
|
resJsonData.put(Constants.RES_KEY_MESSAGE, this.messageAccessor.getResMessage(MsgConstants.NOT_READABLE_JSON_DATA, Constants.RES_KEY_MSG_FAIL));
|
|
return this.handleExceptionInternal(ex, resJsonData, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
}
|
|
@Override
|
protected ResponseEntity<Object> handleHttpMessageNotReadable(
|
final HttpMessageNotReadableException ex, final HttpHeaders headers,
|
final HttpStatus status, final WebRequest request) {
|
Map<String, Object> resJsonData = new HashMap<String, Object>();
|
resJsonData.put(Constants.RES_KEY_MESSAGE,
|
this.messageAccessor.getResMessage(MsgConstants.NOT_READABLE_JSON_DATA, Constants.RES_KEY_MSG_FAIL));
|
|
return this.handleExceptionInternal(ex, resJsonData, headers, HttpStatus.BAD_REQUEST, request);
|
}
|
|
@Override
|
protected ResponseEntity<Object> handleMethodArgumentNotValid(
|
final MethodArgumentNotValidException ex, final HttpHeaders headers,
|
final HttpStatus status, final WebRequest request) {
|
final String bodyOfResponse = "This should be application specific4";
|
return this.handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
|
}
|
|
@Override
|
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
|
HttpStatus status, WebRequest request) {
|
LOGGER.error(ex.getMessage(), ex);
|
return super.handleExceptionInternal(ex, body, headers, status, request);
|
}
|
}
|