001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie;
016    
017    import org.apache.oozie.util.XLog;
018    import org.apache.oozie.util.ParamChecker;
019    
020    /**
021     * Base exception for all Oozie exception. <p/> It requires error codes an captures the Log info at exception time. <p/>
022     * Error codes should be modeled in subclasses as Enums.
023     */
024    public class XException extends Exception {
025        private ErrorCode errorCode;
026    
027        /**
028         * Private constructor use by the public constructors.
029         *
030         * @param message error message.
031         * @param errorCode error code.
032         * @param cause exception cause.
033         */
034        private XException(String message, ErrorCode errorCode, Throwable cause) {
035            super(message, cause);
036            this.errorCode = ParamChecker.notNull(errorCode, "errorCode");
037        }
038    
039        /**
040         * Create an XException from a XException.
041         *
042         * @param cause the XException cause.
043         */
044        public XException(XException cause) {
045            this(cause.getMessage(), cause.getErrorCode(), cause);
046        }
047    
048        /**
049         * Create an EXception from an error code plus parameter to create the exception message. <p/> The value of {@link
050         * ErrorCode#getTemplate} is used as a StringFormat template for the exception message. <p/> If the last parameter
051         * is an Exception it is used as the exception cause.
052         *
053         * @param errorCode the error code for the exception.
054         * @param params parameters used to create the exception message together with the error code template. If the last
055         * parameter is an Exception it is used as the exception cause.
056         */
057        public XException(ErrorCode errorCode, Object... params) {
058            this(errorCode.format(params), errorCode, XLog.getCause(params));
059        }
060    
061        /**
062         * Return the error code of the exception.
063         *
064         * @return exception error code.
065         */
066        public ErrorCode getErrorCode() {
067            return errorCode;
068        }
069    
070    }