001/*
002 *  Copyright 2018 Anyware Services
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 */
016package org.ametys.plugins.linkdirectory.dynamic;
017
018/**
019 * Common exception for dynamic information
020 */
021public class DynamicInformationException extends RuntimeException
022{
023    /**
024     * Enum for type of exception
025     */
026    public enum ExceptionType
027    {
028        /** The connection is not authorized */
029        UNAUTHORIZED,
030        /** Connection timed out */
031        TIMEOUT,
032        /** Configuration exception (unknown host, host unreachable, 404, ...) */
033        CONFIGURATION_EXCEPTION,
034        /** Nothing defined */
035        UNKNOWN;
036    }
037    
038    private ExceptionType _type = ExceptionType.UNKNOWN;
039    
040    private boolean _silent;
041    
042    /**
043     * Constructs a new exception with the specified detail message.
044     * @param message the detail message.
045     * @param type reason of the exception
046     */
047    public DynamicInformationException(String message, ExceptionType type)
048    {
049        this(message, type, null, true);
050    }
051
052    /**
053     * Constructs a new exception with the specified detail message and cause.
054     * @param message the detail message.
055     * @param cause the cause.
056     */
057    public DynamicInformationException(String message, Throwable cause)
058    {
059        this(message, ExceptionType.UNKNOWN, cause, false);
060    }
061    
062    /**
063     * Constructs a new exception with the specified detail message and cause.
064     * @param message the detail message.
065     * @param type reason of the exception
066     * @param cause the cause.
067     */
068    public DynamicInformationException(String message, ExceptionType type, Throwable cause)
069    {
070        this(message, type, cause, false);
071    }
072
073    /**
074     * Constructs a new exception with the specified detail message and cause.
075     * @param message the detail message.
076     * @param type reason of the exception
077     * @param cause the cause.
078     * @param silent if true, indicates to not log errors
079     */
080    public DynamicInformationException(String message, ExceptionType type, Throwable cause, boolean silent)
081    {
082        super(message, cause);
083        _type = type;
084        _silent = silent;
085    }
086
087    /**
088     * Get the type of exception
089     * @return {@link ExceptionType}
090     */
091    public ExceptionType getType()
092    {
093        return _type;
094    }
095    
096    /**
097     * Returns true if this exception should not be logged as an error
098     * @return true if in silent mode
099     */
100    public boolean isSilent()
101    {
102        return _silent;
103    }
104}