Example usage for com.mongodb CommandResult getErrorMessage

List of usage examples for com.mongodb CommandResult getErrorMessage

Introduction

In this page you can find the example usage for com.mongodb CommandResult getErrorMessage.

Prototype

@Nullable
public String getErrorMessage() 

Source Link

Document

Gets the error message associated with a failed command.

Usage

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

private Optional<CommandResult> runDBCommand(String database, DBObject command) {
    MongoClient client = client();/*from ww w  . j a  v a 2  s  .c o m*/
    try {
        DB db = client.getDB(database);
        CommandResult status;
        try {
            status = db.command(command);
        } catch (MongoException e) {
            LOG.warn("Command " + command + " on " + getServerAddress() + " failed", e);
            return Optional.absent();
        }
        if (!status.ok()) {
            LOG.debug("Unexpected result of {} on {}: {}",
                    new Object[] { command, getServerAddress(), status.getErrorMessage() });
        }
        return Optional.of(status);
    } finally {
        client.close();
    }
}

From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java

License:Apache License

private void fetchAndPrintCollectionStats() {
    for (String databaseName : mongoClient.listDatabaseNames()) {
        DB db = mongoClient.getDB(databaseName);
        Set<String> collectionNames = db.getCollectionNames();
        if (collectionNames != null && collectionNames.size() > 0) {
            for (String collectionName : collectionNames) {
                DBCollection collection = db.getCollection(collectionName);
                CommandResult collectionStatsResult = collection.getStats();
                if (collectionStatsResult != null && collectionStatsResult.ok()) {
                    DBObject collectionStats = (DBObject) JSON.parse(collectionStatsResult.toString());
                    printCollectionStats(db.getName(), collectionName, collectionStats);
                } else {
                    String errorMessage = "Retrieving stats for collection " + collectionName + " of "
                            + db.getName() + " failed";
                    if (collectionStatsResult != null) {
                        errorMessage = errorMessage
                                .concat(" with error message " + collectionStatsResult.getErrorMessage());
                    }//from  ww  w.j  a v  a2  s . c  o m
                    logger.error(errorMessage);
                }
            }
        }
    }
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

public Iterable<DBObject> mapReduce(MapReduceCommand cmd) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(cmd);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }/*from  w w w.  j a  v  a2s  .  com*/
    return output.results();
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

public Iterable<DBObject> mapReduce(String map, String reduce) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, null, OutputType.INLINE, null);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }//w w w .j  a  va2  s . c o  m
    return output.results();
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private Iterable<DBObject> mapReduce(String map, String reduce, DBObject query) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, null, OutputType.INLINE, query);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }//from  w ww .j a  va 2s . c  o m
    return output.results();
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget,
        MapReduceCommand.OutputType outputType, String orderBy, DBObject query) throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, outputTarget, outputType, query);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }/*  w  w w  .  j  a  v  a2 s.  com*/
    DBCollection c = output.getOutputCollection();
    DBCursor cursor = null;
    if (orderBy != null) {
        cursor = c.find().sort(MapperUtil.getSort(orderBy));
    } else {
        cursor = c.find();
    }
    List<DBObject> list = new ArrayList<DBObject>();
    for (Iterator<DBObject> it = cursor.iterator(); it.hasNext();) {
        list.add(it.next());
    }
    return list;
}

From source file:com.bugull.mongo.AdvancedDao.java

License:Apache License

private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget,
        MapReduceCommand.OutputType outputType, String orderBy, int pageNum, int pageSize, DBObject query)
        throws MapReduceException {
    MapReduceOutput output = coll.mapReduce(map, reduce, outputTarget, outputType, query);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new MapReduceException(cr.getErrorMessage());
    }//w  w  w.java2  s.  c  om
    DBCollection c = output.getOutputCollection();
    DBCursor cursor = null;
    if (orderBy != null) {
        cursor = c.find().sort(MapperUtil.getSort(orderBy)).skip((pageNum - 1) * pageSize).limit(pageSize);
    } else {
        cursor = c.find().skip((pageNum - 1) * pageSize).limit(pageSize);
    }
    List<DBObject> list = new ArrayList<DBObject>();
    for (Iterator<DBObject> it = cursor.iterator(); it.hasNext();) {
        list.add(it.next());
    }
    return list;
}

