/driver-core/src/main/com/mongodb/MongoException.java

https://github.com/mebigfatguy/mongo-java-driver · Java · 174 lines · 62 code · 21 blank · 91 comment · 5 complexity · 6d9bf3cf57d99ffe51b5591def4d9057 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb;
  17. import com.mongodb.lang.Nullable;
  18. import java.util.Collections;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import static com.mongodb.assertions.Assertions.notNull;
  22. /**
  23. * Top level Exception for all Exceptions, server-side or client-side, that come from the driver.
  24. */
  25. public class MongoException extends RuntimeException {
  26. /**
  27. * An error label indicating that the exception can be treated as a transient transaction error.
  28. *
  29. * @see #hasErrorLabel(String)
  30. * @since 3.8
  31. */
  32. public static final String TRANSIENT_TRANSACTION_ERROR_LABEL = "TransientTransactionError";
  33. /**
  34. * An error label indicating that the exception can be treated as an unknown transaction commit result.
  35. *
  36. * @see #hasErrorLabel(String)
  37. * @since 3.8
  38. */
  39. public static final String UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL = "UnknownTransactionCommitResult";
  40. private static final long serialVersionUID = -4415279469780082174L;
  41. private final int code;
  42. private final Set<String> errorLabels = new HashSet<String>();
  43. /**
  44. * Static helper to create or cast a MongoException from a throwable
  45. *
  46. * @param t a throwable, which may be null
  47. * @return a MongoException
  48. */
  49. @Nullable
  50. public static MongoException fromThrowable(@Nullable final Throwable t) {
  51. if (t == null) {
  52. return null;
  53. } else {
  54. return fromThrowableNonNull(t);
  55. }
  56. }
  57. /**
  58. * Static helper to create or cast a MongoException from a throwable
  59. *
  60. * @param t a throwable, which may not be null
  61. * @return a MongoException
  62. * @since 3.7
  63. */
  64. public static MongoException fromThrowableNonNull(final Throwable t) {
  65. if (t instanceof MongoException) {
  66. return (MongoException) t;
  67. } else {
  68. return new MongoException(t.getMessage(), t);
  69. }
  70. }
  71. /**
  72. * @param msg the message
  73. */
  74. public MongoException(final String msg) {
  75. super(msg);
  76. code = -3;
  77. }
  78. /**
  79. * @param code the error code
  80. * @param msg the message
  81. */
  82. public MongoException(final int code, final String msg) {
  83. super(msg);
  84. this.code = code;
  85. }
  86. /**
  87. * @param msg the message
  88. * @param t the throwable cause
  89. */
  90. public MongoException(final String msg, final Throwable t) {
  91. super(msg, t);
  92. code = -4;
  93. }
  94. /**
  95. * @param code the error code
  96. * @param msg the message
  97. * @param t the throwable cause
  98. */
  99. public MongoException(final int code, final String msg, final Throwable t) {
  100. super(msg, t);
  101. this.code = code;
  102. }
  103. /**
  104. * Gets the exception code
  105. *
  106. * @return the error code.
  107. */
  108. public int getCode() {
  109. return code;
  110. }
  111. /**
  112. * Adds the given error label to the exception.
  113. *
  114. * @param errorLabel the non-null error label to add to the exception
  115. *
  116. * @since 3.8
  117. */
  118. public void addLabel(final String errorLabel) {
  119. notNull("errorLabel", errorLabel);
  120. errorLabels.add(errorLabel);
  121. }
  122. /**
  123. * Removes the given error label from the exception.
  124. *
  125. * @param errorLabel the non-null error label to remove from the exception
  126. *
  127. * @since 3.8
  128. */
  129. public void removeLabel(final String errorLabel) {
  130. notNull("errorLabel", errorLabel);
  131. errorLabels.remove(errorLabel);
  132. }
  133. /**
  134. * Gets the set of error labels associated with this exception.
  135. *
  136. * @return the error labels, which may not be null but may be empty
  137. * @since 3.8
  138. */
  139. public Set<String> getErrorLabels() {
  140. return Collections.unmodifiableSet(errorLabels);
  141. }
  142. /**
  143. * Return true if the exception is labelled with the given error label, and false otherwise.
  144. *
  145. * @param errorLabel the non-null error label
  146. * @return true if the exception is labelled with the given error label
  147. * @since 3.8
  148. */
  149. public boolean hasErrorLabel(final String errorLabel) {
  150. notNull("errorLabel", errorLabel);
  151. return errorLabels.contains(errorLabel);
  152. }
  153. }