use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
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 io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class HighwayServerInvoke method sendResponse.
private void sendResponse(Map<String, String> context, Response response) {
ResponseHeader header = new ResponseHeader();
header.setStatusCode(response.getStatusCode());
header.setReasonPhrase(response.getReasonPhrase());
header.setContext(context);
header.setHeaders(response.getHeaders());
WrapSchema bodySchema = operationProtobuf.findResponseSchema(response.getStatusCode());
Object body = response.getResult();
if (response.isFailed()) {
body = ((InvocationException) body).getErrorData();
}
try {
Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body);
netSocket.write(respBuffer);
} catch (Exception e) {
// 没招了,直接打日志
String msg = String.format("encode response failed, %s, msgId=%d", operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
LOGGER.error(msg, e);
}
}
use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class TestLocator method testLocateStatic.
@Test
public void testLocateStatic() {
MicroserviceMeta msm = new MicroserviceMeta("app:ms");
ServicePathManager spm = new ServicePathManager(msm);
RestOperationMeta rom = createRestOperatonMeta("GET", "abc/");
spm.addResource(rom);
rom = createRestOperatonMeta("POST", "abc/");
spm.addResource(rom);
try {
spm.addResource(rom);
} catch (Throwable e) {
Assert.assertEquals("operation with url abc/, method POST is duplicated", e.getMessage());
}
Assert.assertEquals(1, spm.getStaticPathOperationMap().size());
Assert.assertEquals(2, spm.getStaticPathOperationMap().get("abc/").values().size());
try {
spm.locateOperation("abcd", "GET");
} catch (InvocationException e) {
Assert.assertEquals("Not Found", ((CommonExceptionData) e.getErrorData()).getMessage());
}
try {
spm.locateOperation("abc/", "PUT");
} catch (InvocationException e) {
Assert.assertEquals("Method Not Allowed", ((CommonExceptionData) e.getErrorData()).getMessage());
}
OperationLocator locator = spm.locateOperation("abc/", "GET");
Assert.assertEquals(Collections.emptyMap(), locator.getPathVarMap());
locator.locate(spm, "abc/", "POST");
Assert.assertEquals(Collections.emptyMap(), locator.getPathVarMap());
}
use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class TestConfig method testResponse.
@Test
public void testResponse() {
Response response = Response.create(400, "test", null);
InvocationException exception = response.getResult();
Assert.assertEquals(null, exception.getErrorData());
response = Response.create(400, "test", "errorData");
exception = response.getResult();
Assert.assertEquals("errorData", exception.getErrorData());
}
use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class OperationLocator method locate.
public void locate(ServicePathManager servicePathManager, String path, String httpMethod) {
// 在静态路径中查找
operation = locateStaticPathOperation(path, httpMethod, servicePathManager.getStaticPathOperationMap());
if (operation != null) {
// 全部定位完成
return;
}
// 在动态路径中查找
operation = locateDynamicPathOperation(path, servicePathManager.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, servicePathManager.getMicroserviceMeta().getName());
throw new InvocationException(status, status.getReasonPhrase());
}
Aggregations