This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push: new dfe9f42 [SCB-2159] add TimeoutExceptionConverter (#2120) dfe9f42 is described below commit dfe9f42029f410e9ce3dddf0e824edfe5097b68e Author: wujimin <wuji...@huawei.com> AuthorDate: Thu Dec 10 09:23:27 2020 +0800 [SCB-2159] add TimeoutExceptionConverter (#2120) --- .../converter/TimeoutExceptionConverter.java | 51 ++++++++++++++++++++++ ...e.servicecomb.core.exception.ExceptionConverter | 1 + 2 files changed, 52 insertions(+) diff --git a/core/src/main/java/org/apache/servicecomb/core/exception/converter/TimeoutExceptionConverter.java b/core/src/main/java/org/apache/servicecomb/core/exception/converter/TimeoutExceptionConverter.java new file mode 100644 index 0000000..cee170d --- /dev/null +++ b/core/src/main/java/org/apache/servicecomb/core/exception/converter/TimeoutExceptionConverter.java @@ -0,0 +1,51 @@ +/* + * 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.core.exception.converter; + +import static javax.ws.rs.core.Response.Status.REQUEST_TIMEOUT; + +import java.util.concurrent.TimeoutException; + +import javax.annotation.Nullable; +import javax.ws.rs.core.Response.StatusType; + +import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.exception.ExceptionConverter; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; + +public class TimeoutExceptionConverter implements ExceptionConverter<TimeoutException> { + public static final int ORDER = Short.MAX_VALUE; + + @Override + public int getOrder() { + return ORDER; + } + + @Override + public boolean canConvert(Throwable throwable) { + return throwable instanceof TimeoutException; + } + + @Override + public InvocationException convert(@Nullable Invocation invocation, TimeoutException throwable, + StatusType genericStatus) { + String message = String.format("Request Timeout. Details: %s", throwable.getMessage()); + return new InvocationException(REQUEST_TIMEOUT, + ExceptionConverter.getGenericCode(genericStatus), message, + throwable); + } +} diff --git a/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.exception.ExceptionConverter b/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.exception.ExceptionConverter index aea2c1c..3e3c92a 100644 --- a/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.exception.ExceptionConverter +++ b/core/src/main/resources/META-INF/services/org.apache.servicecomb.core.exception.ExceptionConverter @@ -18,4 +18,5 @@ org.apache.servicecomb.core.exception.converter.ConstraintViolationExceptionConverter org.apache.servicecomb.core.exception.converter.InvocationExceptionConverter org.apache.servicecomb.core.exception.converter.IllegalArgumentExceptionConverter +org.apache.servicecomb.core.exception.converter.TimeoutExceptionConverter org.apache.servicecomb.core.exception.converter.DefaultExceptionConverter \ No newline at end of file