From 2d142b11c1ffe38a0fd1912c4829ca7901980a9c Mon Sep 17 00:00:00 2001
From: Edwin Kempin <edwin.kempin@sap.com>
Date: Wed, 21 Jan 2015 15:30:17 +0100
Subject: [PATCH] Fix exception handling when trying to parse an edit as
 anonymous user

Accessing a file in a change edit (http://host:8080/#/c/3/edit/test.txt)
as not signed in user failed with Internal Server Error, rather than
saying that the user must sign in.

Change-Id: I7f19e13e3f5663598daa0a4f63ca24a3f7eb844e
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
---
 .../server/api/changes/ChangeApiImpl.java     |  2 +-
 .../gerrit/server/change/Revisions.java       | 29 +++++++++----------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java
index e2d4a9200c..1a52b8746b 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/changes/ChangeApiImpl.java
@@ -126,7 +126,7 @@ class ChangeApiImpl extends ChangeApi.NotImplemented implements ChangeApi {
     try {
       return revisionApi.create(
           revisions.parse(change, IdString.fromDecoded(id)));
-    } catch (OrmException e) {
+    } catch (OrmException | IOException e) {
       throw new RestApiException("Cannot parse revision", e);
     }
   }
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java
index dc5e445b13..bb5775be0e 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java
@@ -69,7 +69,8 @@ public class Revisions implements ChildCollection<ChangeResource, RevisionResour
 
   @Override
   public RevisionResource parse(ChangeResource change, IdString id)
-      throws ResourceNotFoundException, OrmException {
+      throws ResourceNotFoundException, AuthException, OrmException,
+      IOException {
     if (id.equals("current")) {
       PatchSet.Id p = change.getChange().currentPatchSetId();
       PatchSet ps = p != null ? dbProvider.get().patchSets().get(p) : null;
@@ -103,7 +104,7 @@ public class Revisions implements ChildCollection<ChangeResource, RevisionResour
   }
 
   private List<RevisionResource> find(ChangeResource change, String id)
-      throws OrmException {
+      throws OrmException, IOException, AuthException {
     if (id.equals("0")) {
       return loadEdit(change, null);
     } else if (id.length() < 6 && id.matches("^[1-9][0-9]{0,4}$")) {
@@ -157,22 +158,18 @@ public class Revisions implements ChildCollection<ChangeResource, RevisionResour
   }
 
   private List<RevisionResource> loadEdit(ChangeResource change, RevId revid)
-      throws OrmException {
-    try {
-      Optional<ChangeEdit> edit = editUtil.byChange(change.getChange());
-      if (edit.isPresent()) {
-        PatchSet ps = new PatchSet(new PatchSet.Id(
-            change.getChange().getId(), 0));
-        ps.setRevision(edit.get().getRevision());
-        if (revid == null || edit.get().getRevision().equals(revid)) {
-          return Collections.singletonList(
-              new RevisionResource(change, ps, edit));
-        }
+      throws AuthException, IOException {
+    Optional<ChangeEdit> edit = editUtil.byChange(change.getChange());
+    if (edit.isPresent()) {
+      PatchSet ps = new PatchSet(new PatchSet.Id(
+          change.getChange().getId(), 0));
+      ps.setRevision(edit.get().getRevision());
+      if (revid == null || edit.get().getRevision().equals(revid)) {
+        return Collections.singletonList(
+            new RevisionResource(change, ps, edit));
       }
-      return Collections.emptyList();
-    } catch (AuthException | IOException e) {
-      throw new OrmException(e);
     }
+    return Collections.emptyList();
   }
 
   private static List<RevisionResource> toResources(final ChangeResource change,