Search in sources :

Example 1 with CouchDbNotFoundException

use of me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException in project solarthing by wildmountainfarms.

the class CouchDbPacketSaver method handle.

@Override
public void handle(PacketCollection packetCollection) throws PacketHandleException {
    // Normally we would try and create the database, but that doesn't work with non-admin cookie authenticated users
    String id = packetCollection.getDbId();
    String revision = idMap == null ? null : idMap.get(id);
    final JsonData jsonData;
    try {
        jsonData = new StringJsonData(MAPPER.writeValueAsString(packetCollection));
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Cannot serialize packet collection! This is bad!", e);
    }
    try {
        final DocumentResponse response;
        if (revision == null) {
            response = database.putDocument(id, jsonData);
        } else {
            response = database.updateDocument(id, revision, jsonData);
        }
        LOGGER.debug("Now revision is: " + response.getRev() + ". It was: " + revision);
        if (idMap != null) {
            // Currently, if we have a new document ID, we never, ever, need to worry about using an older document ID, so we can clear the map to avoid keeping unnecessary memory
            idMap.clear();
            idMap.put(id, response.getRev());
        }
    } catch (CouchDbNotFoundException ex) {
        throw new PacketHandleException("Got 'not found'. Does the database exist? Make sure to run the couchdb-setup!", ex);
    } catch (CouchDbUpdateConflictException ex) {
        if (idMap == null) {
            // we are ignoring conflicts
            LOGGER.debug("Got update conflict exception. Ignoring...");
            return;
        }
        try {
            String actualRev = database.getCurrentRevision(id);
            idMap.put(id, actualRev);
            LOGGER.debug("We were able to get the actual Revision ID for id=" + id + " actual rev=" + actualRev);
        } catch (CouchDbException revEx) {
            LOGGER.debug("Unable to get the actual Revision ID for id=" + id, revEx);
        }
        throw new PacketHandleException("Conflict while saving something to couchdb. id=" + id + " rev=" + revision + ". This usually means we put a packet in the database, but we weren't able to cache its rev id.", ex);
    } catch (CouchDbException ex) {
        if (ex.getCause() instanceof IOException) {
            throw new PacketHandleException("We got a DbAccessException probably meaning we couldn't reach the database.", ex);
        } else {
            throw new PacketHandleException("Got a DbAccessException without IOException as a cause. Something must be wrong.", ex);
        }
    }
}
Also used : StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData) CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) DocumentResponse(me.retrodaredevil.couchdbjava.response.DocumentResponse) CouchDbNotFoundException(me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException) IOException(java.io.IOException) CouchDbUpdateConflictException(me.retrodaredevil.couchdbjava.exception.CouchDbUpdateConflictException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData) PacketHandleException(me.retrodaredevil.solarthing.packets.handling.PacketHandleException)

Example 2 with CouchDbNotFoundException

use of me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException in project solarthing by wildmountainfarms.

the class CouchDbSetupMain method createUserIfNotExists.

private void createUserIfNotExists(String username, SolarThingDatabaseType.UserType userType) throws CouchDbException {
    CouchDbDatabase usersDatabase = instance.getUsersDatabase();
    String documentId = "org.couchdb.user:" + username;
    DocumentData userDocumentData = null;
    try {
        userDocumentData = usersDatabase.getDocument(documentId);
    } catch (CouchDbNotFoundException ignored) {
    }
    final UserEntry user;
    if (userDocumentData != null) {
        try {
            user = CouchDbJacksonUtil.readValue(MAPPER, userDocumentData.getJsonData(), UserEntry.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Could not parse user document data. Please report this bug", e);
        }
    } else {
        user = null;
    }
    if (user != null) {
        out.println("User: " + username + " exists! Continuing.");
    } else {
        out.println("Please enter a password for '" + username + "'.");
        String password = prompt.promptUserPassword(userType);
        final JsonData jsonData;
        try {
            jsonData = new StringJsonData(MAPPER.writeValueAsString(new UserEntry(username, password)));
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Couldn't serialize json! Report this!", e);
        }
        usersDatabase.createIfNotExists();
        usersDatabase.putDocument(documentId, jsonData);
    }
}
Also used : StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData) DocumentData(me.retrodaredevil.couchdbjava.response.DocumentData) CouchDbNotFoundException(me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException) CouchDbDatabase(me.retrodaredevil.couchdbjava.CouchDbDatabase) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) StringJsonData(me.retrodaredevil.couchdbjava.json.StringJsonData)

Example 3 with CouchDbNotFoundException

use of me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException in project solarthing by wildmountainfarms.

the class CouchDbAlterDatabase method delete.

@Override
public void delete(String documentId, UpdateToken updateToken) throws SolarThingDatabaseException {
    RevisionUpdateToken revisionUpdateToken = CouchDbSolarThingDatabase.checkUpdateToken(updateToken);
    String revision = revisionUpdateToken.getRevision();
    try {
        database.deleteDocument(documentId, revision);
    } catch (CouchDbUnauthorizedException e) {
        throw new UnauthorizedSolarThingDatabaseException(e);
    } catch (CouchDbUpdateConflictException e) {
        throw new UpdateConflictSolarThingDatabaseException("Update conflict on delete. Must not be latest revision. documentId: " + documentId + " revision: " + revision, e);
    } catch (CouchDbNotFoundException e) {
        throw new NotFoundSolarThingDatabaseException("(Not found) Could not delete documentId: " + documentId + " revision: " + revision, e);
    } catch (CouchDbException e) {
        throw new SolarThingDatabaseException("Could not delete documentId: " + documentId + " revision: " + revision, e);
    }
}
Also used : CouchDbUnauthorizedException(me.retrodaredevil.couchdbjava.exception.CouchDbUnauthorizedException) CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) CouchDbNotFoundException(me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) UnauthorizedSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) CouchDbUpdateConflictException(me.retrodaredevil.couchdbjava.exception.CouchDbUpdateConflictException) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) UnauthorizedSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Aggregations

CouchDbNotFoundException (me.retrodaredevil.couchdbjava.exception.CouchDbNotFoundException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 CouchDbException (me.retrodaredevil.couchdbjava.exception.CouchDbException)2 CouchDbUpdateConflictException (me.retrodaredevil.couchdbjava.exception.CouchDbUpdateConflictException)2 JsonData (me.retrodaredevil.couchdbjava.json.JsonData)2 StringJsonData (me.retrodaredevil.couchdbjava.json.StringJsonData)2 IOException (java.io.IOException)1 CouchDbDatabase (me.retrodaredevil.couchdbjava.CouchDbDatabase)1 CouchDbUnauthorizedException (me.retrodaredevil.couchdbjava.exception.CouchDbUnauthorizedException)1 DocumentData (me.retrodaredevil.couchdbjava.response.DocumentData)1 DocumentResponse (me.retrodaredevil.couchdbjava.response.DocumentResponse)1 NotFoundSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException)1 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)1 UnauthorizedSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException)1 UpdateConflictSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException)1 PacketHandleException (me.retrodaredevil.solarthing.packets.handling.PacketHandleException)1