Search in sources :

Example 21 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class CustomErrorDecoder method decode.

@Override
public Exception decode(String methodKey, Response response) {
    if (response.body() != null) {
        try {
            String content = CharStreams.toString(new InputStreamReader(response.body().asInputStream(), StandardCharsets.UTF_8));
            Map responseBody = JsonUtils.toObject(content, Map.class);
            if (responseBody.containsKey("code")) {
                int code = Integer.parseInt(responseBody.get("code").toString());
                return new JeesuiteBaseException(code, Objects.toString(responseBody.get("msg")));
            }
        } catch (Exception e) {
        }
    } else {
        logger.error("feign_client_error ->method:{},status:{},message:{}", methodKey, response.status(), response.reason());
        String message = response.reason();
        if (message == null)
            message = "服务调用错误";
        return new JeesuiteBaseException(response.status(), message + "(" + methodKey + ")");
    }
    String error = String.format("feign_client_error ->method:%s,status:%s,message:%s", methodKey, response.status(), response.reason());
    return new JeesuiteBaseException(500, error);
}
Also used : InputStreamReader(java.io.InputStreamReader) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) Map(java.util.Map) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 22 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class GlobalExceptionHandler method exceptionHandler.

@ExceptionHandler(Exception.class)
@ResponseBody
public WrapperResponseEntity exceptionHandler(Exception e, HttpServletResponse response) {
    // 缓存回滚
    if (rollbackCacheMethod != null) {
        try {
            rollbackCacheMethod.invoke(null);
        } catch (Exception e2) {
        }
    }
    WrapperResponseEntity resp = new WrapperResponseEntity();
    e = (Exception) getActualThrowable(e);
    if (e instanceof JeesuiteBaseException) {
        JeesuiteBaseException e1 = (JeesuiteBaseException) e;
        resp.setCode(e1.getCode());
        resp.setMsg(e1.getMessage());
    } else if (e instanceof org.springframework.web.HttpRequestMethodNotSupportedException) {
        resp.setCode(HttpStatus.METHOD_NOT_ALLOWED.value());
        resp.setMsg(e.getMessage());
    } else if (e instanceof org.springframework.web.HttpMediaTypeException) {
        resp.setCode(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
        resp.setMsg(e.getMessage());
    } else if (e instanceof org.springframework.web.bind.MissingServletRequestParameterException) {
        resp.setCode(1001);
        resp.setMsg(e.getMessage());
    } else if (e instanceof MethodArgumentNotValidException) {
        List<ObjectError> errors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
        StringBuffer errorMsg = new StringBuffer();
        errors.stream().forEach(x -> errorMsg.append(x.getDefaultMessage()).append(";"));
        resp.setCode(400);
        resp.setMsg(errorMsg.toString());
    } else {
        Throwable parent = e.getCause();
        if (parent instanceof IllegalStateException) {
            resp.setCode(501);
            resp.setMsg(e.getMessage());
        } else {
            resp.setCode(500);
            resp.setMsg("系统繁忙");
        }
    }
    // 默认情况http code都转换为200,异常信息由异常体传递
    if (Boolean.parseBoolean(CurrentRuntimeContext.getRequest().getHeader(CustomRequestHeaders.HEADER_HTTP_STATUS_KEEP))) {
        int errorCode = resp.getCode();
        if (errorCode >= 400 && errorCode <= 500) {
            response.setStatus(errorCode);
        } else {
            response.setStatus(500);
        }
    }
    response.addIntHeader(CustomRequestHeaders.HEADER_EXCEPTION_CODE, resp.getCode());
    response.addHeader(CustomRequestHeaders.HEADER_RESP_KEEP, Boolean.TRUE.toString());
    // 
    ActionLogCollector.onResponseEnd(response, e);
    return resp;
}
Also used : MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) WrapperResponseEntity(com.jeesuite.springweb.model.WrapperResponseEntity) ObjectError(org.springframework.validation.ObjectError) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 23 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class GlobalExceptionHandler method exceptionHandler.

