diff options
4 files changed, 119 insertions, 0 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java index adb63e13a23..bb4c43d8244 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/queue/InternalCeQueueImpl.java @@ -41,6 +41,7 @@ import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDao; import org.sonar.db.ce.CeQueueDto; import org.sonar.server.computation.task.projectanalysis.component.VisitException; +import org.sonar.server.computation.task.step.TypedException; import org.sonar.server.organization.DefaultOrganizationProvider; import static com.google.common.base.Preconditions.checkArgument; @@ -131,6 +132,9 @@ public class InternalCeQueueImpl extends CeQueueImpl implements InternalCeQueue if (stacktrace != null) { activityDto.setErrorStacktrace(stacktrace); } + if (error instanceof TypedException) { + activityDto.setErrorType(((TypedException) error).getType()); + } } @CheckForNull diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java index c54c5bbad1d..3626afc8a63 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/queue/InternalCeQueueImplTest.java @@ -46,6 +46,7 @@ import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; import org.sonar.db.organization.OrganizationDto; +import org.sonar.server.computation.task.step.TypedException; import org.sonar.server.organization.DefaultOrganization; import org.sonar.server.organization.DefaultOrganizationProvider; @@ -236,6 +237,21 @@ public class InternalCeQueueImplTest { assertThat(activityDto.get().getErrorMessage()).isEqualTo(error.getMessage()); assertThat(activityDto.get().getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error)); + assertThat(activityDto.get().getErrorType()).isNull(); + } + + @Test + public void remove_saves_error_when_TypedMessageException_is_provided() { + Throwable error = new TypedException("aType", "aMessage"); + + CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1"); + Optional<CeTask> peek = underTest.peek(WORKER_UUID_1); + underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error); + + CeActivityDto activityDto = dbTester.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()).get(); + assertThat(activityDto.getErrorType()).isEqualTo("aType"); + assertThat(activityDto.getErrorMessage()).isEqualTo("aMessage"); + assertThat(activityDto.getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error)); } @Test diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/TypedException.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/TypedException.java new file mode 100644 index 00000000000..fb1197a43d4 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/step/TypedException.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.computation.task.step; + +import static java.util.Objects.requireNonNull; + +public class TypedException extends RuntimeException { + + private final String type; + + public TypedException(String type, String message) { + super(message); + this.type = requireNonNull(type, "Type must not be null"); + } + + public String getType() { + return type; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + sb.append("type='").append(type).append("',"); + sb.append("message='").append(getMessage()).append("'"); + sb.append(']'); + return sb.toString(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/step/TypedExceptionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/step/TypedExceptionTest.java new file mode 100644 index 00000000000..80dfe6cbadb --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/step/TypedExceptionTest.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.computation.task.step; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TypedExceptionTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void should_create_exception() { + TypedException exception = new TypedException("aType", "the message"); + assertThat(exception.getMessage()).isEqualTo("the message"); + assertThat(exception.getType()).isEqualTo("aType"); + } + + @Test + public void test_toString() { + TypedException exception = new TypedException("aType", "the message"); + + assertThat(exception.toString()).isEqualTo("[type='aType',message='the message']"); + } + + @Test + public void throw_NPE_if_type_is_null() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("Type must not be null"); + + new TypedException(null, "the message"); + } +} |