Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose ExceptionClass only in debug mode #584

Merged
merged 11 commits into from
Oct 25, 2023
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
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;
tatu-at-datastax marked this conversation as resolved.
Show resolved Hide resolved
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
// enable debug mode for unit tests, since it can not be injected
tatu-at-datastax marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -13,7 +13,7 @@ public DseTestResource() {

if (null == System.getProperty("testing.containers.cassandra-image")) {
System.setProperty(
"testing.containers.cassandra-image", "stargateio/dse-next:4.0.7-336cdd7405ee");
"testing.containers.cassandra-image", "stargateio/dse-next:4.0.11-45d4657e507e");
}

if (null == System.getProperty("testing.containers.stargate-image")) {
Expand All @@ -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