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

https://github.com/foursquare/mongo-java-driver · Java · 129 lines · 55 code · 14 blank · 60 comment · 10 complexity · f0f8daf90941171d036ea8314ad2c3d2 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 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 org.bson.BsonDocument;
  18. import org.bson.BsonInt32;
  19. import org.bson.BsonValue;
  20. import static java.lang.String.format;
  21. /**
  22. * An exception representing an error reported due to a write failure.
  23. *
  24. */
  25. public class WriteConcernException extends MongoServerException {
  26. private static final long serialVersionUID = -1100801000476719450L;
  27. private final WriteConcernResult writeConcernResult;
  28. private final BsonDocument response;
  29. /**
  30. * Construct a new instance.
  31. *
  32. * @param response the response to the write operation
  33. * @param address the address of the server that executed the operation
  34. * @param writeConcernResult the write concern result
  35. */
  36. public WriteConcernException(final BsonDocument response, final ServerAddress address,
  37. final WriteConcernResult writeConcernResult) {
  38. super(extractErrorCode(response),
  39. format("Write failed with error code %d and error message '%s'", extractErrorCode(response), extractErrorMessage(response)),
  40. address);
  41. this.response = response;
  42. this.writeConcernResult = writeConcernResult;
  43. }
  44. /**
  45. * For internal use only: extract the error code from the response to a getlasterror command.
  46. * @param response the response
  47. * @return the code, or -1 if there is none
  48. */
  49. public static int extractErrorCode(final BsonDocument response) {
  50. // mongos may set an err field containing duplicate key error information
  51. if (response.containsKey("err")) {
  52. String errorMessage = extractErrorMessage(response);
  53. if (errorMessage.contains("E11000 duplicate key error")) {
  54. return 11000;
  55. }
  56. }
  57. // mongos may return a list of documents representing getlasterror responses from each shard. Return the one with a matching
  58. // "err" field, so that it can be used to get the error code
  59. if (!response.containsKey("code") && response.containsKey("errObjects")) {
  60. for (BsonValue curErrorDocument : response.getArray("errObjects")) {
  61. if (extractErrorMessage(response).equals(extractErrorMessage(curErrorDocument.asDocument()))) {
  62. return curErrorDocument.asDocument().getNumber("code").intValue();
  63. }
  64. }
  65. }
  66. return response.getNumber("code", new BsonInt32(-1)).intValue();
  67. }
  68. /**
  69. * For internal use only: extract the error message from the response to a getlasterror command.
  70. *
  71. * @param response the response
  72. * @return the error message
  73. */
  74. public static String extractErrorMessage(final BsonDocument response) {
  75. if (response.isString("err")) {
  76. return response.getString("err").getValue();
  77. } else if (response.isString("errmsg")) {
  78. return response.getString("errmsg").getValue();
  79. } else {
  80. return null;
  81. }
  82. }
  83. /**
  84. * Gets the write result.
  85. *
  86. * @return the write result
  87. */
  88. public WriteConcernResult getWriteConcernResult() {
  89. return writeConcernResult;
  90. }
  91. /**
  92. * Gets the error code associated with the write concern failure.
  93. *
  94. * @return the error code
  95. */
  96. public int getErrorCode() {
  97. return extractErrorCode(response);
  98. }
  99. /**
  100. * Gets the error message associated with the write concern failure.
  101. *
  102. * @return the error message
  103. */
  104. public String getErrorMessage() {
  105. return extractErrorMessage(response);
  106. }
  107. /**
  108. * Gets the response to the write operation.
  109. *
  110. * @return the response to the write operation
  111. */
  112. public BsonDocument getResponse() {
  113. return response;
  114. }
  115. }