[ https://issues.apache.org/jira/browse/SCB-1014?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16680770#comment-16680770 ]
ASF GitHub Bot commented on SCB-1014: ------------------------------------- yhs0092 closed pull request #985: [SCB-1014] Fix priority of ExceptionToResponseConverter URL: https://github.com/apache/servicecomb-java-chassis/pull/985 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/demo/demo-edge/business-2.0.0/src/main/java/org/apache/servicecomb/demo/edge/business/error/CustomExceptionToResponseConverter.java b/demo/demo-edge/business-2.0.0/src/main/java/org/apache/servicecomb/demo/edge/business/error/CustomExceptionToProducerResponseConverter.java similarity index 91% rename from demo/demo-edge/business-2.0.0/src/main/java/org/apache/servicecomb/demo/edge/business/error/CustomExceptionToResponseConverter.java rename to demo/demo-edge/business-2.0.0/src/main/java/org/apache/servicecomb/demo/edge/business/error/CustomExceptionToProducerResponseConverter.java index fd2ccd90f..9894b6d37 100644 --- a/demo/demo-edge/business-2.0.0/src/main/java/org/apache/servicecomb/demo/edge/business/error/CustomExceptionToResponseConverter.java +++ b/demo/demo-edge/business-2.0.0/src/main/java/org/apache/servicecomb/demo/edge/business/error/CustomExceptionToProducerResponseConverter.java @@ -21,10 +21,11 @@ import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -import org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter; +import org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; -public class CustomExceptionToResponseConverter implements ExceptionToResponseConverter<IllegalStateException> { +public class CustomExceptionToProducerResponseConverter implements + ExceptionToProducerResponseConverter<IllegalStateException> { @Override public Class<IllegalStateException> getExceptionClass() { return IllegalStateException.class; diff --git a/demo/demo-edge/business-2.0.0/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/demo/demo-edge/business-2.0.0/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter similarity index 97% rename from demo/demo-edge/business-2.0.0/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter rename to demo/demo-edge/business-2.0.0/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter index 9a433318c..1633f6968 100644 --- a/demo/demo-edge/business-2.0.0/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter +++ b/demo/demo-edge/business-2.0.0/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter @@ -15,4 +15,4 @@ # limitations under the License. # -org.apache.servicecomb.demo.edge.business.error.CustomExceptionToResponseConverter \ No newline at end of file +org.apache.servicecomb.demo.edge.business.error.CustomExceptionToProducerResponseConverter \ No newline at end of file diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java similarity index 89% rename from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToResponseConverter.java rename to swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java index 03d4cb392..a64405ed5 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/DefaultExceptionToProducerResponseConverter.java @@ -21,8 +21,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class DefaultExceptionToResponseConverter implements ExceptionToResponseConverter<Throwable> { - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionToResponseConverter.class); +public class DefaultExceptionToProducerResponseConverter implements ExceptionToProducerResponseConverter<Throwable> { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionToProducerResponseConverter.class); @Override public Class<Throwable> getExceptionClass() { diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java index 7b9757e7f..7bdd7f7cc 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java @@ -44,7 +44,7 @@ public static final String CONSUMER_INNER_REASON_PHRASE = "Cse Internal Bad Request"; - private static ExceptionToResponseConverters exceptionToResponseConverters = new ExceptionToResponseConverters(); + private static ExceptionToProducerResponseConverters exceptionToProducerResponseConverters = new ExceptionToProducerResponseConverters(); public static final StatusType CONSUMER_INNER_STATUS = new HttpStatus(CONSUMER_INNER_STATUS_CODE, CONSUMER_INNER_REASON_PHRASE); @@ -125,6 +125,6 @@ protected static InvocationException convertException(int statusCode, String rea } public static Response convertExceptionToResponse(SwaggerInvocation swaggerInvocation, Throwable e) { - return exceptionToResponseConverters.convertExceptionToResponse(swaggerInvocation, e); + return exceptionToProducerResponseConverters.convertExceptionToResponse(swaggerInvocation, e); } } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToProducerResponseConverter.java similarity index 93% rename from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java rename to swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToProducerResponseConverter.java index 4c14ad87f..a74b4714a 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToProducerResponseConverter.java @@ -19,7 +19,7 @@ import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -public interface ExceptionToResponseConverter<T extends Throwable> { +public interface ExceptionToProducerResponseConverter<T extends Throwable> { Class<T> getExceptionClass(); default int getOrder() { diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToProducerResponseConverters.java similarity index 64% rename from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java rename to swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToProducerResponseConverters.java index 997b75c75..177206f53 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionToProducerResponseConverters.java @@ -23,15 +23,19 @@ import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -public class ExceptionToResponseConverters { - private Map<Class<?>, ExceptionToResponseConverter<Throwable>> exceptionToResponseConverters = +public class ExceptionToProducerResponseConverters { + private Map<Class<?>, ExceptionToProducerResponseConverter<Throwable>> exceptionToProducerResponseConverters = new HashMap<>(); - private ExceptionToResponseConverter<Throwable> defaultConverter; + private ExceptionToProducerResponseConverter<Throwable> defaultConverter; + /** + * Load the {@link ExceptionToProducerResponseConverter} implementations. Ensure that those converters whose {@link ExceptionToProducerResponseConverter#getOrder()} + * return smaller value takes higher priority. + */ @SuppressWarnings("unchecked") - public ExceptionToResponseConverters() { - SPIServiceUtils.getSortedService(ExceptionToResponseConverter.class).forEach(converter -> { + public ExceptionToProducerResponseConverters() { + SPIServiceUtils.getSortedService(ExceptionToProducerResponseConverter.class).forEach(converter -> { if (converter.getExceptionClass() == null) { if (defaultConverter == null) { defaultConverter = converter; @@ -39,15 +43,17 @@ public ExceptionToResponseConverters() { return; } - exceptionToResponseConverters.put(converter.getExceptionClass(), converter); + if (!exceptionToProducerResponseConverters.containsKey(converter.getExceptionClass())) { + exceptionToProducerResponseConverters.put(converter.getExceptionClass(), converter); + } }); } public Response convertExceptionToResponse(SwaggerInvocation swaggerInvocation, Throwable e) { - ExceptionToResponseConverter<Throwable> converter = null; + ExceptionToProducerResponseConverter<Throwable> converter = null; Class<?> clazz = e.getClass(); while (converter == null) { - converter = exceptionToResponseConverters.get(clazz); + converter = exceptionToProducerResponseConverters.get(clazz); if (clazz == Throwable.class) { break; } diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationExceptionToProducerResponseConverter.java similarity index 90% rename from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationExceptionToResponseConverter.java rename to swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationExceptionToProducerResponseConverter.java index 368b114a1..2c3ddfd4b 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationExceptionToProducerResponseConverter.java @@ -19,7 +19,8 @@ import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -public class InvocationExceptionToResponseConverter implements ExceptionToResponseConverter<InvocationException> { +public class InvocationExceptionToProducerResponseConverter implements + ExceptionToProducerResponseConverter<InvocationException> { @Override public Class<InvocationException> getExceptionClass() { return InvocationException.class; diff --git a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter similarity index 93% rename from swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter rename to swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter index b717491e6..f7315d67c 100644 --- a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter +++ b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter @@ -15,5 +15,5 @@ # limitations under the License. # -org.apache.servicecomb.swagger.invocation.exception.InvocationExceptionToResponseConverter -org.apache.servicecomb.swagger.invocation.exception.DefaultExceptionToResponseConverter +org.apache.servicecomb.swagger.invocation.exception.InvocationExceptionToProducerResponseConverter +org.apache.servicecomb.swagger.invocation.exception.DefaultExceptionToProducerResponseConverter diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/ErrorToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/ErrorToProducerResponseConverter.java similarity index 92% rename from swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/ErrorToResponseConverter.java rename to swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/ErrorToProducerResponseConverter.java index 241869a40..87eaae408 100644 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/ErrorToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/ErrorToProducerResponseConverter.java @@ -21,7 +21,7 @@ import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -public class ErrorToResponseConverter implements ExceptionToResponseConverter<Error> { +public class ErrorToProducerResponseConverter implements ExceptionToProducerResponseConverter<Error> { @Override public Class<Error> getExceptionClass() { return Error.class; diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestDefaultExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestDefaultExceptionToProducerResponseConverter.java similarity index 89% rename from swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestDefaultExceptionToResponseConverter.java rename to swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestDefaultExceptionToProducerResponseConverter.java index 9fbe32da4..c90d5e7e3 100644 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestDefaultExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestDefaultExceptionToProducerResponseConverter.java @@ -23,8 +23,8 @@ import mockit.Mocked; -public class TestDefaultExceptionToResponseConverter { - DefaultExceptionToResponseConverter converter = new DefaultExceptionToResponseConverter(); +public class TestDefaultExceptionToProducerResponseConverter { + DefaultExceptionToProducerResponseConverter converter = new DefaultExceptionToProducerResponseConverter(); @Test public void getExceptionClass() { diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestExceptionToProducerResponseConverters.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestExceptionToProducerResponseConverters.java new file mode 100644 index 000000000..319e41153 --- /dev/null +++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestExceptionToProducerResponseConverters.java @@ -0,0 +1,189 @@ +/* + * 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.swagger.invocation.exception; + +import java.io.IOException; +import java.util.Arrays; + +import javax.ws.rs.core.Response.Status; + +import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; +import org.apache.servicecomb.swagger.invocation.Response; +import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; +import org.junit.Assert; +import org.junit.Test; + +import mockit.Expectations; +import mockit.Mocked; + +public class TestExceptionToProducerResponseConverters { + @Test + public void convertExceptionToResponse( + @Mocked ExceptionToProducerResponseConverter<Throwable> c1, + @Mocked Response r1, + @Mocked ExceptionToProducerResponseConverter<Throwable> c2, + @Mocked Response r2, + @Mocked ExceptionToProducerResponseConverter<Throwable> cDef) { + new Expectations(SPIServiceUtils.class) { + { + SPIServiceUtils.getSortedService(ExceptionToProducerResponseConverter.class); + result = Arrays.asList(c1, c2, cDef); + + c1.getExceptionClass(); + result = Throwable.class; + c1.convert((SwaggerInvocation) any, (Throwable) any); + result = r1; + + c2.getExceptionClass(); + result = Exception.class; + c2.convert((SwaggerInvocation) any, (Throwable) any); + result = r2; + + cDef.getExceptionClass(); + result = null; + } + }; + + ExceptionToProducerResponseConverters exceptionToProducerResponseConverters = new ExceptionToProducerResponseConverters(); + + Assert.assertSame(r1, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new Throwable())); + Assert.assertSame(r2, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new Exception())); + Assert.assertSame(r2, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, + new IllegalStateException())); + } + + @Test + public void convertExceptionToResponse_checkDefaultConverterPriority( + @Mocked ExceptionToProducerResponseConverter<Throwable> c1, + @Mocked Response r1, + @Mocked ExceptionToProducerResponseConverter<Throwable> c2, + @Mocked Response r2, + @Mocked ExceptionToProducerResponseConverter<Throwable> cDef, + @Mocked Response rDef, + @Mocked ExceptionToProducerResponseConverter<Throwable> cDef2) { + new Expectations(SPIServiceUtils.class) { + { + SPIServiceUtils.getSortedService(ExceptionToProducerResponseConverter.class); + result = Arrays.asList(c1, c2, cDef, cDef2); + + c1.getExceptionClass(); + result = RuntimeException.class; + c1.convert((SwaggerInvocation) any, (Throwable) any); + result = r1; + + c2.getExceptionClass(); + result = InvocationException.class; + c2.convert((SwaggerInvocation) any, (Throwable) any); + result = r2; + + cDef.getExceptionClass(); + result = null; + cDef.convert((SwaggerInvocation) any, (Throwable) any); + result = rDef; + + cDef2.getExceptionClass(); + result = null; + } + }; + + ExceptionToProducerResponseConverters exceptionToProducerResponseConverters = new ExceptionToProducerResponseConverters(); + + Assert.assertSame(r2, + exceptionToProducerResponseConverters + .convertExceptionToResponse(null, new InvocationException(Status.UNAUTHORIZED, ""))); + Assert.assertSame(r1, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new RuntimeException())); + Assert.assertSame(rDef, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, + new IOException())); + } + + @Test + public void convertExceptionToResponse_CheckCommonConvertPriority( + @Mocked ExceptionToProducerResponseConverter<RuntimeException0> cR0, + @Mocked ExceptionToProducerResponseConverter<RuntimeException0> cR0_LowPriority, + @Mocked ExceptionToProducerResponseConverter<RuntimeException1> cR1, + @Mocked ExceptionToProducerResponseConverter<RuntimeException> cR, + @Mocked ExceptionToProducerResponseConverter<Throwable> cT, + @Mocked ExceptionToProducerResponseConverter<?> cDef, + @Mocked Response rR0, + @Mocked Response rR1, + @Mocked Response rR, + @Mocked Response rT) { + new Expectations(SPIServiceUtils.class) { + { + SPIServiceUtils.getSortedService(ExceptionToProducerResponseConverter.class); + result = Arrays.asList(cR, cR0, cR0_LowPriority, cR1, cDef, cT); + + cR0.getExceptionClass(); + result = RuntimeException0.class; + cR0.convert((SwaggerInvocation) any, (RuntimeException0) any); + result = rR0; + + cR0_LowPriority.getExceptionClass(); + result = RuntimeException0.class; + + cR1.getExceptionClass(); + result = RuntimeException1.class; + cR1.convert((SwaggerInvocation) any, (RuntimeException1) any); + result = rR1; + + cR.getExceptionClass(); + result = RuntimeException.class; + cR.convert((SwaggerInvocation) any, (RuntimeException) any); + result = rR; + + cT.getExceptionClass(); + result = Throwable.class; + cT.convert((SwaggerInvocation) any, (Throwable) any); + result = rT; + + cDef.getExceptionClass(); + result = null; + } + }; + + ExceptionToProducerResponseConverters exceptionToProducerResponseConverters = new ExceptionToProducerResponseConverters(); + + Assert.assertSame(rR0, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new RuntimeException0_0())); + Assert.assertSame(rR0, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new RuntimeException0())); + Assert.assertSame(rR1, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new RuntimeException1())); + Assert.assertSame(rR, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new RuntimeException())); + // Actually, a Throwable exception converter will act like a default converter, as our implementation expects. + Assert.assertSame(rT, + exceptionToProducerResponseConverters.convertExceptionToResponse(null, new IOException())); + } + + static class RuntimeException0 extends RuntimeException { + private static final long serialVersionUID = -5151948381107463505L; + } + + static class RuntimeException1 extends RuntimeException { + private static final long serialVersionUID = 1752513688353075486L; + } + + static class RuntimeException0_0 extends RuntimeException0 { + private static final long serialVersionUID = -6645187961518504765L; + } +} diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestExceptionToResponseConverters.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestExceptionToResponseConverters.java deleted file mode 100644 index d2d6d6da4..000000000 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestExceptionToResponseConverters.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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.swagger.invocation.exception; - -import java.io.IOException; -import java.util.Arrays; - -import javax.ws.rs.core.Response.Status; - -import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; -import org.apache.servicecomb.swagger.invocation.Response; -import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -import org.junit.Assert; -import org.junit.Test; - -import mockit.Expectations; -import mockit.Mocked; - -public class TestExceptionToResponseConverters { - @SuppressWarnings({"rawtypes", "unchecked"}) - @Test - public void convertExceptionToResponse(@Mocked ExceptionToResponseConverter c1, - @Mocked Response r1, - @Mocked ExceptionToResponseConverter c2, - @Mocked Response r2, - @Mocked ExceptionToResponseConverter cDef) { - new Expectations(SPIServiceUtils.class) { - { - SPIServiceUtils.getSortedService(ExceptionToResponseConverter.class); - result = Arrays.asList(c1, c2, cDef); - - c1.getExceptionClass(); - result = Throwable.class; - c1.convert((SwaggerInvocation) any, (Throwable) any); - result = r1; - - c2.getExceptionClass(); - result = Exception.class; - c2.convert((SwaggerInvocation) any, (Throwable) any); - result = r2; - - cDef.getExceptionClass(); - result = null; - } - }; - - ExceptionToResponseConverters exceptionToResponseConverters = new ExceptionToResponseConverters(); - - Assert.assertSame(r1, - exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, new Throwable())); - Assert.assertSame(r2, - exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, new Exception())); - Assert.assertSame(r2, - exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, - new IllegalStateException())); - } - - @SuppressWarnings({"rawtypes", "unchecked"}) - @Test - public void convertExceptionToResponse2(@Mocked ExceptionToResponseConverter c1, - @Mocked Response r1, - @Mocked ExceptionToResponseConverter c2, - @Mocked Response r2, - @Mocked ExceptionToResponseConverter cDef, - @Mocked Response rDef, - @Mocked ExceptionToResponseConverter cDef2) { - new Expectations(SPIServiceUtils.class) { - { - SPIServiceUtils.getSortedService(ExceptionToResponseConverter.class); - result = Arrays.asList(c1, c2, cDef, cDef2); - - c1.getExceptionClass(); - result = RuntimeException.class; - c1.convert((SwaggerInvocation) any, (Throwable) any); - result = r1; - - c2.getExceptionClass(); - result = InvocationException.class; - c2.convert((SwaggerInvocation) any, (Throwable) any); - result = r2; - - cDef.getExceptionClass(); - result = null; - cDef.convert((SwaggerInvocation) any, (Throwable) any); - result = rDef; - - cDef2.getExceptionClass(); - result = null; - } - }; - - ExceptionToResponseConverters exceptionToResponseConverters = new ExceptionToResponseConverters(); - - Assert.assertSame(r2, - exceptionToResponseConverters - .convertExceptionToResponse((SwaggerInvocation) null, new InvocationException(Status.UNAUTHORIZED, ""))); - Assert.assertSame(r1, - exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, new RuntimeException())); - Assert.assertSame(rDef, - exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, - new IOException())); - } -} diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestInvocationExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestInvocationExceptionToProducerResponseConverter.java similarity index 88% rename from swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestInvocationExceptionToResponseConverter.java rename to swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestInvocationExceptionToProducerResponseConverter.java index 38e66ca15..49dbd59ea 100644 --- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestInvocationExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/exception/TestInvocationExceptionToProducerResponseConverter.java @@ -23,8 +23,8 @@ import mockit.Mocked; -public class TestInvocationExceptionToResponseConverter { - InvocationExceptionToResponseConverter converter = new InvocationExceptionToResponseConverter(); +public class TestInvocationExceptionToProducerResponseConverter { + InvocationExceptionToProducerResponseConverter converter = new InvocationExceptionToProducerResponseConverter(); @Test public void getExceptionClass() { diff --git a/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter similarity index 89% rename from swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter rename to swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter index a7cfecd16..a3afa7714 100644 --- a/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter +++ b/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter @@ -1,18 +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.swagger.invocation.exception.ErrorToResponseConverter \ No newline at end of file +# +# 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.swagger.invocation.exception.ErrorToProducerResponseConverter \ No newline at end of file diff --git a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToProducerResponseConverter.java similarity index 89% rename from swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java rename to swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToProducerResponseConverter.java index 5ade95199..58e3e4e0c 100644 --- a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToResponseConverter.java +++ b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ConstraintViolationExceptionToProducerResponseConverter.java @@ -21,11 +21,11 @@ import org.apache.servicecomb.swagger.invocation.Response; import org.apache.servicecomb.swagger.invocation.SwaggerInvocation; -import org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter; +import org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; -public class ConstraintViolationExceptionToResponseConverter - implements ExceptionToResponseConverter<ConstraintViolationException> { +public class ConstraintViolationExceptionToProducerResponseConverter + implements ExceptionToProducerResponseConverter<ConstraintViolationException> { @Override public Class<ConstraintViolationException> getExceptionClass() { return ConstraintViolationException.class; diff --git a/swagger/swagger-invocation/invocation-validator/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/swagger/swagger-invocation/invocation-validator/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter similarity index 95% rename from swagger/swagger-invocation/invocation-validator/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter rename to swagger/swagger-invocation/invocation-validator/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter index 9667b7a6b..f6d6fd906 100644 --- a/swagger/swagger-invocation/invocation-validator/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter +++ b/swagger/swagger-invocation/invocation-validator/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.invocation.exception.ExceptionToProducerResponseConverter @@ -15,4 +15,4 @@ # limitations under the License. # -org.apache.servicecomb.swagger.invocation.validator.ConstraintViolationExceptionToResponseConverter \ No newline at end of file +org.apache.servicecomb.swagger.invocation.validator.ConstraintViolationExceptionToProducerResponseConverter \ No newline at end of file ---------------------------------------------------------------- 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 > Fix priority problem of ExceptionToResponseConverter > ---------------------------------------------------- > > Key: SCB-1014 > URL: https://issues.apache.org/jira/browse/SCB-1014 > Project: Apache ServiceComb > Issue Type: Bug > Reporter: YaoHaishi > Assignee: YaoHaishi > Priority: Major > > Currently the priority rule of default ExceptionToResponseConverter is that > the smaller the order is, the higher the priority is. But the other common > ExceptionToResponseConverters obey the contrary priority rule. > We need to modify the implementation logic of ExceptionToResponseConverters > to make sure the two kinds of ExceptionToResponseConverters obey the same > priority rule. > And the name of ExceptionToResponseConverter will be changed to remind users > to be aware of the modification. -- This message was sent by Atlassian JIRA (v7.6.3#76005)