Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilou242 committed Jan 14, 2025
1 parent 44dc5e8 commit f42d2a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import ai.startree.thirdeye.scheduler.events.MockEventsLoader;
import ai.startree.thirdeye.service.ResourcesBootstrapService;
import ai.startree.thirdeye.spi.Constants;
import ai.startree.thirdeye.spi.ThirdEyeStatus;
import ai.startree.thirdeye.spi.datalayer.bao.AbstractManager;
import ai.startree.thirdeye.worker.task.TaskDriver;
import ch.qos.logback.classic.Level;
Expand Down Expand Up @@ -80,7 +79,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.servlets.CrossOriginFilter;
Expand Down Expand Up @@ -155,8 +153,7 @@ public void run(final ThirdEyeServerConfiguration configuration, final Environme
registerResources(env.jersey());
env.jersey().register(new ThirdEyeJsonProcessingExceptionMapper());
env.jersey().register(new ThirdEyeExceptionMapper());
env.jersey().register(new GenericExceptionMapper<>(TimeoutException.class, ThirdEyeStatus.ERR_TIMEOUT));
env.jersey().register(new GenericExceptionMapper<>(Throwable.class, ThirdEyeStatus.ERR_UNKNOWN));
env.jersey().register(new GenericExceptionMapper());

// Persistence layer connectivity health check registry
env.healthChecks().register("database", injector.getInstance(DatabaseHealthCheck.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,32 @@
import io.dropwizard.jersey.errors.LoggingExceptionMapper;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This mapper has lesser priority than all others.
* Most Exceptions thrown implemented in this code base should be wrapped inside a ThirdEyeException
* and will benefit from the ThirdEyeExceptionMapper
* Complete this class only for exceptions that are hard to wrap with a ThirdEyeException.
*/
@Provider
public class GenericExceptionMapper<E extends Throwable> extends LoggingExceptionMapper<E> implements
ExceptionMapper<E> {
public class GenericExceptionMapper extends LoggingExceptionMapper<Throwable> {

private final Class<E> clazz;
private final ThirdEyeStatus status;
private final Logger logger;
private static final Logger LOG = LoggerFactory.getLogger(GenericExceptionMapper.class);

public GenericExceptionMapper(final Class<E> clazz, final ThirdEyeStatus status) {
this.clazz = clazz;
this.status = status;
this.logger = LoggerFactory.getLogger(GenericExceptionMapper.class.toString() + "." + clazz.getSimpleName());
public GenericExceptionMapper() {
}

@Override
public Response toResponse(final E exception) {
logger.debug(
public Response toResponse(final Throwable exception) {
final ThirdEyeStatus status = statusFor(exception);
LOG.debug(
"Request failed because of a {}. Returning error code {}",
clazz.getSimpleName(),
exception.getClass().getSimpleName(),
status.getRecommendedStatusCode());
final StatusApi statusApi = new StatusApi()
.setCode(status)
Expand All @@ -58,5 +59,12 @@ public Response toResponse(final E exception) {
.entity(statusList)
.build();
}


private static ThirdEyeStatus statusFor(final Throwable exception) {
if (exception instanceof TimeoutException) {
return ThirdEyeStatus.ERR_TIMEOUT;
} else {
return ThirdEyeStatus.ERR_UNKNOWN;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -142,13 +141,11 @@ public Response runTask(
// can be moved to CrudResource if /validate is needed for other entities.
public Response validateMultiple(
@Parameter(hidden = true) @Auth final ThirdEyeServerPrincipal principal,
final List<AlertApi> list) throws TimeoutException {
throw new TimeoutException("a timeout happened");

//ensureExists(list, "Invalid request");
final List<AlertApi> list) {
ensureExists(list, "Invalid request");

//alertService.validateMultiple(principal, list);
//return Response.ok().build();
alertService.validateMultiple(principal, list);
return Response.ok().build();
}

@Path("evaluate")
Expand Down

0 comments on commit f42d2a2

Please sign in to comment.