Example usage for com.mongodb MongoWriteException getError

List of usage examples for com.mongodb MongoWriteException getError

Introduction

In this page you can find the example usage for com.mongodb MongoWriteException getError.

Prototype

public WriteError getError() 

Source Link

Document

Gets the error.

Usage

From source file:org.axonframework.mongo.eventsourcing.tokenstore.MongoTokenStore.java

License:Apache License

private AbstractTokenEntry<?> loadOrInsertTokenEntry(String processorName, int segment) {
    Document document = mongoTemplate.trackingTokensCollection().findOneAndUpdate(
            claimableTokenEntryFilter(processorName, segment),
            combine(set("owner", nodeId), set("timestamp", clock.millis())),
            new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));

    if (document == null) {
        try {//from w  w  w . j  a v  a  2  s  .c o m
            AbstractTokenEntry<?> tokenEntry = new GenericTokenEntry<>(null, serializer, contentType,
                    processorName, segment);
            tokenEntry.claim(nodeId, claimTimeout);

            mongoTemplate.trackingTokensCollection().insertOne(tokenEntryToDocument(tokenEntry));

            return tokenEntry;
        } catch (MongoWriteException exception) {
            if (ErrorCategory.fromErrorCode(exception.getError().getCode()) == ErrorCategory.DUPLICATE_KEY) {
                throw new UnableToClaimTokenException(
                        format("Unable to claim token '%s[%s]'", processorName, segment));
            }
        }
    }
    return documentToTokenEntry(document);
}

From source file:org.jaqpot.core.service.filter.excmappers.MongoWriteExceptionMapper.java

License:Open Source License

@Override
public Response toResponse(MongoWriteException exception) {
    LOG.log(Level.INFO, "MongoWriteExceptionMapper exception caught", exception);
    ErrorReport error;//from w ww  .j av a2s .com
    Response.Status status = Response.Status.INTERNAL_SERVER_ERROR;
    if (ErrorCategory.DUPLICATE_KEY.equals(exception.getError().getCategory())) {
        error = ErrorReportFactory.alreadyInDatabase(exception.getMessage());
        status = Response.Status.BAD_REQUEST;
    } else {
        error = ErrorReportFactory.internalServerError(exception, "MongoWriteException");
    }

    return Response.ok(error, MediaType.APPLICATION_JSON).status(status).build();
}

From source file:org.opencb.opencga.storage.mongodb.metadata.MongoDBStudyConfigurationManager.java

License:Apache License

@Override
public long lockStudy(int studyId, long lockDuration, long timeout)
        throws InterruptedException, TimeoutException {
    try {/*from  ww w.  j av  a2 s  .  c  o m*/
        // Ensure document exists
        collection.update(new Document("_id", studyId), set("id", studyId),
                new QueryOptions(MongoDBCollection.UPSERT, true));
    } catch (MongoWriteException e) {
        // Duplicated key exception
        if (e.getError().getCode() != 11000) {
            throw e;
        }
    } catch (DuplicateKeyException ignore) {
        // Ignore this exception.
        // With UPSERT=true, this command should never throw DuplicatedKeyException.
        // See https://jira.mongodb.org/browse/SERVER-14322
    }
    return mongoLock.lock(studyId, lockDuration, timeout);
}

From source file:week2.UserDAO.java

License:Apache License

public boolean addUser(String username, String password, String email) {

    String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));

    // XXX WORK HERE
    // create an object suitable for insertion into the user collection
    // be sure to add username and hashed password to the document. problem instructions
    // will tell you the schema that the documents must follow.
    Document doc = new Document("_id", username).append("password", passwordHash);

    if (email != null && !email.equals("")) {
        // XXX WORK HERE
        // if there is an email address specified, add it to the document too.
        doc.append("email", email);
    }/* ww w .  j  a  v  a 2s .c  o m*/

    try {
        // XXX WORK HERE
        // insert the document into the user collection here
        usersCollection.insertOne(doc);
        return true;
    } catch (MongoWriteException e) {
        if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
            System.out.println("Username already in use: " + username);
            return false;
        }
        throw e;
    }
}