001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
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// --------------------------------------------------------------------------------
016
017package com.echothree.util.common.exception;
018
019import com.echothree.model.control.core.common.CommandMessageTypes;
020import com.echothree.util.common.message.Message;
021import com.echothree.util.server.message.MessageUtils;
022
023/**
024 *  This is the common superclass for all application exceptions. This
025 *  class and its subclasses support the chained exception facility that allows
026 *  a root cause Throwable to be wrapped by this class or one of its
027 *  descendants.
028 */
029
030public abstract class BaseException
031        extends RuntimeException {
032    
033    protected BaseException() {
034        super();
035    }
036    
037    protected BaseException(String message) {
038        super(message);
039    }
040    
041    protected BaseException(Throwable cause) {
042        super(cause);
043    }
044
045    private static String getExceptionMessage(Message message) {
046        String result = null;
047
048        try {
049            // Check if we're running in the app server. If not, a ClassNotFoundException will be thrown.
050            if(Class.forName("com.echothree.util.server.message.MessageUtils") != null) {
051                result = MessageUtils.getInstance().getExceptionMessage(CommandMessageTypes.Error.name(), message);
052            }
053        } catch(ClassNotFoundException cnfe) {
054            result = new StringBuilder("??").append(message.getKey()).append("??").toString();
055        }
056
057        return result;
058    }
059
060    protected BaseException(Message message) {
061        super(getExceptionMessage(message));
062    }
063
064}