arina-ielchiieva commented on a change in pull request #1519: DRILL-6760: Retain original exception in Verbose Error Message URL: https://github.com/apache/drill/pull/1519#discussion_r231496451
########## File path: common/src/main/java/org/apache/drill/common/util/DrillExceptionUtils.java ########## @@ -0,0 +1,96 @@ +/* + * 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.drill.common.util; + +import org.apache.commons.lang3.StringUtils; +import org.apache.drill.exec.proto.UserBitShared; + +import java.lang.reflect.Constructor; +import java.util.Arrays; + +public class DrillExceptionUtils { + static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillExceptionUtils.class); + + public static Throwable getThrowable(UserBitShared.DrillPBError error) { + UserBitShared.ExceptionWrapper wrapper = error.getException(); + return getThrowable(wrapper); + } + + /** + * recreate throwable from exception PB which received from remote or local node. + * if no match ctor found, the base Throwable(Exception or Error) is created as a substitution + * + * @param exceptionWrapper the exception PB + * @return Throwable deserialized from PB + */ + public static Throwable getThrowable(UserBitShared.ExceptionWrapper exceptionWrapper) { + String className = exceptionWrapper.getExceptionClass(); + if (StringUtils.isBlank(className) || exceptionWrapper.getStackTraceCount() < 1) { + return null; + } + Throwable inner = getThrowable(exceptionWrapper.getCause()); + try { + Throwable throwable = getInstance(className, exceptionWrapper.getMessage(), inner); + int size = exceptionWrapper.getStackTraceCount(); + StackTraceElement[] stackTrace = new StackTraceElement[size]; + for (int i = 0; i < size; ++i) { + UserBitShared.StackTraceElementWrapper w = exceptionWrapper.getStackTrace(i); + stackTrace[i] = new StackTraceElement(w.getClassName(), w.getMethodName(), w.getFileName(), w.getLineNumber()); + } + throwable.setStackTrace(stackTrace); + return throwable; + } catch (Throwable t) { + logger.error("Failed to get throwable from PB", t); + return null; + } + } + + private static Throwable getInstance(String className, String message, Throwable inner) throws ReflectiveOperationException { Review comment: Please add java doc. ---------------------------------------------------------------- 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 With regards, Apache Git Services