Remove StorageException catch clauses from httpd
Some of these are the antipattern of returning an empty result instead of letting a fatal error propagate. In others, the purpose of the catch clause is to do additional logging or add context information to the exception message. In these cases, switch to catching RuntimeException instead, under the assumption that the additional context is useful regardless of the type of unexpected error. Change-Id: I21b94ae2c00b9c09c2b81c5fc8cd1b66bb491bc9
This commit is contained in:
@@ -15,7 +15,6 @@
|
|||||||
package com.google.gerrit.httpd;
|
package com.google.gerrit.httpd;
|
||||||
|
|
||||||
import com.google.gerrit.common.PageLinks;
|
import com.google.gerrit.common.PageLinks;
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||||
import com.google.gerrit.reviewdb.client.Change;
|
import com.google.gerrit.reviewdb.client.Change;
|
||||||
@@ -61,7 +60,7 @@ public class NumericChangeIdRedirectServlet extends HttpServlet {
|
|||||||
} catch (ResourceConflictException | ResourceNotFoundException e) {
|
} catch (ResourceConflictException | ResourceNotFoundException e) {
|
||||||
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
return;
|
return;
|
||||||
} catch (StorageException | PermissionBackendException e) {
|
} catch (PermissionBackendException | RuntimeException e) {
|
||||||
throw new IOException("Unable to lookup change " + id.id, e);
|
throw new IOException("Unable to lookup change " + id.id, e);
|
||||||
}
|
}
|
||||||
String path =
|
String path =
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
|
|||||||
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.registration.DynamicItem;
|
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||||
import com.google.gerrit.extensions.restapi.AuthException;
|
import com.google.gerrit.extensions.restapi.AuthException;
|
||||||
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
|
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
|
||||||
@@ -110,7 +109,7 @@ class RunAsFilter implements Filter {
|
|||||||
} catch (UnprocessableEntityException e) {
|
} catch (UnprocessableEntityException e) {
|
||||||
replyError(req, res, SC_FORBIDDEN, "no account matches " + RUN_AS, null);
|
replyError(req, res, SC_FORBIDDEN, "no account matches " + RUN_AS, null);
|
||||||
return;
|
return;
|
||||||
} catch (StorageException | IOException | ConfigInvalidException e) {
|
} catch (IOException | ConfigInvalidException | RuntimeException e) {
|
||||||
logger.atWarning().withCause(e).log("cannot resolve account for %s", RUN_AS);
|
logger.atWarning().withCause(e).log("cannot resolve account for %s", RUN_AS);
|
||||||
replyError(req, res, SC_INTERNAL_SERVER_ERROR, "cannot resolve " + RUN_AS, e);
|
replyError(req, res, SC_INTERNAL_SERVER_ERROR, "cannot resolve " + RUN_AS, e);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USE
|
|||||||
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_UUID;
|
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_UUID;
|
||||||
|
|
||||||
import com.google.gerrit.common.PageLinks;
|
import com.google.gerrit.common.PageLinks;
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.registration.DynamicItem;
|
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||||
import com.google.gerrit.httpd.HtmlDomUtil;
|
import com.google.gerrit.httpd.HtmlDomUtil;
|
||||||
import com.google.gerrit.httpd.LoginUrlToken;
|
import com.google.gerrit.httpd.LoginUrlToken;
|
||||||
@@ -188,33 +187,20 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private AuthResult byUserName(String userName) {
|
private AuthResult byUserName(String userName) {
|
||||||
try {
|
List<AccountState> accountStates = queryProvider.get().byExternalId(SCHEME_USERNAME, userName);
|
||||||
List<AccountState> accountStates =
|
if (accountStates.isEmpty()) {
|
||||||
queryProvider.get().byExternalId(SCHEME_USERNAME, userName);
|
getServletContext().log("No accounts with username " + userName + " found");
|
||||||
if (accountStates.isEmpty()) {
|
|
||||||
getServletContext().log("No accounts with username " + userName + " found");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (accountStates.size() > 1) {
|
|
||||||
getServletContext().log("Multiple accounts with username " + userName + " found");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return auth(accountStates.get(0).getAccount().getId());
|
|
||||||
} catch (StorageException e) {
|
|
||||||
getServletContext().log("cannot query account index", e);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (accountStates.size() > 1) {
|
||||||
|
getServletContext().log("Multiple accounts with username " + userName + " found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return auth(accountStates.get(0).getAccount().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<AuthResult> byPreferredEmail(String email) {
|
private Optional<AuthResult> byPreferredEmail(String email) {
|
||||||
try {
|
return auth(queryProvider.get().byPreferredEmail(email).stream().findFirst());
|
||||||
Optional<AccountState> match =
|
|
||||||
queryProvider.get().byPreferredEmail(email).stream().findFirst();
|
|
||||||
return auth(match);
|
|
||||||
} catch (StorageException e) {
|
|
||||||
getServletContext().log("cannot query database", e);
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<AuthResult> byAccountId(String idStr) {
|
private Optional<AuthResult> byAccountId(String idStr) {
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||||||
|
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import com.google.gerrit.common.PageLinks;
|
import com.google.gerrit.common.PageLinks;
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.registration.DynamicItem;
|
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||||
import com.google.gerrit.httpd.CanonicalWebUrl;
|
import com.google.gerrit.httpd.CanonicalWebUrl;
|
||||||
import com.google.gerrit.httpd.HtmlDomUtil;
|
import com.google.gerrit.httpd.HtmlDomUtil;
|
||||||
@@ -128,7 +127,7 @@ class HttpLoginServlet extends HttpServlet {
|
|||||||
logger.atFine().log(
|
logger.atFine().log(
|
||||||
"Associating external identity \"%s\" to user \"%s\"", remoteExternalId, user);
|
"Associating external identity \"%s\" to user \"%s\"", remoteExternalId, user);
|
||||||
updateRemoteExternalId(arsp, remoteExternalId);
|
updateRemoteExternalId(arsp, remoteExternalId);
|
||||||
} catch (AccountException | StorageException | ConfigInvalidException e) {
|
} catch (AccountException | ConfigInvalidException e) {
|
||||||
logger.atSevere().withCause(e).log(
|
logger.atSevere().withCause(e).log(
|
||||||
"Unable to associate external identity \"%s\" to user \"%s\"", remoteExternalId, user);
|
"Unable to associate external identity \"%s\" to user \"%s\"", remoteExternalId, user);
|
||||||
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
|
|||||||
import com.google.common.base.CharMatcher;
|
import com.google.common.base.CharMatcher;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.auth.oauth.OAuthServiceProvider;
|
import com.google.gerrit.extensions.auth.oauth.OAuthServiceProvider;
|
||||||
import com.google.gerrit.extensions.auth.oauth.OAuthToken;
|
import com.google.gerrit.extensions.auth.oauth.OAuthToken;
|
||||||
import com.google.gerrit.extensions.auth.oauth.OAuthUserInfo;
|
import com.google.gerrit.extensions.auth.oauth.OAuthUserInfo;
|
||||||
@@ -188,7 +187,7 @@ class OAuthSession {
|
|||||||
logger.atInfo().log("OAuth2: linking claimed identity to %s", claimedId.get().toString());
|
logger.atInfo().log("OAuth2: linking claimed identity to %s", claimedId.get().toString());
|
||||||
try {
|
try {
|
||||||
accountManager.link(claimedId.get(), req);
|
accountManager.link(claimedId.get(), req);
|
||||||
} catch (StorageException | ConfigInvalidException e) {
|
} catch (ConfigInvalidException e) {
|
||||||
logger.atSevere().log(
|
logger.atSevere().log(
|
||||||
"Cannot link: %s to user identity:\n Claimed ID: %s is %s",
|
"Cannot link: %s to user identity:\n Claimed ID: %s is %s",
|
||||||
user.getExternalId(), claimedId.get(), claimedIdentifier);
|
user.getExternalId(), claimedId.get(), claimedIdentifier);
|
||||||
@@ -203,7 +202,7 @@ class OAuthSession {
|
|||||||
throws AccountException, IOException {
|
throws AccountException, IOException {
|
||||||
try {
|
try {
|
||||||
accountManager.link(identifiedUser.get().getAccountId(), areq);
|
accountManager.link(identifiedUser.get().getAccountId(), areq);
|
||||||
} catch (StorageException | ConfigInvalidException e) {
|
} catch (ConfigInvalidException e) {
|
||||||
logger.atSevere().log(
|
logger.atSevere().log(
|
||||||
"Cannot link: %s to user identity: %s",
|
"Cannot link: %s to user identity: %s",
|
||||||
user.getExternalId(), identifiedUser.get().getAccountId());
|
user.getExternalId(), identifiedUser.get().getAccountId());
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
|
|||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.auth.oauth.OAuthServiceProvider;
|
import com.google.gerrit.extensions.auth.oauth.OAuthServiceProvider;
|
||||||
import com.google.gerrit.extensions.auth.oauth.OAuthToken;
|
import com.google.gerrit.extensions.auth.oauth.OAuthToken;
|
||||||
import com.google.gerrit.extensions.auth.oauth.OAuthUserInfo;
|
import com.google.gerrit.extensions.auth.oauth.OAuthUserInfo;
|
||||||
@@ -164,7 +163,7 @@ class OAuthSessionOverOpenID {
|
|||||||
logger.atFine().log("Claimed account already exists: link to it.");
|
logger.atFine().log("Claimed account already exists: link to it.");
|
||||||
try {
|
try {
|
||||||
accountManager.link(claimedId.get(), areq);
|
accountManager.link(claimedId.get(), areq);
|
||||||
} catch (StorageException | ConfigInvalidException e) {
|
} catch (ConfigInvalidException e) {
|
||||||
logger.atSevere().log(
|
logger.atSevere().log(
|
||||||
"Cannot link: %s to user identity:\n Claimed ID: %s is %s",
|
"Cannot link: %s to user identity:\n Claimed ID: %s is %s",
|
||||||
user.getExternalId(), claimedId.get(), claimedIdentifier);
|
user.getExternalId(), claimedId.get(), claimedIdentifier);
|
||||||
@@ -178,7 +177,7 @@ class OAuthSessionOverOpenID {
|
|||||||
try {
|
try {
|
||||||
logger.atFine().log("Linking \"%s\" to \"%s\"", user.getExternalId(), accountId);
|
logger.atFine().log("Linking \"%s\" to \"%s\"", user.getExternalId(), accountId);
|
||||||
accountManager.link(accountId, areq);
|
accountManager.link(accountId, areq);
|
||||||
} catch (StorageException | ConfigInvalidException e) {
|
} catch (ConfigInvalidException e) {
|
||||||
logger.atSevere().log(
|
logger.atSevere().log(
|
||||||
"Cannot link: %s to user identity: %s", user.getExternalId(), accountId);
|
"Cannot link: %s to user identity: %s", user.getExternalId(), accountId);
|
||||||
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||||
|
|||||||
@@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
package com.google.gerrit.httpd.raw;
|
package com.google.gerrit.httpd.raw;
|
||||||
|
|
||||||
import com.google.gerrit.exceptions.StorageException;
|
|
||||||
import com.google.gerrit.extensions.restapi.AuthException;
|
import com.google.gerrit.extensions.restapi.AuthException;
|
||||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||||
import com.google.gerrit.extensions.restapi.Url;
|
import com.google.gerrit.extensions.restapi.Url;
|
||||||
@@ -145,7 +144,7 @@ public class CatServlet extends HttpServlet {
|
|||||||
} catch (ResourceConflictException | NoSuchChangeException | AuthException e) {
|
} catch (ResourceConflictException | NoSuchChangeException | AuthException e) {
|
||||||
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
return;
|
return;
|
||||||
} catch (StorageException | PermissionBackendException | IOException e) {
|
} catch (PermissionBackendException | IOException e) {
|
||||||
getServletContext().log("Cannot query database", e);
|
getServletContext().log("Cannot query database", e);
|
||||||
rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user