PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/src/java/org/apache/hcatalog/common/HCatException.java

#
Java | 157 lines | 55 code | 24 blank | 78 comment | 7 complexity | d34ec215490d071efedcc88b3b4eb090 MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hcatalog.common;
 19
 20import java.io.IOException;
 21
 22/**
 23 * Class representing exceptions thrown by Howl.
 24 */
 25public class HCatException extends IOException {
 26
 27  private static final long serialVersionUID = 1L;
 28
 29  /** The error type enum for this exception. */
 30  private final ErrorType errorType;
 31
 32  /**
 33   * Instantiates a new howl exception.
 34   * @param errorType the error type
 35   */
 36  public HCatException(ErrorType errorType) {
 37    this(errorType, null, null);
 38  }
 39
 40
 41  /**
 42   * Instantiates a new howl exception.
 43   * @param errorType the error type
 44   * @param cause the cause
 45   */
 46  public HCatException(ErrorType errorType, Throwable cause) {
 47    this(errorType, null, cause);
 48  }
 49
 50  /**
 51   * Instantiates a new howl exception.
 52   * @param errorType the error type
 53   * @param extraMessage extra messages to add to the message string
 54   */
 55  public HCatException(ErrorType errorType, String extraMessage) {
 56    this(errorType, extraMessage, null);
 57  }
 58
 59  /**
 60   * Instantiates a new howl exception.
 61   * @param errorType the error type
 62   * @param extraMessage extra messages to add to the message string
 63   * @param cause the cause
 64   */
 65  public HCatException(ErrorType errorType, String extraMessage, Throwable cause) {
 66    super(buildErrorMessage(
 67        errorType,
 68        extraMessage,
 69        cause), cause);
 70    this.errorType = errorType;
 71  }
 72
 73
 74  //TODO : remove default error type constructors after all exceptions
 75  //are changed to use error types
 76  /**
 77   * Instantiates a new howl exception.
 78   * @param message the error message
 79   */
 80  public HCatException(String message) {
 81    this(ErrorType.ERROR_INTERNAL_EXCEPTION, message, null);
 82  }
 83
 84  /**
 85   * Instantiates a new howl exception.
 86   * @param message the error message
 87   * @param cause the cause
 88   */
 89  public HCatException(String message, Throwable cause) {
 90    this(ErrorType.ERROR_INTERNAL_EXCEPTION, message, cause);
 91  }
 92
 93
 94  /**
 95   * Builds the error message string. The error type message is appended with the extra message. If appendCause
 96   * is true for the error type, then the message of the cause also is added to the message.
 97   * @param type the error type
 98   * @param extraMessage the extra message string
 99   * @param cause the cause for the exception
100   * @return the exception message string
101   */
102  public static String buildErrorMessage(ErrorType type, String extraMessage, Throwable cause) {
103
104    //Initial message is just the error type message
105    StringBuffer message = new StringBuffer(HCatException.class.getName());
106    message.append(" : " + type.getErrorCode());
107    message.append(" : " + type.getErrorMessage());
108
109    if( extraMessage != null ) {
110      //Add the extra message value to buffer
111      message.append(" : " + extraMessage);
112    }
113
114    if( type.appendCauseMessage() ) {
115      if( cause != null && cause.getMessage() != null ) {
116        //Add the cause message to buffer
117        message.append(". Cause : " + cause.toString());
118      }
119    }
120
121    return message.toString();
122  }
123
124
125  /**
126   * Is this a retriable error.
127   * @return is it retriable
128   */
129  public boolean isRetriable() {
130    return errorType.isRetriable();
131  }
132
133  /**
134   * Gets the error type.
135   * @return the error type enum
136   */
137  public ErrorType getErrorType() {
138    return errorType;
139  }
140
141  /**
142   * Gets the error code.
143   * @return the error code
144   */
145  public int getErrorCode() {
146    return errorType.getErrorCode();
147  }
148
149  /* (non-Javadoc)
150   * @see java.lang.Throwable#toString()
151   */
152  @Override
153  public String toString() {
154    return getMessage();
155  }
156
157}