Skip to content

Commit

Permalink
expose ExceptionClass only in debug mode (#584)
Browse files Browse the repository at this point in the history
Co-authored-by: Tatu Saloranta <tatu.saloranta@datastax.com>
  • Loading branch information
Yuqi-Du and tatu-at-datastax authored Oct 25, 2023
1 parent 0164be7 commit c74956d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/main/java/io/stargate/sgv2/jsonapi/JsonApiStartUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.StartupEvent;
import io.stargate.sgv2.api.common.properties.datastore.DataStoreProperties;
import io.stargate.sgv2.jsonapi.config.DebugModeConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
Expand All @@ -14,13 +15,16 @@ public class JsonApiStartUp {

private static final Logger LOGGER = LoggerFactory.getLogger(JsonApiStartUp.class);
private final DataStoreProperties dataStoreProperties;
private final DebugModeConfig config;

@Inject
public JsonApiStartUp(DataStoreProperties dataStoreProperties) {
public JsonApiStartUp(DataStoreProperties dataStoreProperties, DebugModeConfig config) {
this.dataStoreProperties = dataStoreProperties;
this.config = config;
}

void onStart(@Observes StartupEvent ev) {
LOGGER.info(String.format("DEBUG mode Enabled: %s", config.enabled()));
LOGGER.info(
String.format("VectorSearch Enabled: %s", dataStoreProperties.vectorSearchEnabled()));
LOGGER.info(
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/stargate/sgv2/jsonapi/config/DebugModeConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.stargate.sgv2.jsonapi.config;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "stargate.debug")
public interface DebugModeConfig {

@WithDefault("false")
boolean enabled();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.stargate.sgv2.jsonapi.exception;

import io.smallrye.config.SmallRyeConfig;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.config.DebugModeConfig;
import io.stargate.sgv2.jsonapi.exception.mappers.ThrowableToErrorMapper;
import jakarta.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.eclipse.microprofile.config.ConfigProvider;

/**
* Our own {@link RuntimeException} that uses {@link ErrorCode} to describe the exception cause.
Expand Down Expand Up @@ -58,8 +61,17 @@ public CommandResult get() {
}

public CommandResult.Error getCommandResultError(String message) {
Map<String, Object> fields =
Map.of("errorCode", errorCode.name(), "exceptionClass", this.getClass().getSimpleName());
Map<String, Object> fields = null;
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
// enable debug mode for unit tests, since it can not be injected
DebugModeConfig debugModeConfig = config.getConfigMapping(DebugModeConfig.class);
final boolean debugEnabled = debugModeConfig.enabled();
if (debugEnabled) {
fields =
Map.of("errorCode", errorCode.name(), "exceptionClass", this.getClass().getSimpleName());
} else {
fields = Map.of("errorCode", errorCode.name());
}
return new CommandResult.Error(message, fields, Response.Status.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.smallrye.config.SmallRyeConfig;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.config.DebugModeConfig;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import jakarta.ws.rs.core.Response;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.eclipse.microprofile.config.ConfigProvider;

/**
* Simple mapper for mapping {@link Throwable}s to {@link CommandResult.Error}, with a default
Expand All @@ -22,9 +25,12 @@ public final class ThrowableToErrorMapper {
return jae.getCommandResultError(message);
}
// Override response error code
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
DebugModeConfig debugModeConfig = config.getConfigMapping(DebugModeConfig.class);
final boolean debugEnabled = debugModeConfig.enabled();
if (throwable instanceof StatusRuntimeException sre) {
Map<String, Object> fields =
Map.of("exceptionClass", throwable.getClass().getSimpleName());
debugEnabled ? Map.of("exceptionClass", throwable.getClass().getSimpleName()) : null;
if (sre.getStatus().getCode() == Status.Code.UNAUTHENTICATED) {
return new CommandResult.Error(message, fields, Response.Status.UNAUTHORIZED);
} else if (sre.getStatus().getCode() == Status.Code.INTERNAL) {
Expand All @@ -36,7 +42,8 @@ public final class ThrowableToErrorMapper {
}
}
// add error code as error field
Map<String, Object> fields = Map.of("exceptionClass", throwable.getClass().getSimpleName());
Map<String, Object> fields =
debugEnabled ? Map.of("exceptionClass", throwable.getClass().getSimpleName()) : null;
return new CommandResult.Error(message, fields, Response.Status.OK);
};

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

stargate:

debug:
enabled: false

# disable all sgv2 exception mappers, handled differently
exception-mappers:
enabled: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.stargate.sgv2.common.testprofiles.NoGlobalResourcesTestProfile;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@QuarkusTest
@TestProfile(NoGlobalResourcesTestProfile.Impl.class)
class JsonApiExceptionTest {

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.stargate.sgv2.common.testprofiles.NoGlobalResourcesTestProfile;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@QuarkusTest
@TestProfile(NoGlobalResourcesTestProfile.Impl.class)
class ThrowableCommandResultSupplierTest {

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Map<String, String> start() {
propsBuilder.put(
"stargate.jsonapi.embedding.service.custom.clazz",
"io.stargate.sgv2.jsonapi.service.embedding.operation.test.CustomITEmbeddingService");
propsBuilder.put("stargate.debug.enabled", "true");
return propsBuilder.build();
}
}
2 changes: 2 additions & 0 deletions src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
stargate:
data-store:
ignore-bridge: true
debug:
enabled: true

# change test port from 8081 (used by other SG services)
quarkus:
Expand Down

0 comments on commit c74956d

Please sign in to comment.