public CommandFailed(String commandName, String reason, Throwable cause) { super(createCustomMessage(commandName, reason), cause, ErrorCode.COMMAND_FAILED); this.commandName = commandName; this.reason = reason; }
private BsonDocument getQuery(BsonValue<?> value) throws BadValueException { if (!(value instanceof BsonDocument)) { throw new BadValueException("Unknown top level operator: $query"); } return (BsonDocument) value; }
@Override public MongoException asMongoException() throws IllegalStateException { return new MongoException(errorDesc, errorCode); }
public TypesMismatchException newWithMessage(String customeMessage) { return new TypesMismatchException(getFieldId(), getExpectedType(), getFoundType(), customeMessage); } }
public static <T> Status<T> from(MongoException ex) { return new ErrorStatus<>(ex.getErrorCode(), ex.getLocalizedMessage()); }
private int getAndCheckFirstDot(String namespace) throws InvalidNamespaceException { int firstDot = namespace.indexOf('.'); if (firstDot == 0) { throw new InvalidNamespaceException(namespace, "The first character shall not be a dot"); } if (firstDot < 0) { return namespace.length(); } if (firstDot == namespace.length() - 1) { throw new InvalidNamespaceException(namespace, "The last character shall not be the first " + "dot"); } return firstDot; } }
@Override public String getErrorDesc() { return exception.getLocalizedMessage(); }
@Override public ErrorCode getErrorCode() { return exception.getErrorCode(); }
public static BsonTimestamp getTimestamp(BsonDocument doc, String fieldId) throws NoSuchKeyException, TypesMismatchException { Entry<?> entry = doc.getEntry(fieldId); if (entry == null) { throw new NoSuchKeyException(fieldId); } BsonValue<?> value = entry.getValue(); if (!value.isTimestamp()) { throw new TypesMismatchException(fieldId, "timestamp", value.getType()); } return value.asTimestamp(); }
@Nonnull public static BsonArray getArray(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isArray()) { throw new TypesMismatchException(fieldId, BsonType.ARRAY, object.getType()); } return object.asArray(); }
public MongoException( @Nonnull ErrorCode errorCode, @Nonnull Object... args) { super(calculateMessage(errorCode, args)); this.errorCode = errorCode; }
@Nonnull public static BsonValue getValue(BsonDocument doc, String fieldId) throws NoSuchKeyException { BsonValue object = doc.get(fieldId); if (object == null) { throw new NoSuchKeyException(fieldId); } return object; }
private static int getAndCheckFirstDot(String namespace) throws InvalidNamespaceException { int firstDot = namespace.indexOf('.'); if (firstDot == 0) { throw new InvalidNamespaceException(namespace, "The first character shall not be a dot"); } if (firstDot < 0) { return namespace.length(); } if (firstDot == namespace.length() - 1) { throw new InvalidNamespaceException(namespace, "The last character shall not be the first " + "dot"); } return firstDot; }
@Nonnull public static BsonObjectId getObjectId(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isObjectId()) { throw new TypesMismatchException(fieldId, "objectId", object.getType()); } return object.asObjectId(); }
public CommandFailed(String commandName, String reason) { super(createCustomMessage(commandName, reason), ErrorCode.COMMAND_FAILED); this.commandName = commandName; this.reason = reason; }
public MongoException( @Nonnull Throwable cause, @Nonnull ErrorCode errorCode, @Nonnull Object... args) { super(calculateMessage(errorCode, args), cause); this.errorCode = errorCode; }
@Nonnull public static Entry<?> getEntry(BsonDocument doc, String fieldId) throws NoSuchKeyException { Entry<?> entry = doc.getEntry(fieldId); if (entry == null) { throw new NoSuchKeyException(fieldId); } return entry; }
public static String getCollection(String namespace) throws InvalidNamespaceException { int firstDotIndex = getAndCheckFirstDot(namespace); if (firstDotIndex == namespace.length()) { //if there is no dot throw new InvalidNamespaceException( //then there throw InvalidNamespaceException namespace, "Does not have collection part" ); } return namespace.substring(firstDotIndex + 1); }
public static BsonTimestamp getTimestampFromDateTime(Entry<?> entry) throws TypesMismatchException { BsonValue<?> value = entry.getValue(); if (!value.isDateTime()) { throw new TypesMismatchException(entry.getKey(), BsonType.DATETIME, value.getType()); } return TimestampToDateTime.toTimestamp( value.asDateTime(), DefaultBsonValues::newTimestamp); } }
@Nonnull protected String getCollection(String namespace) throws InvalidNamespaceException { int firstDotIndex = getAndCheckFirstDot(namespace); if (firstDotIndex == namespace.length()) { //if there is no dot throw new InvalidNamespaceException( //then there throw InvalidNamespaceException namespace, "Does not have collection part" ); } return namespace.substring(firstDotIndex + 1); }