use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.
the class TestProducerSchemaFactory method testGetOrCreateProducer.
@Test
public void testGetOrCreateProducer() throws Exception {
OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
Assert.assertEquals("add", operationMeta.getOperationId());
SwaggerProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
ReflectUtils.setField(addBody, "x", 1);
ReflectUtils.setField(addBody, "y", 2);
Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody }) {
@Override
public String getInvocationQualifiedName() {
return "";
}
};
Holder<Response> holder = new Holder<>();
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(3, (int) holder.value.getResult());
invocation.setSwaggerArguments(new Object[] { 1, 2 });
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(true, holder.value.isFailed());
InvocationException exception = (InvocationException) holder.value.getResult();
CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.
the class TestResponse method testAr.
@Test
public void testAr() {
ar.success(Status.ACCEPTED, 1);
Assert.assertEquals(true, response.isSuccessed());
Assert.assertEquals(false, response.isFailed());
Assert.assertEquals(1, (int) response.getResult());
Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatusCode());
Assert.assertEquals(Status.ACCEPTED.getReasonPhrase(), response.getReasonPhrase());
Assert.assertEquals(Status.ACCEPTED, response.getStatus());
ar.success(2);
Assert.assertEquals(2, (int) response.getResult());
Assert.assertEquals(Status.OK.getStatusCode(), response.getStatusCode());
Response r = Response.succResp(3);
ar.complete(r);
Assert.assertEquals(r, response);
ar.consumerFail(new Error("abc"));
CommonExceptionData data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
ar.fail(InvocationType.CONSUMER, new Error("abc"));
data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
InvocationException consumerException = new InvocationException(300, "abc", "def");
ar.consumerFail(consumerException);
Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
Assert.assertEquals(300, response.getStatusCode());
ar.fail(InvocationType.CONSUMER, consumerException);
Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
Assert.assertEquals(300, response.getStatusCode());
ar.producerFail(new Error("abc"));
data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
Assert.assertEquals("Cse Internal Server Error", data.getMessage());
Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
ar.fail(InvocationType.PRODUCER, new Error("abc"));
data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
Assert.assertEquals("Cse Internal Server Error", data.getMessage());
Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
InvocationException producerException = new InvocationException(500, "abc", "def");
ar.producerFail(producerException);
Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
Assert.assertEquals(500, response.getStatusCode());
ar.fail(InvocationType.PRODUCER, producerException);
Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
Assert.assertEquals(500, response.getStatusCode());
}
use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.
the class TestException method testInvocationException.
@Test
public void testInvocationException() {
InvocationException oExceptionIn = new InvocationException(Status.OK, "I am gone now");
oExceptionIn = ExceptionFactory.convertConsumerException(new Throwable());
Assert.assertEquals(490, oExceptionIn.getStatusCode());
oExceptionIn = ExceptionFactory.convertConsumerException(new Throwable(), "abc");
Assert.assertEquals(490, oExceptionIn.getStatusCode());
Assert.assertEquals("abc", ((CommonExceptionData) oExceptionIn.getErrorData()).getMessage());
oExceptionIn = ExceptionFactory.convertProducerException(new Throwable());
Assert.assertEquals(590, oExceptionIn.getStatusCode());
oExceptionIn = ExceptionFactory.convertProducerException(new Throwable(), "abcd");
Assert.assertEquals(590, oExceptionIn.getStatusCode());
Assert.assertEquals("abcd", ((CommonExceptionData) oExceptionIn.getErrorData()).getMessage());
oExceptionIn = ExceptionFactory.convertConsumerException(new InvocationException(Status.OK, new String("fake-object")));
Assert.assertEquals(200, oExceptionIn.getStatusCode());
oExceptionIn = ExceptionFactory.convertConsumerException(new InvocationTargetException(new Throwable()));
Assert.assertNotEquals("java.lang.Throwable", oExceptionIn.getMessage());
InvocationException oTemp = new InvocationException(Status.OK, new CommonExceptionData("testObject"));
Assert.assertEquals("OK", oTemp.getReasonPhrase());
Assert.assertEquals("CommonExceptionData [message=testObject]", (oTemp.getErrorData().toString()));
}
use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.
the class AbortFault method injectFault.
@Override
public void injectFault(Invocation invocation, FaultParam faultParam, AsyncResponse asynResponse) {
// get the config values related to abort.
int abortPercent = FaultInjectionUtil.getFaultInjectionConfig(invocation, "abort.percent");
if (abortPercent == FaultInjectionConst.FAULT_INJECTION_DEFAULT_VALUE) {
LOGGER.debug("Fault injection: Abort percentage is not configured");
asynResponse.success("success");
return;
}
// check fault abort condition.
boolean isAbort = FaultInjectionUtil.isFaultNeedToInject(faultParam.getReqCount(), abortPercent);
if (isAbort) {
// 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");
asynResponse.success("success");
return;
}
// if request need to be abort then return failure with given error code
CommonExceptionData errorData = new CommonExceptionData("aborted by fault inject");
asynResponse.consumerFail(new InvocationException(errorCode, "aborted by fault inject", errorData));
return;
}
asynResponse.success("success");
}
use of org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData in project incubator-servicecomb-java-chassis by apache.
the class DefaultHttpClientFilter method afterReceiveResponse.
@Override
public Response afterReceiveResponse(Invocation invocation, HttpServletResponseEx responseEx) {
OperationMeta operationMeta = invocation.getOperationMeta();
ResponseMeta responseMeta = operationMeta.findResponseMeta(responseEx.getStatus());
RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
ProduceProcessor produceProcessor = findProduceProcessor(swaggerRestOperation, responseEx);
if (produceProcessor == null) {
String msg = String.format("method %s, path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", swaggerRestOperation.getHttpMethod(), swaggerRestOperation.getAbsolutePath(), responseEx.getStatus(), responseEx.getStatusType().getReasonPhrase(), responseEx.getHeader(HttpHeaders.CONTENT_TYPE));
Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
return Response.consumerFailResp(exception);
}
Object result = null;
try {
result = produceProcessor.decodeResponse(responseEx.getBodyBuffer(), responseMeta.getJavaType());
} catch (Exception e) {
return Response.consumerFailResp(e);
}
Response response = Response.create(responseEx.getStatusType(), result);
for (String headerName : responseEx.getHeaderNames()) {
Collection<String> headerValues = responseEx.getHeaders(headerName);
for (String headerValue : headerValues) {
response.getHeaders().addHeader(headerName, headerValue);
}
}
return response;
}
Aggregations