Search in sources :

Example 1 with ErrorMessage

use of com.jd.httpservice.utils.web.WebResponse.ErrorMessage in project jdchain-core by blockchain-jd-com.

the class GatewayGlobalExceptionHandler method json.

@ExceptionHandler(value = Exception.class)
@ResponseBody
public WebResponse json(HttpServletRequest req, Exception ex) {
    ErrorMessage message = null;
    String reqURL = "[" + req.getMethod() + "] " + req.getRequestURL().toString();
    if (ex instanceof BusinessException) {
        logger.error("BusinessException occurred! --[RequestURL=" + reqURL + "][" + ex.getClass().toString() + "] " + ex.getMessage(), ex);
        BusinessException businessException = (BusinessException) ex;
        message = new ErrorMessage(businessException.getErrorCode(), businessException.getMessage());
    } else {
        logger.error("Unexpected exception occurred! --[RequestURL=" + reqURL + "][" + ex.getClass().toString() + "]" + ex.getMessage(), ex);
        message = new ErrorMessage(ErrorCode.UNEXPECTED.getValue(), ErrorCode.UNEXPECTED.getDescription(ex.getMessage()));
    }
    WebResponse response = WebResponse.createFailureResult(message);
    return response;
}
Also used : BusinessException(utils.BusinessException) WebResponse(com.jd.httpservice.utils.web.WebResponse) ErrorMessage(com.jd.httpservice.utils.web.WebResponse.ErrorMessage) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ErrorMessage

use of com.jd.httpservice.utils.web.WebResponse.ErrorMessage in project jdchain-core by blockchain-jd-com.

the class PeerGlobalExceptionHandler method json.

@ExceptionHandler(value = Exception.class)
@ResponseBody
public WebResponse json(HttpServletRequest req, Exception ex) {
    String reqURL = req.getRequestURL().insert(0, "[" + req.getMethod() + "] ").toString();
    ErrorMessage message = null;
    if (ex instanceof BusinessException) {
        logger.error("BusinessException occurred! --[RequestURL=" + reqURL + "][" + ex.getClass().toString() + "] " + ex.getMessage(), ex);
        BusinessException businessException = (BusinessException) ex;
        message = new ErrorMessage(businessException.getErrorCode(), businessException.getMessage());
    } else if (ex instanceof JSONException) {
        logger.error("JSONException occurred! --[RequestURL=" + reqURL + "][" + ex.getClass().toString() + "] " + ex.getMessage(), ex);
        message = new ErrorMessage(ErrorCode.REQUEST_PARAM_FORMAT_ILLEGAL.getValue(), ErrorCode.REQUEST_PARAM_FORMAT_ILLEGAL.getDescription());
    } else {
        logger.error("Unexpected exception occurred! --[RequestURL=" + reqURL + "][" + ex.getClass().toString() + "]" + ex.getMessage(), ex);
        message = new ErrorMessage(ErrorCode.UNEXPECTED.getValue(), ErrorCode.UNEXPECTED.getDescription(ex.getMessage()));
    }
    WebResponse responseResult = WebResponse.createFailureResult(message);
    return responseResult;
}
Also used : BusinessException(utils.BusinessException) WebResponse(com.jd.httpservice.utils.web.WebResponse) JSONException(com.alibaba.fastjson.JSONException) ErrorMessage(com.jd.httpservice.utils.web.WebResponse.ErrorMessage) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

WebResponse (com.jd.httpservice.utils.web.WebResponse)2 ErrorMessage (com.jd.httpservice.utils.web.WebResponse.ErrorMessage)2 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 BusinessException (utils.BusinessException)2 JSONException (com.alibaba.fastjson.JSONException)1