[ https://issues.apache.org/jira/browse/SCB-1060?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16709951#comment-16709951 ]
ASF GitHub Bot commented on SCB-1060: ------------------------------------- liubao68 closed pull request #1018: [SCB-1060]edge support Exception converter URL: https://github.com/apache/servicecomb-java-chassis/pull/1018 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java index 53edb8b9b..75f899195 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java @@ -31,6 +31,7 @@ import org.apache.servicecomb.it.testcase.TestDefaultValue; import org.apache.servicecomb.it.testcase.TestDownload; import org.apache.servicecomb.it.testcase.TestDownloadSlowStreamEdge; +import org.apache.servicecomb.it.testcase.TestExceptionConvertEdge; import org.apache.servicecomb.it.testcase.TestGenericEdge; import org.apache.servicecomb.it.testcase.TestIgnoreMethod; import org.apache.servicecomb.it.testcase.TestIgnoreStaticMethod; @@ -106,6 +107,7 @@ private static void runShareTestCases() throws Throwable { ITJUnitUtils.runWithRest(TestAcceptType.class); ITJUnitUtils.runWithRest(TestDownload.class); + ITJUnitUtils.runWithHighwayAndRest(TestExceptionConvertEdge.class); ITJUnitUtils.runWithHighwayAndRest(TestTrace.class); ITJUnitUtils.run(TestTraceEdge.class); diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestExceptionConvertEdge.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestExceptionConvertEdge.java new file mode 100644 index 000000000..d022c2711 --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestExceptionConvertEdge.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.it.testcase; + +import org.apache.servicecomb.it.extend.engine.GateRestTemplate; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; + +public class TestExceptionConvertEdge { + private static GateRestTemplate client = GateRestTemplate.createEdgeRestTemplate("edgeExceptionConvertSchema"); + + @Test + public void testTimeoutAdd() { + int result = client.getForObject("/add?x=10&y=12", Integer.class); + Assert.assertEquals(22, result); + + try { + client.getForObject("/add?x=88&y=21", Object.class); + } catch (RestClientException e) { + HttpClientErrorException exception = (HttpClientErrorException) e; + Assert.assertEquals(HttpStatus.EXPECTATION_FAILED, exception.getStatusCode()); + Assert.assertTrue(exception.getResponseBodyAsString().contains("change the response")); + } + } +} diff --git a/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/converter/CustomException.java b/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/converter/CustomException.java new file mode 100644 index 000000000..e9f7fffec --- /dev/null +++ b/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/converter/CustomException.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.it.edge.converter; + +public class CustomException { + private String data; + + private int statusCode; + + public CustomException(String data, int statusCode) { + this.data = data; + this.statusCode = statusCode; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + @Override + public String toString() { + return "CustomException{" + + "data='" + data + '\'' + + ", statusCode=" + statusCode + + '}'; + } +} diff --git a/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/converter/CustomExceptionConverter.java b/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/converter/CustomExceptionConverter.java new file mode 100644 index 000000000..8678f4c93 --- /dev/null +++ b/integration-tests/it-edge/src/main/java/org/apache/servicecomb/it/edge/converter/CustomExceptionConverter.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.it.edge.converter; + +import java.util.concurrent.TimeoutException; + +import javax.ws.rs.core.Response.Status; + +import org.apache.servicecomb.swagger.invocation.Response; +import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; +import org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; + +public class CustomExceptionConverter implements ExceptionToProducerResponseConverter<TimeoutException> { + @Override + public Class<TimeoutException> getExceptionClass() { + return TimeoutException.class; + } + + @Override + public int getOrder() { + return -12; + } + + @Override + public Response convert(SwaggerInvocation swaggerInvocation, TimeoutException e) { + CustomException customException = new CustomException("change the response", 777); + InvocationException stt = new InvocationException(Status.EXPECTATION_FAILED, customException); + return Response.failResp(stt); + } +} diff --git a/integration-tests/it-edge/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter b/integration-tests/it-edge/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter new file mode 100644 index 000000000..2b858b4f4 --- /dev/null +++ b/integration-tests/it-edge/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +org.apache.servicecomb.it.edge.converter.CustomExceptionConverter \ No newline at end of file diff --git a/integration-tests/it-edge/src/main/resources/microservice.yaml b/integration-tests/it-edge/src/main/resources/microservice.yaml index 59b55809b..0d49d44a1 100644 --- a/integration-tests/it-edge/src/main/resources/microservice.yaml +++ b/integration-tests/it-edge/src/main/resources/microservice.yaml @@ -124,3 +124,9 @@ servicecomb: path: "/url/business/v2/.*" microserviceName: business versionRule: 2.0.0-3.0.0 + request: + it-producer: + edgeExceptionConvertSchema: + timeout: 30000 + add: + timeout: 1000 diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/EdgeExceptionConvertSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/EdgeExceptionConvertSchema.java new file mode 100644 index 000000000..4e8031e1d --- /dev/null +++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/EdgeExceptionConvertSchema.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.it.schema; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@RestSchema(schemaId = "edgeExceptionConvertSchema") +@RequestMapping(path = "/v1/edgeExceptionConvertSchema") +public class EdgeExceptionConvertSchema { + @RequestMapping(path = "/add", method = RequestMethod.GET) + public int add(int x, int y) { + if (x == 88) { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return x + y; + } +} diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java index a64405ed5..3dbd0cccc 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java @@ -33,6 +33,7 @@ @Override public Response convert(SwaggerInvocation swaggerInvocation, Throwable e) { LOGGER.error("invoke failed, invocation={}", swaggerInvocation.getInvocationQualifiedName(), e); - return Response.producerFailResp(e); + //not only producer but also consumer + return Response.failResp(swaggerInvocation.getInvocationType(), e); } } diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java index 1e5b55515..6b2dd7305 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java @@ -43,6 +43,7 @@ import org.apache.servicecomb.serviceregistry.api.Const; import org.apache.servicecomb.swagger.invocation.AsyncResponse; import org.apache.servicecomb.swagger.invocation.Response; +import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; @@ -243,7 +244,8 @@ protected void fail(Throwable e) { } stageTrace.finishClientFiltersResponse(); - asyncResp.fail(invocation.getInvocationType(), e); + //convert exception + asyncResp.handle(ExceptionFactory.convertExceptionToResponse(invocation, e)); } protected void setCseContext() { ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > edge support Exception converter > --------------------------------- > > Key: SCB-1060 > URL: https://issues.apache.org/jira/browse/SCB-1060 > Project: Apache ServiceComb > Issue Type: Bug > Reporter: 何一乐 > Priority: Major > -- This message was sent by Atlassian JIRA (v7.6.3#76005)