Java Code Examples for org.apache.servicecomb.swagger.invocation.Response#isFailed()

The following examples show how to use org.apache.servicecomb.swagger.invocation.Response#isFailed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadbalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected boolean isFailedResponse(Response resp) {
  if (resp.isFailed()) {
    if (InvocationException.class.isInstance(resp.getResult())) {
      InvocationException e = (InvocationException) resp.getResult();
      return e.getStatusCode() == ExceptionFactory.CONSUMER_INNER_STATUS_CODE
          || e.getStatusCode() == 503;
    } else {
      return true;
    }
  } else {
    return false;
  }
}
 
Example 2
Source File: ProviderBizkeeperCommand.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isFailedResponse(Response resp) {
  if (resp.isFailed()) {
    if (InvocationException.class.isInstance(resp.getResult())) {
      InvocationException e = (InvocationException) resp.getResult();
      return e.getStatusCode() == ExceptionFactory.PRODUCER_INNER_STATUS_CODE;
    } else {
      return true;
    }
  } else {
    return false;
  }
}
 
Example 3
Source File: ConsumerBizkeeperCommand.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isFailedResponse(Response resp) {
  if (resp.isFailed()) {
    if (InvocationException.class.isInstance(resp.getResult())) {
      InvocationException e = (InvocationException) resp.getResult();
      return e.getStatusCode() == ExceptionFactory.CONSUMER_INNER_STATUS_CODE;
    } else {
      return true;
    }
  } else {
    return false;
  }
}
 
Example 4
Source File: HighwayServerInvoke.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void sendResponse(Map<String, String> context, Response response) {
  invocation.getInvocationStageTrace().finishHandlersResponse();

  ResponseHeader header = new ResponseHeader();
  header.setStatusCode(response.getStatusCode());
  header.setReasonPhrase(response.getReasonPhrase());
  header.setContext(context);
  header.setHeaders(response.getHeaders());

  ResponseRootSerializer bodySchema = operationProtobuf.findResponseRootSerializer(response.getStatusCode());
  Object body = response.getResult();
  if (response.isFailed()) {
    body = ((InvocationException) body).getErrorData();
  }

  try {
    Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body);
    invocation.getInvocationStageTrace().finishServerFiltersResponse();
    connection.write(respBuffer.getByteBuf());
  } catch (Exception e) {
    // keep highway performance and simple, this encoding/decoding error not need handle by client
    String msg = String.format("encode response failed, %s, msgId=%d",
        invocation.getOperationMeta().getMicroserviceQualifiedName(),
        msgId);
    LOGGER.error(msg, e);
  } finally {
    if (invocation != null) {
      invocation.onFinish(response);
    }
  }
}
 
Example 5
Source File: ServerRestArgsFilter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
  Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
  ProduceProcessor produceProcessor =
      (ProduceProcessor) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
  Object body = response.getResult();
  if (response.isFailed()) {
    body = ((InvocationException) body).getErrorData();
  }

  if (null != invocation && isDownloadFileResponseType(invocation, response)) {
    return responseEx.sendPart(PartUtils.getSinglePart(null, body));
  }

  responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");

  CompletableFuture<Void> future = new CompletableFuture<>();
  try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
    produceProcessor.encodeResponse(output, body);

    responseEx.setBodyBuffer(output.getBuffer());
    future.complete(null);
  } catch (Throwable e) {
    future.completeExceptionally(ExceptionFactory.convertProducerException(e));
  }
  return future;
}
 
Example 6
Source File: FilterNode.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private Response rethrowExceptionInResponse(Response response) {
  if (response.isFailed() && response.getResult() instanceof Throwable) {
    AsyncUtils.rethrow(response.getResult());
  }

  return response;
}