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 01:19:49 UTC

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

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