Skip to content

Commit

Permalink
Merge pull request #125 from dwasinge/exception-logging
Browse files Browse the repository at this point in the history
Reduce Exception Logging For Status Not Found
  • Loading branch information
mcanoy authored Feb 25, 2021
2 parents e8d294d + ece2b70 commit 403adaa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ public Response findAllProjectHooks(@PathParam("customer") String customer,
@Timed(name = "performedStatusGet", description = "Time to get status", unit = MetricUnits.MILLISECONDS)
public Response getStatus(@PathParam("customer") String customer, @PathParam("engagement") String engagement) {

Status status = engagementService.getProjectStatus(customer, engagement);
return Response.ok().entity(status).build();
Optional<Status> status = engagementService.getProjectStatus(customer, engagement);
if(status.isPresent()) {
return Response.ok().entity(status).build();
}
return Response.status(404).build();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand Down Expand Up @@ -191,7 +190,7 @@ private Status getProjectStatusFile(String customerName, String engagementName)
return status;
}

public Status getProjectStatus(String customerName, String engagementName) {
public Optional<Status> getProjectStatus(String customerName, String engagementName) {

List<ProjectTreeNode> nodes = projectService
.getProjectTree(GitLabPathUtils.getValidPath(engagementPathPrefix, customerName, engagementName));
Expand All @@ -200,11 +199,11 @@ public Status getProjectStatus(String customerName, String engagementName) {
List<ProjectTreeNode> status = nodes.stream().filter(node -> STATUS_FILE.equals(node.getName()))
.collect(Collectors.toList());
if (status.isEmpty()) {
throw new WebApplicationException("failed to find status.json", 404);
return Optional.empty();
}

// get status
return getProjectStatusFile(customerName, engagementName);
return Optional.of(getProjectStatusFile(customerName, engagementName));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
Expand All @@ -23,6 +24,7 @@

import com.redhat.labs.lodestar.exception.UnexpectedGitLabResponseException;
import com.redhat.labs.lodestar.models.Engagement;
import com.redhat.labs.lodestar.models.Status;
import com.redhat.labs.lodestar.models.gitlab.Group;
import com.redhat.labs.lodestar.models.gitlab.Hook;
import com.redhat.labs.lodestar.models.gitlab.Project;
Expand Down Expand Up @@ -191,12 +193,9 @@ void setup() {
given(gitLabService.getProjectTree(Mockito.anyString(), Mockito.anyBoolean())).willReturn(r);
given(gitLabService.getFile(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).willReturn(null);

WebApplicationException exception = assertThrows(WebApplicationException.class, () -> {
engagementService.getProjectStatus("nope", "nada");
});
Optional<Status> status = engagementService.getProjectStatus("nope", "nada");

assertEquals(404, exception.getResponse().getStatus());
assertEquals("failed to find status.json", exception.getMessage());
assertTrue(status.isEmpty());

}

Expand Down

0 comments on commit 403adaa

Please sign in to comment.