diff --git a/src/main/java/org/springframework/data/couchbase/config/CouchbaseBucketFactoryBean.java b/src/main/java/org/springframework/data/couchbase/config/CouchbaseBucketFactoryBean.java index 560ac6d6..a4dd771e 100644 --- a/src/main/java/org/springframework/data/couchbase/config/CouchbaseBucketFactoryBean.java +++ b/src/main/java/org/springframework/data/couchbase/config/CouchbaseBucketFactoryBean.java @@ -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 { +public class CouchbaseBucketFactoryBean extends AbstractFactoryBean 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 { 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); + } } diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseDataIntegrityViolationException.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseDataIntegrityViolationException.java new file mode 100644 index 00000000..67b3131e --- /dev/null +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseDataIntegrityViolationException.java @@ -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); + } + +} diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java new file mode 100644 index 00000000..03f43d87 --- /dev/null +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java @@ -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. + *

+ * 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; + } + +} diff --git a/src/main/java/org/springframework/data/couchbase/core/OperationCancellationException.java b/src/main/java/org/springframework/data/couchbase/core/OperationCancellationException.java new file mode 100644 index 00000000..d29fc53e --- /dev/null +++ b/src/main/java/org/springframework/data/couchbase/core/OperationCancellationException.java @@ -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); + } + +} diff --git a/src/main/java/org/springframework/data/couchbase/core/OperationInterruptedException.java b/src/main/java/org/springframework/data/couchbase/core/OperationInterruptedException.java new file mode 100644 index 00000000..717add3a --- /dev/null +++ b/src/main/java/org/springframework/data/couchbase/core/OperationInterruptedException.java @@ -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); + } + +} diff --git a/src/main/java/org/springframework/data/couchbase/core/WriteResultChecking.java b/src/main/java/org/springframework/data/couchbase/core/WriteResultChecking.java new file mode 100644 index 00000000..2fe524ff --- /dev/null +++ b/src/main/java/org/springframework/data/couchbase/core/WriteResultChecking.java @@ -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 +}