add error handling for Query and ViewQuery methods

This commit is contained in:
Simon Baslé
2015-06-29 19:56:14 +02:00
parent 81d13ff6f7
commit 5c34b7d0c4
3 changed files with 79 additions and 17 deletions

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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<ViewRow> 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<T> result = new ArrayList<T>(allRows.size());
for (final ViewRow row : allRows) {
result.add(mapToEntity(row.id(), row.document(RawJsonDocument.class), entityClass));
List<ViewRow> allRows = response.allRows();
final List<T> result = new ArrayList<T>(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 <T> List<T> findByN1QL(Query n1ql, Class<T> entityClass) {
QueryResult queryResult = queryN1QL(n1ql);
try {
QueryResult queryResult = queryN1QL(n1ql);
List<QueryRow> allRows = queryResult.allRows();
List<T> result = new ArrayList<T>(allRows.size());
for (QueryRow row : allRows) {
String json = row.value().toString();
T decoded = translationService.decodeFragment(json, entityClass);
result.add(decoded);
if (queryResult.finalSuccess()) {
List<QueryRow> allRows = queryResult.allRows();
List<T> result = new ArrayList<T>(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