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
019package org.apache.oozie;
020
021import org.apache.oozie.util.XLog;
022import org.apache.oozie.util.ParamChecker;
023
024/**
025 * Base exception for all Oozie exception. <p> It requires error codes an captures the Log info at exception time. <p>
026 * Error codes should be modeled in subclasses as Enums.
027 */
028public class XException extends Exception {
029    private ErrorCode errorCode;
030
031    /**
032     * Private constructor use by the public constructors.
033     *
034     * @param message error message.
035     * @param errorCode error code.
036     * @param cause exception cause.
037     */
038    private XException(String message, ErrorCode errorCode, Throwable cause) {
039        super(message, cause);
040        this.errorCode = ParamChecker.notNull(errorCode, "errorCode");
041    }
042
043    /**
044     * Create an XException from a XException.
045     *
046     * @param cause the XException cause.
047     */
048    public XException(XException cause) {
049        this(cause.getMessage(), cause.getErrorCode(), cause);
050    }
051
052    /**
053     * Create an EXception from an error code plus parameter to create the exception message. <p> The value of {@link
054     * ErrorCode#getTemplate} is used as a StringFormat template for the exception message. <p> If the last parameter
055     * is an Exception it is used as the exception cause.
056     *
057     * @param errorCode the error code for the exception.
058     * @param params parameters used to create the exception message together with the error code template. If the last
059     * parameter is an Exception it is used as the exception cause.
060     */
061    public XException(ErrorCode errorCode, Object... params) {
062        this(errorCode.format(params), errorCode, XLog.getCause(params));
063    }
064
065    /**
066     * Return the error code of the exception.
067     *
068     * @return exception error code.
069     */
070    public ErrorCode getErrorCode() {
071        return errorCode;
072    }
073
074}