protected static InvocationException doCreate(StatusType status, Object errorData) { return new InvocationException(status, errorData); }
protected static InvocationException doCreate(int statusCode, String reasonPhrase, CommonExceptionData data, Throwable e) { return new InvocationException(statusCode, reasonPhrase, data, e); }
protected void initProduceProcessor() { produceProcessor = restOperationMeta.ensureFindProduceProcessor(requestEx); if (produceProcessor == null) { String msg = String.format("Accept %s is not supported", requestEx.getHeader(HttpHeaders.ACCEPT)); throw new InvocationException(Status.NOT_ACCEPTABLE, msg); } }
public void ensureStatusUp() { SCBStatus currentStatus = getStatus(); if (!SCBStatus.UP.equals(currentStatus)) { String message = "The request is rejected. Cannot process the request due to STATUS = " + currentStatus; LOGGER.warn(message); throw new InvocationException(Status.SERVICE_UNAVAILABLE, message); } }
private Object checkRequiredAndDefaultValue() { if (isRequired()) { throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } return getDefaultValue(); }
@Override public Response convert(SwaggerInvocation swaggerInvocation, ConstraintViolationException e) { return Response.createFail(new InvocationException(Status.BAD_REQUEST, e.getConstraintViolations().toString())); }
private Object checkRequiredAndDefaultValue() { if (!ignoreRequiredCheck && isRequired()) { throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } Object defaultValue = getDefaultValue(); if (!ignoreDefaultValue && defaultValue != null) { return defaultValue; } return null; }
private Object checkRequiredAndDefaultValue() { if (!ignoreRequiredCheck && isRequired()) { throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } return getDefaultValue(); }
private Object checkRequiredAndDefaultValue() { if (isRequired()) { throw new InvocationException(Status.BAD_REQUEST, "Parameter is required."); } return getDefaultValue(); }
private boolean isLimitNewRequest(QpsController qpsController, AsyncResponse asyncResp) { if (qpsController.isLimitNewRequest()) { CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol"); asyncResp.producerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData)); return true; } else { return false; } } }
private void runInExecutor() { try { if (isInQueueTimeout()) { throw new InvocationException(Status.INTERNAL_SERVER_ERROR, "Timeout when processing the request."); } doRunInExecutor(); } catch (Throwable e) { String msg = String.format("handle request error, %s, msgId=%d", operationMeta.getMicroserviceQualifiedName(), msgId); LOGGER.error(msg, e); sendResponse(header.getContext(), Response.providerFailResp(e)); } }
private boolean defineEndpointAndHandle(Invocation invocation, AsyncResponse asyncResp) throws Exception { String endpointUri = invocation.getLocalContext(SERVICECOMB_SERVER_ENDPOINT); if (endpointUri == null) { return false; } URI formatUri = new URI(endpointUri); Transport transport = SCBEngine.getInstance().getTransportManager().findTransport(formatUri.getScheme()); if (transport == null) { LOGGER.error("not deployed transport {}, ignore {}.", formatUri.getScheme(), endpointUri); throw new InvocationException(Status.BAD_REQUEST, "the endpoint's transport is not found."); } Endpoint endpoint = new Endpoint(transport, endpointUri); invocation.setEndpoint(endpoint); invocation.next(resp -> { asyncResp.handle(resp); }); return true; }
public static Object[] restToArgs(HttpServletRequest request, RestOperationMeta restOperation) throws InvocationException { List<RestParam> paramList = restOperation.getParamList(); Object[] paramValues = new Object[paramList.size()]; for (int idx = 0; idx < paramList.size(); idx++) { RestParam param = paramList.get(idx); try { paramValues[idx] = param.getParamProcessor().getValue(request); } catch (Exception e) { // Avoid information leak of user input. throw new InvocationException(Status.BAD_REQUEST, String.format("Parameter is not valid for operation [%s]. Parameter is [%s]. Processor is [%s].", restOperation.getOperationMeta().getMicroserviceQualifiedName(), param.getParamName(), param.getParamProcessor().getProcessorType())); } } return paramValues; } }
public void locate(String microserviceName, String path, String httpMethod, MicroservicePaths microservicePaths) { // 在静态路径中查找 operation = locateStaticPathOperation(path, httpMethod, microservicePaths.getStaticPathOperationMap()); if (operation != null) { // 全部定位完成 return; } // 在动态路径中查找 operation = locateDynamicPathOperation(path, microservicePaths.getDynamicPathOperationList(), httpMethod); if (operation != null) { return; } Status status = Status.NOT_FOUND; if (resourceFound) { status = Status.METHOD_NOT_ALLOWED; } LOGGER.error("locate path failed, status:{}, http method:{}, path:{}, microserviceName:{}", status, httpMethod, path, microserviceName); throw new InvocationException(status, status.getReasonPhrase()); }
@Override public void injectFault(Invocation invocation, FaultParam faultParam, AsyncResponse asyncResponse) { if (!shouldAbort(invocation, faultParam)) { asyncResponse.success(SUCCESS_RESPONSE); return; } // get the config values related to abort percentage. int errorCode = FaultInjectionUtil.getFaultInjectionConfig(invocation, "abort.httpStatus"); if (errorCode == FaultInjectionConst.FAULT_INJECTION_DEFAULT_VALUE) { LOGGER.debug("Fault injection: Abort error code is not configured"); asyncResponse.success(SUCCESS_RESPONSE); return; } // if request need to be abort then return failure with given error code CommonExceptionData errorData = new CommonExceptionData(ABORTED_ERROR_MSG); asyncResponse.consumerFail(new InvocationException(errorCode, ABORTED_ERROR_MSG, errorData)); }
protected void findRestOperation(MicroserviceMeta microserviceMeta) { ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta); if (servicePathManager == null) { LOGGER.error("No schema defined for {}:{}.", microserviceMeta.getAppId(), microserviceMeta.getName()); throw new InvocationException(Status.NOT_FOUND, Status.NOT_FOUND.getReasonPhrase()); } OperationLocator locator = locateOperation(servicePathManager); requestEx.setAttribute(RestConst.PATH_PARAMETERS, locator.getPathVarMap()); this.restOperationMeta = locator.getOperation(); }
@Override public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception { String token = invocation.getContext(Const.AUTH_TOKEN); if (null != token && authenticationTokenManager.valid(token)) { invocation.next(asyncResp); } else { asyncResp.producerFail(new InvocationException(new HttpStatus(401, "UNAUTHORIZED"), "UNAUTHORIZED")); } } }
@Override public void handle(Buffer buff) { if (failed) { return; } uploadSize += buff.length(); if (bodyLimit != -1 && uploadSize > bodyLimit) { failed = true; // enqueue a delete for the error uploads context.fail(new InvocationException(Status.REQUEST_ENTITY_TOO_LARGE, Status.REQUEST_ENTITY_TOO_LARGE.getReasonPhrase())); context.vertx().runOnContext(v -> deleteFileUploads()); } else { // multipart requests will not end up in the request body // url encoded should also not, however jQuery by default // post in urlencoded even if the payload is something else if (!isMultipart /* && !isUrlEncoded */) { body.appendBuffer(buff); } } }
@Override public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception { if (!Config.INSTANCE.isConsumerEnabled()) { invocation.next(asyncResp); return; } QpsController qpsController = qpsControllerMgr.getOrCreate(invocation.getMicroserviceName(), invocation); if (qpsController.isLimitNewRequest()) { // return http status 429 CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol"); asyncResp.consumerFail( new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData)); return; } invocation.next(asyncResp); } }
public Response doInvoke(SwaggerInvocation invocation) { Response response = null; try { invocation.onBusinessMethodStart(); Object[] args = argumentsMapper.toProducerArgs(invocation); for (ProducerInvokeExtension producerInvokeExtension : producerInvokeExtenstionList) { producerInvokeExtension.beforeMethodInvoke(invocation, this, args); } Object result = producerMethod.invoke(producerInstance, args); response = responseMapper.mapResponse(invocation.getStatus(), result); invocation.onBusinessMethodFinish(); invocation.onBusinessFinish(); } catch (IllegalArgumentException ae) { invocation.onBusinessMethodFinish(); invocation.onBusinessFinish(); // ae.getMessage() is always null. Give a custom error message. response = processException(invocation, new InvocationException(Status.BAD_REQUEST.getStatusCode(), "", new CommonExceptionData("Parameters not valid or types not match."), ae)); } catch (Throwable e) { invocation.onBusinessMethodFinish(); invocation.onBusinessFinish(); response = processException(invocation, e); } return response; }