Merge pull request #45 from simonbasle/exception

[SDK2] Exception translating
This commit is contained in:
Simon Baslé
2015-06-29 16:37:20 +02:00
6 changed files with 309 additions and 3 deletions

View File

@@ -21,6 +21,9 @@ import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseBucket;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.couchbase.core.CouchbaseExceptionTranslator;
/**
* The Factory Bean to help {@link CouchbaseBucketParser} constructing a {@link Bucket} from a given
@@ -28,12 +31,14 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
*
* @author Simon Baslé
*/
public class CouchbaseBucketFactoryBean extends AbstractFactoryBean<Bucket> {
public class CouchbaseBucketFactoryBean extends AbstractFactoryBean<Bucket> implements PersistenceExceptionTranslator {
private final Cluster cluster;
private final String bucketName;
private final String bucketPassword;
private final PersistenceExceptionTranslator exceptionTranslator = new CouchbaseExceptionTranslator();
public CouchbaseBucketFactoryBean(Cluster cluster) {
this(cluster, null, null);
}
@@ -57,10 +62,17 @@ public class CouchbaseBucketFactoryBean extends AbstractFactoryBean<Bucket> {
protected Bucket createInstance() throws Exception {
if (bucketName == null) {
return cluster.openBucket();
} else if (bucketPassword == null) {
}
else if (bucketPassword == null) {
return cluster.openBucket(bucketName);
} else {
}
else {
return cluster.openBucket(bucketName, bucketPassword);
}
}
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
return exceptionTranslator.translateExceptionIfPossible(ex);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core;
import org.springframework.dao.DataIntegrityViolationException;
/**
* A Couchbase specific integrity violation exception, thrown as a result of failing db operations.
*
* @author Michael Nitschinger
*/
public class CouchbaseDataIntegrityViolationException extends DataIntegrityViolationException {
private static final long serialVersionUID = -3724991479213025850L;
public CouchbaseDataIntegrityViolationException(String msg) {
super(msg);
}
public CouchbaseDataIntegrityViolationException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2012-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core;
import java.util.concurrent.TimeoutException;
import com.couchbase.client.core.BackpressureException;
import com.couchbase.client.core.BucketClosedException;
import com.couchbase.client.core.DocumentConcurrentlyModifiedException;
import com.couchbase.client.core.ReplicaNotConfiguredException;
import com.couchbase.client.core.RequestCancelledException;
import com.couchbase.client.core.ServiceNotAvailableException;
import com.couchbase.client.core.config.ConfigurationException;
import com.couchbase.client.core.endpoint.SSLException;
import com.couchbase.client.core.endpoint.kv.AuthenticationException;
import com.couchbase.client.core.env.EnvironmentException;
import com.couchbase.client.core.state.NotConnectedException;
import com.couchbase.client.java.error.BucketDoesNotExistException;
import com.couchbase.client.java.error.CASMismatchException;
import com.couchbase.client.java.error.DesignDocumentException;
import com.couchbase.client.java.error.DocumentAlreadyExistsException;
import com.couchbase.client.java.error.DurabilityException;
import com.couchbase.client.java.error.InvalidPasswordException;
import com.couchbase.client.java.error.RequestTooBigException;
import com.couchbase.client.java.error.TemporaryFailureException;
import com.couchbase.client.java.error.TemporaryLockFailureException;
import com.couchbase.client.java.error.ViewDoesNotExistException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
/**
* Simple {@link PersistenceExceptionTranslator} for Couchbase.
* <p/>
* Convert the given runtime exception to an appropriate exception from the {@code org.springframework.dao} hierarchy.
* Return {@literal null} if no translation is appropriate: any other exception may have resulted from user code, and
* should not be translated.
*
* @author Michael Nitschinger
* @author Simon Baslé
*/
public class CouchbaseExceptionTranslator implements PersistenceExceptionTranslator {
/**
* Translate Couchbase specific exceptions to spring exceptions if possible.
*
* @param ex the exception to translate.
* @return the translated exception or null.
*/
@Override
public final DataAccessException translateExceptionIfPossible(final RuntimeException ex) {
if (ex instanceof InvalidPasswordException
|| ex instanceof NotConnectedException
|| ex instanceof ConfigurationException
|| ex instanceof EnvironmentException
|| ex instanceof InvalidPasswordException
|| ex instanceof SSLException
|| ex instanceof ServiceNotAvailableException
|| ex instanceof BucketClosedException
|| ex instanceof BucketDoesNotExistException
|| ex instanceof AuthenticationException) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}
if (ex instanceof DocumentAlreadyExistsException) {
return new DuplicateKeyException(ex.getMessage(), ex);
}
if (ex instanceof CASMismatchException
|| ex instanceof DocumentConcurrentlyModifiedException
|| ex instanceof ReplicaNotConfiguredException
|| ex instanceof DurabilityException) {
return new DataIntegrityViolationException(ex.getMessage(), ex);
}
if (ex instanceof RequestCancelledException
|| ex instanceof BackpressureException) {
return new OperationCancellationException(ex.getMessage(), ex);
}
if (ex instanceof ViewDoesNotExistException
|| ex instanceof RequestTooBigException
|| ex instanceof DesignDocumentException) {
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
}
if (ex instanceof TemporaryLockFailureException
|| ex instanceof TemporaryFailureException) {
return new TransientDataAccessResourceException(ex.getMessage(), ex);
}
if ((ex instanceof RuntimeException && ex.getCause() instanceof TimeoutException)) {
return new QueryTimeoutException(ex.getMessage(), ex);
}
// Unable to translate exception, therefore just throw the original!
throw ex;
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2012-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core;
import org.springframework.dao.TransientDataAccessException;
/**
* Data Access Exception that identifies Operations cancelled while being processed.
*
* @author Michael Nitschinger
*/
public class OperationCancellationException extends TransientDataAccessException {
/**
* Constructor for OperationCancellationException.
*
* @param msg the detail message
*/
public OperationCancellationException(final String msg) {
super(msg);
}
/**
* Constructor for OperationCancellationException.
*
* @param msg the detail message
* @param cause the root cause from the data access API in use
*/
public OperationCancellationException(final String msg, final Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2012-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core;
import org.springframework.dao.TransientDataAccessException;
/**
* Data Access Exception that identifies Operations interrupted while being processed.
*
* @author Michael Nitschinger
*/
public class OperationInterruptedException extends TransientDataAccessException {
/**
* Constructor for OperationInterruptedException.
*
* @param msg the detail message
*/
public OperationInterruptedException(final String msg) {
super(msg);
}
/**
* Constructor for OperationInterruptedException.
*
* @param msg the detail message
* @param cause the root cause from the data access API in use
*/
public OperationInterruptedException(final String msg, final Throwable cause) {
super(msg, cause);
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2012-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core;
/**
* How failing write results should be handled.
*
* @author Michael Nitschinger
*/
public enum WriteResultChecking {
/**
* Ignore failing write results.
*/
NONE,
/**
* Log failing write results.
*/
LOG,
/**
* Throw Exceptions on failing write results.
*/
EXCEPTION
}