add error handling for Query and ViewQuery methods
This commit is contained in:
@@ -38,11 +38,13 @@ import com.couchbase.client.java.error.InvalidPasswordException;
|
|||||||
import com.couchbase.client.java.error.RequestTooBigException;
|
import com.couchbase.client.java.error.RequestTooBigException;
|
||||||
import com.couchbase.client.java.error.TemporaryFailureException;
|
import com.couchbase.client.java.error.TemporaryFailureException;
|
||||||
import com.couchbase.client.java.error.TemporaryLockFailureException;
|
import com.couchbase.client.java.error.TemporaryLockFailureException;
|
||||||
|
import com.couchbase.client.java.error.TranscodingException;
|
||||||
import com.couchbase.client.java.error.ViewDoesNotExistException;
|
import com.couchbase.client.java.error.ViewDoesNotExistException;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.DataAccessResourceFailureException;
|
import org.springframework.dao.DataAccessResourceFailureException;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.dao.DataRetrievalFailureException;
|
||||||
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||||
import org.springframework.dao.QueryTimeoutException;
|
import org.springframework.dao.QueryTimeoutException;
|
||||||
@@ -115,6 +117,12 @@ public class CouchbaseExceptionTranslator implements PersistenceExceptionTransla
|
|||||||
return new QueryTimeoutException(ex.getMessage(), ex);
|
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!
|
// Unable to translate exception, therefore just throw the original!
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,7 +31,9 @@ import com.couchbase.client.java.PersistTo;
|
|||||||
import com.couchbase.client.java.ReplicateTo;
|
import com.couchbase.client.java.ReplicateTo;
|
||||||
import com.couchbase.client.java.document.Document;
|
import com.couchbase.client.java.document.Document;
|
||||||
import com.couchbase.client.java.document.RawJsonDocument;
|
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.CASMismatchException;
|
||||||
|
import com.couchbase.client.java.error.TranscodingException;
|
||||||
import com.couchbase.client.java.query.Query;
|
import com.couchbase.client.java.query.Query;
|
||||||
import com.couchbase.client.java.query.QueryResult;
|
import com.couchbase.client.java.query.QueryResult;
|
||||||
import com.couchbase.client.java.query.QueryRow;
|
import com.couchbase.client.java.query.QueryRow;
|
||||||
@@ -262,16 +264,24 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP
|
|||||||
query.includeDocs(false);
|
query.includeDocs(false);
|
||||||
query.reduce(false);
|
query.reduce(false);
|
||||||
|
|
||||||
final ViewResult response = queryView(query);
|
try {
|
||||||
List<ViewRow> allRows = response.allRows();
|
final ViewResult response = queryView(query);
|
||||||
//TODO error handling
|
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());
|
List<ViewRow> allRows = response.allRows();
|
||||||
for (final ViewRow row : allRows) {
|
|
||||||
result.add(mapToEntity(row.id(), row.document(RawJsonDocument.class), entityClass));
|
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
|
@Override
|
||||||
@@ -286,17 +296,28 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> List<T> findByN1QL(Query n1ql, Class<T> entityClass) {
|
public <T> List<T> findByN1QL(Query n1ql, Class<T> entityClass) {
|
||||||
QueryResult queryResult = queryN1QL(n1ql);
|
try {
|
||||||
|
QueryResult queryResult = queryN1QL(n1ql);
|
||||||
|
|
||||||
List<QueryRow> allRows = queryResult.allRows();
|
if (queryResult.finalSuccess()) {
|
||||||
List<T> result = new ArrayList<T>(allRows.size());
|
List<QueryRow> allRows = queryResult.allRows();
|
||||||
for (QueryRow row : allRows) {
|
List<T> result = new ArrayList<T>(allRows.size());
|
||||||
String json = row.value().toString();
|
for (QueryRow row : allRows) {
|
||||||
T decoded = translationService.decodeFragment(json, entityClass);
|
String json = row.value().toString();
|
||||||
result.add(decoded);
|
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
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user