001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  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 distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import java.net.*;
016import java.text.*;
017
018import org.apache.juneau.*;
019
020/**
021 * Generic exception thrown from the {@link PojoRest} class.
022 *
023 * <p>
024 * Typically, this is a user-error, such as trying to address a non-existent node in the tree.
025 *
026 * <p>
027 * The status code is an HTTP-equivalent code.  It will be one of the following:
028 * <ul class='spaced-list'>
029 *    <li>
030 *       {@link HttpURLConnection#HTTP_BAD_REQUEST HTTP_BAD_REQUEST}
031 *       - Attempting to do something impossible.
032 *    <li>
033 *       {@link HttpURLConnection#HTTP_NOT_FOUND HTTP_NOT_FOUND}
034 *       - Attempting to access a non-existent node in the tree.
035 *    <li>
036 *       {@link HttpURLConnection#HTTP_FORBIDDEN HTTP_FORBIDDEN}
037 *       - Attempting to overwrite the root object.
038 * </ul>
039 */
040public final class PojoRestException extends FormattedRuntimeException {
041
042   private static final long serialVersionUID = 1L;
043
044   private int status;
045
046   /**
047    * Constructor.
048    *
049    * @param cause The cause of this exception.
050    * @param status HTTP status code.
051    * @param message The {@link MessageFormat}-style message.
052    * @param args Optional {@link MessageFormat}-style arguments.
053    */
054   public PojoRestException(Throwable cause, int status, String message, Object... args) {
055      super(cause, getMessage(cause, message, null), args);
056      this.status = status;
057   }
058
059   /**
060    * Constructor.
061    *
062    * @param status The HTTP-equivalent status code.
063    * @param message The detailed message.
064    * @param args Optional {@link MessageFormat}-style arguments.
065    */
066   public PojoRestException(int status, String message, Object...args) {
067      this(null, status, message, args);
068   }
069
070   /**
071    * The HTTP-equivalent status code.
072    *
073    * <p>
074    * See above for details.
075    *
076    * @return The HTTP-equivalent status code.
077    */
078   public int getStatus() {
079      return status;
080   }
081}