Merge "Handle any exception when allowing failures." into qt-dev

This commit is contained in:
Jeff Sharkey
2019-04-16 21:34:27 +00:00
committed by Android (Google) Code Review

View File

@@ -283,14 +283,6 @@ public class ContentProviderOperation implements Parcelable {
return mType == TYPE_ASSERT; return mType == TYPE_ASSERT;
} }
private ContentProviderResult fail(String msg) throws OperationApplicationException {
if (mFailureAllowed) {
return new ContentProviderResult(msg);
} else {
throw new OperationApplicationException(msg);
}
}
/** /**
* Applies this operation using the given provider. The backRefs array is used to resolve any * Applies this operation using the given provider. The backRefs array is used to resolve any
* back references that were requested using * back references that were requested using
@@ -307,20 +299,35 @@ public class ContentProviderOperation implements Parcelable {
*/ */
public ContentProviderResult apply(ContentProvider provider, ContentProviderResult[] backRefs, public ContentProviderResult apply(ContentProvider provider, ContentProviderResult[] backRefs,
int numBackRefs) throws OperationApplicationException { int numBackRefs) throws OperationApplicationException {
if (mFailureAllowed) {
try {
return applyInternal(provider, backRefs, numBackRefs);
} catch (Exception e) {
return new ContentProviderResult(e.getMessage());
}
} else {
return applyInternal(provider, backRefs, numBackRefs);
}
}
private ContentProviderResult applyInternal(ContentProvider provider,
ContentProviderResult[] backRefs, int numBackRefs)
throws OperationApplicationException {
ContentValues values = resolveValueBackReferences(backRefs, numBackRefs); ContentValues values = resolveValueBackReferences(backRefs, numBackRefs);
String[] selectionArgs = String[] selectionArgs =
resolveSelectionArgsBackReferences(backRefs, numBackRefs); resolveSelectionArgsBackReferences(backRefs, numBackRefs);
if (mType == TYPE_INSERT) { if (mType == TYPE_INSERT) {
Uri newUri = provider.insert(mUri, values); final Uri newUri = provider.insert(mUri, values);
if (newUri == null) { if (newUri != null) {
Log.e(TAG, this.toString()); return new ContentProviderResult(newUri);
return fail("Insert into " + mUri + " returned no result"); } else {
throw new OperationApplicationException(
"Insert into " + mUri + " returned no result");
} }
return new ContentProviderResult(newUri);
} }
int numRows; final int numRows;
if (mType == TYPE_DELETE) { if (mType == TYPE_DELETE) {
numRows = provider.delete(mUri, mSelection, selectionArgs); numRows = provider.delete(mUri, mSelection, selectionArgs);
} else if (mType == TYPE_UPDATE) { } else if (mType == TYPE_UPDATE) {
@@ -346,8 +353,7 @@ public class ContentProviderOperation implements Parcelable {
final String expectedValue = values.getAsString(projection[i]); final String expectedValue = values.getAsString(projection[i]);
if (!TextUtils.equals(cursorValue, expectedValue)) { if (!TextUtils.equals(cursorValue, expectedValue)) {
// Throw exception when expected values don't match // Throw exception when expected values don't match
Log.e(TAG, this.toString()); throw new OperationApplicationException("Found value " + cursorValue
return fail("Found value " + cursorValue
+ " when expected " + expectedValue + " for column " + " when expected " + expectedValue + " for column "
+ projection[i]); + projection[i]);
} }
@@ -358,13 +364,12 @@ public class ContentProviderOperation implements Parcelable {
cursor.close(); cursor.close();
} }
} else { } else {
Log.e(TAG, this.toString());
throw new IllegalStateException("bad type, " + mType); throw new IllegalStateException("bad type, " + mType);
} }
if (mExpectedCount != null && mExpectedCount != numRows) { if (mExpectedCount != null && mExpectedCount != numRows) {
Log.e(TAG, this.toString()); throw new OperationApplicationException(
return fail("Expected " + mExpectedCount + " rows but actual " + numRows); "Expected " + mExpectedCount + " rows but actual " + numRows);
} }
return new ContentProviderResult(numRows); return new ContentProviderResult(numRows);