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