From 5c34b7d0c43ca03090dfd194bd5b2cdcbdb1bb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Basl=C3=A9?= Date: Mon, 29 Jun 2015 19:56:14 +0200 Subject: [PATCH] add error handling for Query and ViewQuery methods --- .../core/CouchbaseExceptionTranslator.java | 8 +++ .../CouchbaseQueryExecutionException.java | 33 +++++++++++ .../couchbase/core/CouchbaseTemplate.java | 55 +++++++++++++------ 3 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 src/main/java/org/springframework/data/couchbase/core/CouchbaseQueryExecutionException.java diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java index 03f43d87..00230549 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java @@ -38,11 +38,13 @@ 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.TranscodingException; 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.DataRetrievalFailureException; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.InvalidDataAccessResourceUsageException; import org.springframework.dao.QueryTimeoutException; @@ -115,6 +117,12 @@ public class CouchbaseExceptionTranslator implements PersistenceExceptionTransla return new QueryTimeoutException(ex.getMessage(), ex); } + if (ex instanceof TranscodingException) { + //note: the more specific CouchbaseQueryExecutionException should be thrown by the template + //when dealing with TranscodingException in the query/n1ql methods. + return new DataRetrievalFailureException(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/CouchbaseQueryExecutionException.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseQueryExecutionException.java new file mode 100644 index 00000000..7cb25a90 --- /dev/null +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseQueryExecutionException.java @@ -0,0 +1,33 @@ +/* + * 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.DataRetrievalFailureException; + +/** + * An {@link DataRetrievalFailureException} that denotes an error during a query (N1QL). + */ +public class CouchbaseQueryExecutionException extends DataRetrievalFailureException { + + public CouchbaseQueryExecutionException(String msg) { + super(msg); + } + + public CouchbaseQueryExecutionException(String msg, Throwable cause) { + super(msg, cause); + } +} diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java index d4a99b1b..d78b4e5e 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java @@ -31,7 +31,9 @@ import com.couchbase.client.java.PersistTo; import com.couchbase.client.java.ReplicateTo; import com.couchbase.client.java.document.Document; import com.couchbase.client.java.document.RawJsonDocument; +import com.couchbase.client.java.document.json.JsonObject; import com.couchbase.client.java.error.CASMismatchException; +import com.couchbase.client.java.error.TranscodingException; import com.couchbase.client.java.query.Query; import com.couchbase.client.java.query.QueryResult; import com.couchbase.client.java.query.QueryRow; @@ -262,16 +264,24 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP query.includeDocs(false); query.reduce(false); - final ViewResult response = queryView(query); - List allRows = response.allRows(); - //TODO error handling + try { + final ViewResult response = queryView(query); + if (response.error() != null) { + throw new CouchbaseQueryExecutionException("Unable to execute view query due to the following view error: " + + response.error().toString()); + } - final List result = new ArrayList(allRows.size()); - for (final ViewRow row : allRows) { - result.add(mapToEntity(row.id(), row.document(RawJsonDocument.class), entityClass)); + List allRows = response.allRows(); + + final List result = new ArrayList(allRows.size()); + for (final ViewRow row : allRows) { + result.add(mapToEntity(row.id(), row.document(RawJsonDocument.class), entityClass)); + } + + return result; + } catch (TranscodingException e) { + throw new CouchbaseQueryExecutionException("Unable to execute view query", e); } - - return result; } @Override @@ -286,17 +296,28 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP @Override public List findByN1QL(Query n1ql, Class entityClass) { - QueryResult queryResult = queryN1QL(n1ql); + try { + QueryResult queryResult = queryN1QL(n1ql); - List allRows = queryResult.allRows(); - List result = new ArrayList(allRows.size()); - for (QueryRow row : allRows) { - String json = row.value().toString(); - T decoded = translationService.decodeFragment(json, entityClass); - result.add(decoded); + if (queryResult.finalSuccess()) { + List allRows = queryResult.allRows(); + List result = new ArrayList(allRows.size()); + for (QueryRow row : allRows) { + String json = row.value().toString(); + T decoded = translationService.decodeFragment(json, entityClass); + result.add(decoded); + } + return result; + } else { + StringBuilder message = new StringBuilder("Unable to execute query due to the following n1ql errors: "); + for (JsonObject error : queryResult.errors()) { + message.append('\n').append(error); + } + throw new CouchbaseQueryExecutionException(message.toString()); + } + } catch (TranscodingException e) { + throw new CouchbaseQueryExecutionException("Unable to execute query", e); } - //TODO error handling - return result; } @Override