From source file:com.bugull.mongo.BuguAggregation.java

License:Apache License

public Iterable<DBObject> results() throws AggregationException {
    int size = stages.size();
    if (size <= 0) {
        throw new AggregationException("Empty stage in aggregation pipeline!");
    }//from w  w  w . ja  v a 2  s . c o  m
    AggregationOutput output = coll.aggregate(stages);
    CommandResult cr = output.getCommandResult();
    if (!cr.ok()) {
        throw new AggregationException(cr.getErrorMessage());
    }
    return output.results();
}

From source file:com.github.joelittlejohn.embedmongo.MongoScriptsMojo.java

License:Apache License

@Override
public void executeStart() throws MojoExecutionException, MojoFailureException {
    DB db = connectToMongoAndGetDatabase();

    if (scriptsDirectory.isDirectory()) {
        Scanner scanner = null;/* www. ja v a 2 s .  co m*/
        StringBuilder instructions = new StringBuilder();
        File[] files = scriptsDirectory.listFiles();

        if (files == null) {
            getLog().info("Can't read scripts directory: " + scriptsDirectory.getAbsolutePath());

        } else {
            getLog().info(
                    "Folder " + scriptsDirectory.getAbsolutePath() + " contains " + files.length + " file(s):");

            for (File file : files) {
                if (file.isFile()) {
                    try {
                        scanner = new Scanner(file);
                        while (scanner.hasNextLine()) {
                            instructions.append(scanner.nextLine()).append("\n");
                        }
                    } catch (FileNotFoundException e) {
                        throw new MojoExecutionException(
                                "Unable to find file with name '" + file.getName() + "'", e);
                    } finally {
                        if (scanner != null) {
                            scanner.close();
                        }
                    }
                    CommandResult result;
                    try {
                        result = db.doEval("(function() {" + instructions.toString() + "})();", new Object[0]);
                    } catch (MongoException e) {
                        throw new MojoExecutionException(
                                "Unable to execute file with name '" + file.getName() + "'", e);
                    }
                    if (!result.ok()) {
                        getLog().error(
                                "- file " + file.getName() + " parsed with error: " + result.getErrorMessage());
                        throw new MojoExecutionException("Error while executing instructions from file '"
                                + file.getName() + "': " + result.getErrorMessage(), result.getException());
                    }
                    getLog().info("- file " + file.getName() + " parsed successfully");
                }
            }
        }
        getLog().info("Data initialized with success");
    }
}

From source file:com.github.maasdi.di.trans.steps.mongodbdelete.MongoDbDelete.java

License:Apache License

protected void commitDelete(DBObject deleteQuery, Object[] row) throws KettleException {
    int retrys = 0;
    MongoException lastEx = null;//  ww w .java2 s  . c o m

    while (retrys <= m_writeRetries && !isStopped()) {
        WriteResult result = null;
        CommandResult cmd = null;
        try {
            logDetailed(BaseMessages.getString(PKG, "MongoDbDelete.Message.ExecutingQuery", deleteQuery));
            result = data.getCollection().drop(deleteQuery);

            cmd = result.getLastError();
            if (cmd != null && !cmd.ok()) {
                String message = cmd.getErrorMessage();
                logError(BaseMessages.getString(PKG, "MongoDbDelete.ErrorMessage.MongoReported", message));

                cmd.throwOnError();
            }
        } catch (MongoException me) {
            lastEx = me;
            retrys++;
            if (retrys <= m_writeRetries) {
                logError(BaseMessages.getString(PKG, "MongoDbDelete.ErrorMessage.ErrorWritingToMongo",
                        me.toString()));
                logBasic(BaseMessages.getString(PKG, "MongoDbDelete.Message.Retry", m_writeRetryDelay));
                try {
                    Thread.sleep(m_writeRetryDelay * 1000);
                    // CHECKSTYLE:OFF
                } catch (InterruptedException e) {
                    // CHECKSTYLE:ON
                }
            }
        }

        if (cmd != null && cmd.ok()) {
            break;
        }
    }

    if ((retrys > m_writeRetries || isStopped()) && lastEx != null) {

        // Send this one to the error stream if doing error handling
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), row, 1, lastEx.getMessage(), "", "MongoDbDelete");
        } else {
            throw new KettleException(lastEx);
        }
    }
}