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

https://github.com/jyemin/mongo-java-driver · Java · 127 lines · 61 code · 16 blank · 50 comment · 4 complexity · 82b6bf08828258aca20a56d594798e3d 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 org.bson.BsonArray;
  18. import org.bson.BsonDocument;
  19. import org.bson.BsonInt32;
  20. import org.bson.BsonString;
  21. import org.bson.codecs.BsonDocumentCodec;
  22. import org.bson.codecs.EncoderContext;
  23. import org.bson.json.JsonWriter;
  24. import java.io.StringWriter;
  25. import static java.lang.String.format;
  26. /**
  27. * An exception indicating that a command sent to a MongoDB server returned a failure.
  28. *
  29. * @since 2.13
  30. * @serial exclude
  31. */
  32. public class MongoCommandException extends MongoServerException {
  33. private static final long serialVersionUID = 8160676451944215078L;
  34. private final BsonDocument response;
  35. /**
  36. * Construct a new instance with the CommandResult from a failed command
  37. *
  38. * @param response the command response
  39. * @param address the address of the server that generated the response
  40. */
  41. public MongoCommandException(final BsonDocument response, final ServerAddress address) {
  42. super(extractErrorCode(response), extractErrorCodeName(response),
  43. format("Command failed with error %s: '%s' on server %s. The full response is %s", extractErrorCodeAndName(response),
  44. extractErrorMessage(response), address, getResponseAsJson(response)), address);
  45. this.response = response;
  46. addLabels(response.getArray("errorLabels", new BsonArray()));
  47. }
  48. /**
  49. * Gets the error code associated with the command failure.
  50. *
  51. * @return the error code
  52. */
  53. public int getErrorCode() {
  54. return getCode();
  55. }
  56. /**
  57. * Gets the name associated with the error code.
  58. *
  59. * @return the error code name, which may be the empty string
  60. * @since 3.8
  61. * @mongodb.server.release 3.4
  62. */
  63. public String getErrorCodeName() {
  64. return super.getErrorCodeName();
  65. }
  66. /**
  67. * Gets the error message associated with the command failure.
  68. *
  69. * @return the error message
  70. */
  71. public String getErrorMessage() {
  72. return extractErrorMessage(response);
  73. }
  74. /**
  75. * For internal use only.
  76. *
  77. * @return the full response to the command failure.
  78. */
  79. public BsonDocument getResponse() {
  80. return response;
  81. }
  82. private static String getResponseAsJson(final BsonDocument commandResponse) {
  83. StringWriter writer = new StringWriter();
  84. JsonWriter jsonWriter = new JsonWriter(writer);
  85. new BsonDocumentCodec().encode(jsonWriter, commandResponse, EncoderContext.builder().build());
  86. return writer.toString();
  87. }
  88. private static String extractErrorCodeAndName(final BsonDocument response) {
  89. int errorCode = extractErrorCode(response);
  90. String errorCodeName = extractErrorCodeName(response);
  91. if (errorCodeName.isEmpty()) {
  92. return Integer.toString(errorCode);
  93. } else {
  94. return String.format("%d (%s)", errorCode, errorCodeName);
  95. }
  96. }
  97. private static int extractErrorCode(final BsonDocument response) {
  98. return response.getNumber("code", new BsonInt32(-1)).intValue();
  99. }
  100. private static String extractErrorCodeName(final BsonDocument response) {
  101. return response.getString("codeName", new BsonString("")).getValue();
  102. }
  103. private static String extractErrorMessage(final BsonDocument response) {
  104. String errorMessage = response.getString("errmsg", new BsonString("")).getValue();
  105. // Satisfy nullability checker
  106. if (errorMessage == null) {
  107. throw new MongoInternalException("This value should not be null");
  108. }
  109. return errorMessage;
  110. }
  111. }