PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. */
  18. package org.apache.hcatalog.common;
  19. import java.io.IOException;
  20. /**
  21. * Class representing exceptions thrown by Howl.
  22. */
  23. public class HCatException extends IOException {
  24. private static final long serialVersionUID = 1L;
  25. /** The error type enum for this exception. */
  26. private final ErrorType errorType;
  27. /**
  28. * Instantiates a new howl exception.
  29. * @param errorType the error type
  30. */
  31. public HCatException(ErrorType errorType) {
  32. this(errorType, null, null);
  33. }
  34. /**
  35. * Instantiates a new howl exception.
  36. * @param errorType the error type
  37. * @param cause the cause
  38. */
  39. public HCatException(ErrorType errorType, Throwable cause) {
  40. this(errorType, null, cause);
  41. }
  42. /**
  43. * Instantiates a new howl exception.
  44. * @param errorType the error type
  45. * @param extraMessage extra messages to add to the message string
  46. */
  47. public HCatException(ErrorType errorType, String extraMessage) {
  48. this(errorType, extraMessage, null);
  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. * @param cause the cause
  55. */
  56. public HCatException(ErrorType errorType, String extraMessage, Throwable cause) {
  57. super(buildErrorMessage(
  58. errorType,
  59. extraMessage,
  60. cause), cause);
  61. this.errorType = errorType;
  62. }
  63. //TODO : remove default error type constructors after all exceptions
  64. //are changed to use error types
  65. /**
  66. * Instantiates a new howl exception.
  67. * @param message the error message
  68. */
  69. public HCatException(String message) {
  70. this(ErrorType.ERROR_INTERNAL_EXCEPTION, message, null);
  71. }
  72. /**
  73. * Instantiates a new howl exception.
  74. * @param message the error message
  75. * @param cause the cause
  76. */
  77. public HCatException(String message, Throwable cause) {
  78. this(ErrorType.ERROR_INTERNAL_EXCEPTION, message, cause);
  79. }
  80. /**
  81. * Builds the error message string. The error type message is appended with the extra message. If appendCause
  82. * is true for the error type, then the message of the cause also is added to the message.
  83. * @param type the error type
  84. * @param extraMessage the extra message string
  85. * @param cause the cause for the exception
  86. * @return the exception message string
  87. */
  88. public static String buildErrorMessage(ErrorType type, String extraMessage, Throwable cause) {
  89. //Initial message is just the error type message
  90. StringBuffer message = new StringBuffer(HCatException.class.getName());
  91. message.append(" : " + type.getErrorCode());
  92. message.append(" : " + type.getErrorMessage());
  93. if( extraMessage != null ) {
  94. //Add the extra message value to buffer
  95. message.append(" : " + extraMessage);
  96. }
  97. if( type.appendCauseMessage() ) {
  98. if( cause != null && cause.getMessage() != null ) {
  99. //Add the cause message to buffer
  100. message.append(". Cause : " + cause.toString());
  101. }
  102. }
  103. return message.toString();
  104. }
  105. /**
  106. * Is this a retriable error.
  107. * @return is it retriable
  108. */
  109. public boolean isRetriable() {
  110. return errorType.isRetriable();
  111. }
  112. /**
  113. * Gets the error type.
  114. * @return the error type enum
  115. */
  116. public ErrorType getErrorType() {
  117. return errorType;
  118. }
  119. /**
  120. * Gets the error code.
  121. * @return the error code
  122. */
  123. public int getErrorCode() {
  124. return errorType.getErrorCode();
  125. }
  126. /* (non-Javadoc)
  127. * @see java.lang.Throwable#toString()
  128. */
  129. @Override
  130. public String toString() {
  131. return getMessage();
  132. }
  133. }