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.api.exception;
017
018import lombok.Getter;
019
020public class ComponentException extends RuntimeException {
021
022    public static enum ErrorOrigin {
023        USER, // Error caused by user misconfiguration
024        BACKEND, // Error caused by backend
025        UNKNOWN // Any other error
026    }
027
028    @Getter
029    private final ErrorOrigin errorOrigin;
030
031    @Getter
032    private final String originalType;
033
034    @Getter
035    private final String originalMessage;
036
037    public ComponentException(final ErrorOrigin errorOrigin, final String type, final String message,
038            final StackTraceElement[] stackTrace, final Throwable cause) {
039        super((type != null ? "(" + type + ") " : "") + message);
040        this.initCause(toGenericThrowable(cause));
041        this.errorOrigin = errorOrigin;
042        originalType = type;
043        originalMessage = message;
044        if (stackTrace != null) {
045            setStackTrace(stackTrace);
046        }
047    }
048
049    public ComponentException(final String type, final String message, final StackTraceElement[] stackTrace,
050            final Throwable cause) {
051        this(ErrorOrigin.UNKNOWN, type, message, stackTrace, cause);
052    }
053
054    public ComponentException(final ErrorOrigin errorOrigin, final String message) {
055        this(errorOrigin, message, null);
056    }
057
058    public ComponentException(final ErrorOrigin errorOrigin, final String message, final Throwable cause) {
059        this(errorOrigin, cause != null ? cause.getClass().getName() : null, message, null, cause);
060    }
061
062    public ComponentException(final String message) {
063        this(ErrorOrigin.UNKNOWN, message);
064    }
065
066    public ComponentException(final String message, final Throwable cause) {
067        this(ErrorOrigin.UNKNOWN, message, cause);
068    }
069
070    public ComponentException(final Throwable cause) {
071        this(ErrorOrigin.UNKNOWN, cause.getMessage(), cause);
072    }
073
074    /**
075     * Convert all exception stack to generic throwable stack to avoid unknown exception at deserialization time..
076     * 
077     * @param t
078     * @return An Throwable.
079     */
080    protected Throwable toGenericThrowable(final Throwable t) {
081        if (t == null) {
082            return null;
083        }
084        if (t instanceof ComponentException) {
085            return t;
086        }
087        Throwable generic = new Throwable(String.format("(%s) : %s", t.getClass().getName(), t.getMessage()));
088        generic.setStackTrace(t.getStackTrace());
089
090        Throwable cause = t.getCause();
091        Throwable genericCause = null;
092        if (cause != null) {
093            genericCause = toGenericThrowable(cause);
094        } else {
095            return generic;
096        }
097
098        if (genericCause != null) {
099            generic = new Throwable(generic.getMessage(), genericCause);
100        }
101
102        return generic;
103    }
104
105}