001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.runtime.base.lang.exception;
017
018import java.lang.reflect.InvocationTargetException;
019import java.util.Collection;
020import java.util.HashSet;
021import java.util.Objects;
022import java.util.Set;
023import java.util.stream.Stream;
024
025import org.talend.sdk.component.api.exception.ComponentException;
026import org.talend.sdk.component.api.exception.DiscoverSchemaException;
027
028public class InvocationExceptionWrapper {
029
030    /**
031     * Wrap the target exception in a Runtime exception
032     *
033     * @param e the exception to wrap in a way which will remove classloader specific exceptions.
034     */
035    public static RuntimeException toRuntimeException(final InvocationTargetException e) {
036        final Set<Throwable> visited = new HashSet<>();
037        visited.add(e.getTargetException());
038        return mapException(e.getTargetException(), visited);
039    }
040
041    private static RuntimeException mapException(final Throwable targetException, final Collection<Throwable> visited) {
042        if (targetException == null) {
043            return null;
044        }
045        if (ComponentException.class.isInstance(targetException)) {
046            return ComponentException.class.cast(targetException);
047        }
048        if (DiscoverSchemaException.class.isInstance(targetException)) {
049            return DiscoverSchemaException.class.cast(targetException);
050        }
051        if (RuntimeException.class.isInstance(targetException)
052                && targetException.getClass().getName().startsWith("java.")) {
053            final RuntimeException cast = RuntimeException.class.cast(targetException);
054            if (cast.getCause() == null
055                    || (cast.getCause() != null && cast.getCause().getClass().getName().startsWith("java."))) {
056                return cast;
057            } // else, let it be wrapped to ensure all the stack is serializable
058        }
059        final ComponentException exception = new ComponentException(targetException.getClass().getName(),
060                targetException.getMessage(), targetException.getStackTrace(), mapCause(targetException, visited));
061        if (exception.getSuppressed() != null && exception.getSuppressed().length > 0) {
062            Stream
063                    .of(exception.getSuppressed())
064                    .map(it -> mapCause(it, new HashSet<>()))
065                    .filter(Objects::nonNull)
066                    .forEach(exception::addSuppressed);
067        }
068        return exception;
069    }
070
071    private static Throwable mapCause(final Throwable targetException, final Collection<Throwable> visited) {
072        final Throwable cause = targetException.getCause();
073        if (cause == null || !visited.add(cause)) {
074            return null;
075        }
076        return mapException(cause, visited);
077    }
078}