From 4fec3d9d21f94ddfb2cfaa4497b130b1810b46e3 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 28 Oct 2014 16:11:44 +0100 Subject: [PATCH] Compute Engine - give context when failing to retrieve a snapshot --- .../org/sonar/server/computation/SwitchSnapshotStep.java | 8 +++++++- .../sonar/server/computation/SwitchSnapshotStepTest.java | 7 +++++++ .../server/computation/SwitchSnapshotStepTest/empty.xml | 3 +++ .../org/sonar/core/computation/db/AnalysisReportDto.java | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/SwitchSnapshotStepTest/empty.xml diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java index a1f3dbf7993..a078a77ee0b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java @@ -47,7 +47,13 @@ public class SwitchSnapshotStep implements ComputationStep { } private void disablePreviousSnapshot(DbSession session, AnalysisReportDto report) { - SnapshotDto referenceSnapshot = dao.getByKey(session, report.getSnapshotId()); + SnapshotDto referenceSnapshot; + + try { + referenceSnapshot = dao.getByKey(session, report.getSnapshotId()); + } catch (Exception exception) { + throw new IllegalStateException(String.format("Unexpected error while trying to retrieve snapshot of analysis %s", report), exception); + } List snapshots = dao.findSnapshotAndChildrenOfProjectScope(session, referenceSnapshot); for (SnapshotDto snapshot : snapshots) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/SwitchSnapshotStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/SwitchSnapshotStepTest.java index e1f74f4cb19..c07035e7975 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/SwitchSnapshotStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/SwitchSnapshotStepTest.java @@ -65,4 +65,11 @@ public class SwitchSnapshotStepTest { db.assertDbUnit(getClass(), "snapshots-result.xml", "snapshots"); } + + @Test(expected = IllegalStateException.class) + public void throw_IllegalStateException_when_not_finding_snapshot() { + db.prepareDbUnit(getClass(), "empty.xml"); + + sut.execute(session, AnalysisReportDto.newForTests(1L).setSnapshotId(1L)); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/SwitchSnapshotStepTest/empty.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/SwitchSnapshotStepTest/empty.xml new file mode 100644 index 00000000000..871dedcb5e9 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/SwitchSnapshotStepTest/empty.xml @@ -0,0 +1,3 @@ + + + diff --git a/sonar-core/src/main/java/org/sonar/core/computation/db/AnalysisReportDto.java b/sonar-core/src/main/java/org/sonar/core/computation/db/AnalysisReportDto.java index 1491a1ae19c..dd54bb2cb47 100644 --- a/sonar-core/src/main/java/org/sonar/core/computation/db/AnalysisReportDto.java +++ b/sonar-core/src/main/java/org/sonar/core/computation/db/AnalysisReportDto.java @@ -106,6 +106,7 @@ public class AnalysisReportDto extends Dto { return Objects.toStringHelper(this) .add("id", getId()) .add("projectKey", getProjectKey()) + .add("snapshotId", getSnapshotId()) .add("status", getStatus()) .add("createdAt", getCreatedAt()) .add("startedAt", getStartedAt()) -- 2.39.5