You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2021/04/02 00:37:40 UTC

[GitHub] [servicecomb-java-chassis] wujimin opened a new pull request #2342: [SCB-2251] make exception processor extendable

wujimin opened a new pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342


   Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/SCB) filed for the change (usually before you start working on it).  Trivial changes like typos do not require a JIRA issue.  Your pull request should address just this issue, without pulling in other changes.
    - [ ] Each commit in the pull request should have a meaningful subject line and body.
    - [ ] Format the pull request title like `[SCB-XXX] Fixes bug in ApproximateQuantiles`, where you replace `SCB-XXX` with the appropriate JIRA issue.
    - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [ ] Run `mvn clean install -Pit` to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   ---
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
wujimin commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606030955



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] liubao68 commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
liubao68 commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606025764



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"
+      },
+      defaultValue = "false")
+  protected boolean printStackTrace;
+
+  @InjectProperty(keys = "print-rate-limit", defaultValue = "false")
+  protected boolean printRateLimit;
+
+  private final Map<Class<?>, ExceptionConverter<Throwable>> converterCache = new ConcurrentHashMapEx<>();
+
+  @Override
+  public int getOrder() {
+    return ORDER;
+  }
+
+  @Override
+  public boolean isPrintStackTrace() {
+    return printStackTrace;
+  }
+
+  public DefaultExceptionProcessor setPrintStackTrace(boolean printStackTrace) {
+    this.printStackTrace = printStackTrace;
+    return this;
+  }
+
+  public boolean isPrintRateLimit() {
+    return printRateLimit;
+  }
+
+  public DefaultExceptionProcessor setPrintRateLimit(boolean printRateLimit) {
+    this.printRateLimit = printRateLimit;
+    return this;
+  }
+
+  @SuppressWarnings("unchecked")
+  @Autowired(required = false)
+  public DefaultExceptionProcessor setConverters(List<ExceptionConverter<? extends Throwable>> converters) {
+    converters.forEach(converter -> this.converters.add((ExceptionConverter<Throwable>) converter));
+    this.converters.sort(Comparator.comparingInt(ExceptionConverter::getOrder));
+    this.converterCache.clear();
+    return this;
+  }
+
+  @Override
+  public InvocationException convert(@Nonnull Invocation invocation, Throwable throwable) {
+    StatusType genericStatus = CONSUMER.equals(invocation.getInvocationType()) ? BAD_REQUEST : INTERNAL_SERVER_ERROR;
+    return convert(invocation, throwable, genericStatus);
+  }
+
+  @Override
+  public InvocationException convert(@Nullable Invocation invocation, Throwable throwable, StatusType genericStatus) {
+    Throwable unwrapped = ExceptionFactory.unwrap(throwable);
+    try {
+      return converterCache.computeIfAbsent(unwrapped.getClass(), clazz -> findConverter(unwrapped))
+          .convert(invocation, unwrapped, genericStatus);
+    } catch (Exception e) {
+      LOGGER.error("BUG: ExceptionConverter.convert MUST not throw exception, please fix it.\n"
+              + "original exception :{}"
+              + "converter exception:{}",

Review comment:
       add blank `converter exception` -> `converter exception `




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
wujimin commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606027699



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"

Review comment:
       servicecomb.exception.invocation.print-stack-trace is for compatible  
   
   others planed same to 
   ```
   sevicecomb.invocation.timeout.check.enabled
   sevicecomb.invocation.timeout.check.strategy
   sevicecomb.invocation.exception.print-stack-trace
   sevicecomb.invocation.exception.print-rate-limit
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] liubao68 commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
liubao68 commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606026048



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"

Review comment:
       why add two configs? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] liubao68 merged pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
liubao68 merged pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
wujimin commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606028907



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"

Review comment:
       not released, no need to make compatible




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] wujimin commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
wujimin commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606027335



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"
+      },
+      defaultValue = "false")
+  protected boolean printStackTrace;
+
+  @InjectProperty(keys = "print-rate-limit", defaultValue = "false")
+  protected boolean printRateLimit;
+
+  private final Map<Class<?>, ExceptionConverter<Throwable>> converterCache = new ConcurrentHashMapEx<>();
+
+  @Override
+  public int getOrder() {
+    return ORDER;
+  }
+
+  @Override
+  public boolean isPrintStackTrace() {
+    return printStackTrace;
+  }
+
+  public DefaultExceptionProcessor setPrintStackTrace(boolean printStackTrace) {
+    this.printStackTrace = printStackTrace;
+    return this;
+  }
+
+  public boolean isPrintRateLimit() {
+    return printRateLimit;
+  }
+
+  public DefaultExceptionProcessor setPrintRateLimit(boolean printRateLimit) {
+    this.printRateLimit = printRateLimit;
+    return this;
+  }
+
+  @SuppressWarnings("unchecked")
+  @Autowired(required = false)
+  public DefaultExceptionProcessor setConverters(List<ExceptionConverter<? extends Throwable>> converters) {
+    converters.forEach(converter -> this.converters.add((ExceptionConverter<Throwable>) converter));
+    this.converters.sort(Comparator.comparingInt(ExceptionConverter::getOrder));
+    this.converterCache.clear();
+    return this;
+  }
+
+  @Override
+  public InvocationException convert(@Nonnull Invocation invocation, Throwable throwable) {
+    StatusType genericStatus = CONSUMER.equals(invocation.getInvocationType()) ? BAD_REQUEST : INTERNAL_SERVER_ERROR;
+    return convert(invocation, throwable, genericStatus);
+  }
+
+  @Override
+  public InvocationException convert(@Nullable Invocation invocation, Throwable throwable, StatusType genericStatus) {
+    Throwable unwrapped = ExceptionFactory.unwrap(throwable);
+    try {
+      return converterCache.computeIfAbsent(unwrapped.getClass(), clazz -> findConverter(unwrapped))
+          .convert(invocation, unwrapped, genericStatus);
+    } catch (Exception e) {
+      LOGGER.error("BUG: ExceptionConverter.convert MUST not throw exception, please fix it.\n"
+              + "original exception :{}"
+              + "converter exception:{}",

Review comment:
       to align main content of original and converter




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-java-chassis] liubao68 commented on a change in pull request #2342: [SCB-2251] make exception processor extendable

Posted by GitBox <gi...@apache.org>.
liubao68 commented on a change in pull request #2342:
URL: https://github.com/apache/servicecomb-java-chassis/pull/2342#discussion_r606025764



##########
File path: core/src/main/java/org/apache/servicecomb/core/exception/DefaultExceptionProcessor.java
##########
@@ -0,0 +1,206 @@
+/*
+ * 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;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS;
+import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
+import static org.apache.servicecomb.core.exception.ExceptionCodes.GENERIC_SERVER;
+import static org.apache.servicecomb.swagger.invocation.InvocationType.CONSUMER;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.Response.StatusType;
+
+import org.apache.servicecomb.config.inject.InjectProperties;
+import org.apache.servicecomb.config.inject.InjectProperty;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@InjectProperties(prefix = "servicecomb.invocation.exception")
+public class DefaultExceptionProcessor implements ExceptionProcessor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionProcessor.class);
+
+  public static final int ORDER = Integer.MAX_VALUE;
+
+  @SuppressWarnings("unchecked")
+  private final List<ExceptionConverter<Throwable>> converters = SPIServiceUtils
+      .getOrLoadSortedService(ExceptionConverter.class).stream()
+      .map(converter -> (ExceptionConverter<Throwable>) converter)
+      .collect(Collectors.toList());
+
+  @InjectProperty(
+      prefix = "servicecomb",
+      keys = {
+          "invocation.exception.print-stack-trace",
+          "exception.invocation.print-stack-trace"
+      },
+      defaultValue = "false")
+  protected boolean printStackTrace;
+
+  @InjectProperty(keys = "print-rate-limit", defaultValue = "false")
+  protected boolean printRateLimit;
+
+  private final Map<Class<?>, ExceptionConverter<Throwable>> converterCache = new ConcurrentHashMapEx<>();
+
+  @Override
+  public int getOrder() {
+    return ORDER;
+  }
+
+  @Override
+  public boolean isPrintStackTrace() {
+    return printStackTrace;
+  }
+
+  public DefaultExceptionProcessor setPrintStackTrace(boolean printStackTrace) {
+    this.printStackTrace = printStackTrace;
+    return this;
+  }
+
+  public boolean isPrintRateLimit() {
+    return printRateLimit;
+  }
+
+  public DefaultExceptionProcessor setPrintRateLimit(boolean printRateLimit) {
+    this.printRateLimit = printRateLimit;
+    return this;
+  }
+
+  @SuppressWarnings("unchecked")
+  @Autowired(required = false)
+  public DefaultExceptionProcessor setConverters(List<ExceptionConverter<? extends Throwable>> converters) {
+    converters.forEach(converter -> this.converters.add((ExceptionConverter<Throwable>) converter));
+    this.converters.sort(Comparator.comparingInt(ExceptionConverter::getOrder));
+    this.converterCache.clear();
+    return this;
+  }
+
+  @Override
+  public InvocationException convert(@Nonnull Invocation invocation, Throwable throwable) {
+    StatusType genericStatus = CONSUMER.equals(invocation.getInvocationType()) ? BAD_REQUEST : INTERNAL_SERVER_ERROR;
+    return convert(invocation, throwable, genericStatus);
+  }
+
+  @Override
+  public InvocationException convert(@Nullable Invocation invocation, Throwable throwable, StatusType genericStatus) {
+    Throwable unwrapped = ExceptionFactory.unwrap(throwable);
+    try {
+      return converterCache.computeIfAbsent(unwrapped.getClass(), clazz -> findConverter(unwrapped))
+          .convert(invocation, unwrapped, genericStatus);
+    } catch (Exception e) {
+      LOGGER.error("BUG: ExceptionConverter.convert MUST not throw exception, please fix it.\n"
+              + "original exception :{}"
+              + "converter exception:{}",

Review comment:
       add blank `[converter exception]` -> `[converter exception ]`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org