Make StorageException a RuntimeException

In the context of a Gerrit server, StorageExceptions are generally
speaking not recoverable: the only place that should be catching them is
a top-level request handler such as RestApiServlet. Such a handler also
needs to be able to catch RuntimeExceptions, and indeed RestApiServlet
already catches bare Exception.

The general best practice in Java today is to use unchecked exception
when errors are not intended to be recoverable[citation needed]. This
avoids polluting interfaces with too many checked exception types.

After this change, most "throws StorageException" clauses are redundant
and can be cleaned up. However, that work is left for future commits;
this commit does the minimum work required to compile.

This commit does not additionally touch DuplicateKeyException, which
also becomes a RuntimeException since it inherits from StorageException.

Change-Id: I75230cf99145d886a5d872150e333519eb54acbb
This commit is contained in:
Dave Borowitz
2018-12-17 15:08:52 -08:00
parent 62f32fcfd6
commit a4fe999a75
11 changed files with 32 additions and 113 deletions

View File

@@ -379,7 +379,6 @@ public class ChangeJson {
return toChangeInfo(cd, limitToPsId, changeInfoSupplier);
} catch (PatchListNotAvailableException
| GpgException
| StorageException
| IOException
| PermissionBackendException
| RuntimeException e) {
@@ -426,7 +425,7 @@ public class ChangeJson {
try {
ensureLoaded(Collections.singleton(cd));
changeInfos.add(format(cd, Optional.empty(), false, ChangeInfo::new));
} catch (StorageException | RuntimeException e) {
} catch (RuntimeException e) {
logger.atWarning().withCause(e).log(
"Omitting corrupt change %s from results", cd.getId());
}

View File

@@ -14,8 +14,6 @@
package com.google.gerrit.server.query.change;
import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.exceptions.StorageRuntimeException;
import com.google.gerrit.index.query.AndSource;
import com.google.gerrit.index.query.IsVisibleToPredicate;
import com.google.gerrit.index.query.Predicate;
@@ -43,14 +41,9 @@ public class AndChangeSource extends AndSource<ChangeData> implements ChangeData
}
@Override
protected List<ChangeData> transformBuffer(List<ChangeData> buffer)
throws StorageRuntimeException {
protected List<ChangeData> transformBuffer(List<ChangeData> buffer) {
if (!hasChange()) {
try {
ChangeData.ensureChangeLoaded(buffer);
} catch (StorageException e) {
throw new StorageRuntimeException(e);
}
ChangeData.ensureChangeLoaded(buffer);
}
return super.transformBuffer(buffer);
}

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.server.restapi.change;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.exceptions.StorageRuntimeException;
import com.google.gerrit.extensions.common.ActionInfo;
import com.google.gerrit.extensions.restapi.ETagView;
import com.google.gerrit.extensions.restapi.Response;
@@ -73,8 +72,8 @@ public class GetRevisionActions implements ETagView<RevisionResource> {
changeResourceFactory.create(cd.notes(), user).prepareETag(h, user);
}
h.putBoolean(cs.furtherHiddenChanges());
} catch (IOException | StorageException | PermissionBackendException e) {
throw new StorageRuntimeException(e);
} catch (IOException | PermissionBackendException e) {
throw new StorageException(e);
}
return h.hash().toString();
}

View File

@@ -124,8 +124,7 @@ public class PreviewSubmit implements RestReadView<RevisionResource> {
.setContentType(f.getMimeType())
.setAttachmentName("submit-preview-" + change.getChangeId() + "." + format);
return bin;
} catch (StorageException
| RestApiException
} catch (RestApiException
| UpdateException
| IOException
| ConfigInvalidException

View File

@@ -24,7 +24,6 @@ import com.google.common.collect.Sets;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.data.ParameterizedString;
import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.exceptions.StorageRuntimeException;
import com.google.gerrit.extensions.api.changes.SubmitInput;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.restapi.AuthException;
@@ -69,7 +68,6 @@ import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jgit.errors.ConfigInvalidException;
@@ -290,9 +288,9 @@ public class Submit
return "Problems with change(s): "
+ unmergeable.stream().map(c -> c.getId().toString()).collect(joining(", "));
}
} catch (PermissionBackendException | StorageException | IOException e) {
} catch (PermissionBackendException | IOException e) {
logger.atSevere().withCause(e).log("Error checking if change is submittable");
throw new StorageRuntimeException("Could not determine problems for the change", e);
throw new StorageException("Could not determine problems for the change", e);
}
return null;
}
@@ -313,7 +311,7 @@ public class Submit
}
} catch (IOException e) {
logger.atSevere().withCause(e).log("Error checking if change is submittable");
throw new StorageRuntimeException("Could not determine problems for the change", e);
throw new StorageException("Could not determine problems for the change", e);
}
ChangeData cd = changeDataFactory.create(resource.getNotes());
@@ -321,42 +319,31 @@ public class Submit
MergeOp.checkSubmitRule(cd, false);
} catch (ResourceConflictException e) {
return null; // submit not visible
} catch (StorageException e) {
logger.atSevere().withCause(e).log("Error checking if change is submittable");
throw new StorageRuntimeException("Could not determine problems for the change", e);
}
ChangeSet cs;
try {
cs = mergeSuperSet.get().completeChangeSet(cd.change(), resource.getUser());
} catch (StorageException | IOException | PermissionBackendException e) {
throw new StorageRuntimeException(
"Could not determine complete set of changes to be submitted", e);
} catch (IOException | PermissionBackendException e) {
throw new StorageException("Could not determine complete set of changes to be submitted", e);
}
String topic = change.getTopic();
int topicSize = 0;
if (!Strings.isNullOrEmpty(topic)) {
topicSize = getChangesByTopic(topic).size();
topicSize = queryProvider.get().byTopicOpen(topic).size();
}
boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
Boolean enabled;
try {
// Recheck mergeability rather than using value stored in the index,
// which may be stale.
// TODO(dborowitz): This is ugly; consider providing a way to not read
// stored fields from the index in the first place.
// cd.setMergeable(null);
// That was done in unmergeableChanges which was called by
// problemsForSubmittingChangeset, so now it is safe to read from
// the cache, as it yields the same result.
enabled = cd.isMergeable();
} catch (StorageException e) {
throw new StorageRuntimeException("Could not determine mergeability", e);
}
// Recheck mergeability rather than using value stored in the index, which may be stale.
// TODO(dborowitz): This is ugly; consider providing a way to not read stored fields from the
// index in the first place.
// cd.setMergeable(null);
// That was done in unmergeableChanges which was called by problemsForSubmittingChangeset, so
// now it is safe to read from the cache, as it yields the same result.
Boolean enabled = cd.isMergeable();
if (submitProblems != null) {
return new UiAction.Description()
@@ -476,14 +463,6 @@ public class Submit
return submitter;
}
private List<ChangeData> getChangesByTopic(String topic) {
try {
return queryProvider.get().byTopicOpen(topic);
} catch (StorageException e) {
throw new StorageRuntimeException(e);
}
}
public static class CurrentRevision implements RestModifyView<ChangeResource, SubmitInput> {
private final Submit submit;
private final ChangeJson.Factory json;