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 */
018package org.apache.oozie.action;
019
020import org.apache.oozie.util.ParamChecker;
021import org.apache.oozie.util.XLog;
022
023/**
024 * ActionExecutor exception. <p/> The exception provides information regarding the transient/no-transient/fatal nature
025 * of the exception.
026 */
027public class ActionExecutorException extends Exception {
028
029    /**
030     * Enum that defines the type of error an {@link ActionExecutor} has produced.
031     */
032    public static enum ErrorType {
033
034        /**
035         * The action will be automatically retried by Oozie.
036         */
037        TRANSIENT,
038
039        /**
040         * The job in set in SUSPEND mode and it will wait for an admin to resume the job.
041         */
042
043        NON_TRANSIENT,
044
045        /**
046         * The action completes with an error transition.
047         */
048        ERROR,
049
050        /**
051         * The action fails. No transition is taken.
052         */
053        FAILED
054    }
055
056    private ErrorType errorType;
057    private String errorCode;
058
059    /**
060     * Create an action executor exception.
061     *
062     * @param errorType the error type.
063     * @param errorCode the error code.
064     * @param message the error message.
065     */
066    public ActionExecutorException(ErrorType errorType, String errorCode, String message) {
067        super(message);
068        this.errorType = ParamChecker.notNull(errorType, "errorType");
069        this.errorCode = ParamChecker.notEmpty(errorCode, "errorCode");
070    }
071
072    /**
073     * Create an action executor exception.
074     *
075     * <p/> If the last parameter is an Exception it is used as the exception cause.
076     *
077     * @param errorType the error type.
078     * @param errorCode the error code.
079     * @param messageTemplate the error message.
080     * @param params parameters used to create the exception message together with the messageTemplate. If the last
081     * parameter is an Exception it is used as the exception cause.
082     */
083    public ActionExecutorException(ErrorType errorType, String errorCode, String messageTemplate, Object... params) {
084        super(errorCode + ": " + XLog.format(messageTemplate, params), XLog.getCause(params));
085        this.errorType = ParamChecker.notNull(errorType, "errorType");
086        this.errorCode = ParamChecker.notEmpty(errorCode, "errorCode");
087    }
088
089    /**
090     * Return the error type of the exception.
091     *
092     * @return the error type of the exception.
093     */
094    public ErrorType getErrorType() {
095        return errorType;
096    }
097
098    /**
099     * Return the error code of the exception.
100     *
101     * @return the error code of the exception.
102     */
103    public String getErrorCode() {
104        return errorCode;
105    }
106}