@ExceptionHandler(Exception.class)
@ResponseBody
public WrapperResponseEntity exceptionHandler(Exception e, HttpServletResponse response) {
    // 缓存回滚
    if (rollbackCacheMethod != null) {
        try {
            rollbackCacheMethod.invoke(null);
        } catch (Exception e2) {
        }
    }
    WrapperResponseEntity resp = new WrapperResponseEntity();
    if (e.getCause() != null && e.getCause() instanceof JeesuiteBaseException) {
        JeesuiteBaseException e1 = (JeesuiteBaseException) e.getCause();
        resp.setCode(e1.getCode());
        resp.setMsg(e1.getMessage());
    } else if (e instanceof JeesuiteBaseException) {
        JeesuiteBaseException e1 = (JeesuiteBaseException) e;
        resp.setCode(e1.getCode());
        resp.setMsg(e1.getMessage());
    } else if (e instanceof org.springframework.web.HttpRequestMethodNotSupportedException) {
        resp.setCode(HttpStatus.METHOD_NOT_ALLOWED.value());
        resp.setMsg(e.getMessage());
    } else if (e instanceof org.springframework.web.HttpMediaTypeException) {
        resp.setCode(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
        resp.setMsg(e.getMessage());
    } else if (e instanceof org.springframework.web.bind.MissingServletRequestParameterException) {
        resp.setCode(1001);
        resp.setMsg(e.getMessage());
    } else {
        Throwable parent = e.getCause();
        if (parent instanceof IllegalStateException) {
            resp.setCode(501);
            resp.setMsg(e.getMessage());
        } else {
            resp.setCode(500);
            resp.setMsg("系统繁忙");
        }
        logger.error("", e);
    }
    if (resp.getCode() >= 400 && resp.getCode() <= 500) {
        response.setStatus(resp.getCode());
    }
    return resp;
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) WrapperResponseEntity(com.jeesuite.springweb.model.WrapperResponseEntity) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class ActionLogCollector method onResponseEnd.

public static void onResponseEnd(HttpServletResponse response, Throwable throwable) {
    if (context.get() == null) {
        if (throwable != null) {
            String requestURI = CurrentRuntimeContext.getRequest().getRequestURI();
            if (throwable instanceof JeesuiteBaseException) {
                log.warn("bizError -> request:{},message:{}", requestURI, throwable.getMessage());
            } else {
                log.error("systemError", throwable);
            }
        }
        return;
    }
    ActionLog actionLog = context.get();
    if (actionLog == null)
        return;
    actionLog.setResponseAt(new Date());
    actionLog.setResponseCode(response.getStatus());
    if (throwable != null) {
        log.error("requestError:{}", actionLog.getExceptions());
    } else if (log.isDebugEnabled()) {
        String requestLogMessage = RequestLogBuilder.responseLogMessage(actionLog.getResponseCode(), actionLog.getResponseData());
        log.debug(requestLogMessage);
    }
    try {
        asyncSendExecutor.execute(new Runnable() {

            @Override
            public void run() {
                saveLog(actionLog);
            }
        });
    } catch (Exception e) {
    } finally {
        context.remove();
    }
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) Date(java.util.Date) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 25 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class SignatureRequestHandler method process.

@Override
public Object process(RequestContext ctx, HttpServletRequest request, BizSystemModule module) {
    String sign = request.getHeader(X_SIGN_HEADER);
    if (StringUtils.isBlank(sign))
        return null;
    String timestamp = request.getHeader(TIMESTAMP_HEADER);
    String appId = request.getHeader(APP_ID_HEADER);
    if (StringUtils.isAnyBlank(timestamp, appId)) {
        throw new JeesuiteBaseException("认证头信息不完整");
    }
    String secret = appIdSecretMappings.get(appId);
    if (StringUtils.isBlank(secret)) {
        throw new JeesuiteBaseException("appId不存在");
    }
    Map<String, Object> requestDatas = new HttpServletRequestReader(request).getRequestDatas();
    String signBaseString = StringUtils.trimToEmpty(ParameterUtils.mapToQueryParams(requestDatas)) + timestamp + secret;
    String expectSign = DigestUtils.md5(signBaseString);
    if (!expectSign.equals(sign)) {
        throw new JeesuiteBaseException("签名错误");
    }
    ctx.set(AbstractZuulFilter.CTX_IGNORE_AUTH, Boolean.TRUE);
    return null;
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) HttpServletRequestReader(com.jeesuite.springweb.servlet.HttpServletRequestReader)

Aggregations

JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)41 IOException (java.io.IOException)14 Request (okhttp3.Request)7 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 CObjectMetadata (com.jeesuite.cos.CObjectMetadata)3 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SignatureException (java.security.SignatureException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CUploadResult (com.jeesuite.cos.CUploadResult)2 WrapperResponseEntity (com.jeesuite.springweb.model.WrapperResponseEntity)2 COSObject (com.qcloud.cos.model.COSObject)2 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)2 QiniuException (com.qiniu.common.QiniuException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2