diff --git a/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java b/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java index cf97141a21..a808440b5e 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/exception/mappers/ThrowableToErrorMapper.java @@ -34,8 +34,7 @@ public final class ThrowableToErrorMapper { if (throwable instanceof UnauthorizedException) { return ErrorCode.UNAUTHENTICATED_REQUEST .toApiException() - .getCommandResultError( - ErrorCode.UNAUTHENTICATED_REQUEST.getMessage(), Response.Status.UNAUTHORIZED); + .getCommandResultError(message, Response.Status.UNAUTHORIZED); } // handle all driver exceptions diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java b/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java index 7eb85b3342..edb9a349f9 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/CQLSessionCache.java @@ -13,6 +13,7 @@ import io.stargate.sgv2.jsonapi.JsonApiStartUp; import io.stargate.sgv2.jsonapi.api.request.DataApiRequestInfo; import io.stargate.sgv2.jsonapi.config.OperationsConfig; +import io.stargate.sgv2.jsonapi.exception.ErrorCode; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import java.net.InetSocketAddress; @@ -135,7 +136,7 @@ private CqlSession getNewSession(SessionCacheKey cacheKey) { builder.withAuthCredentials( Objects.requireNonNull(upc.userName()), Objects.requireNonNull(upc.password())); } else { - throw new RuntimeException( + throw new UnauthorizedException( "Invalid credentials format, expected `Cassandra:Base64(username):Base64(password)`"); } } else { @@ -166,7 +167,7 @@ public CqlSession getSession() { String fixedToken; if ((fixedToken = getFixedToken()) != null && !dataApiRequestInfo.getCassandraToken().orElseThrow().equals(fixedToken)) { - throw new UnauthorizedException("Unauthorized"); + throw new UnauthorizedException(ErrorCode.UNAUTHENTICATED_REQUEST.getMessage()); } return sessionCache.get(getSessionCacheKey()); } @@ -239,7 +240,7 @@ private record UsernamePasswordCredentials(String userName, String password) public static UsernamePasswordCredentials from(String encodedCredentials) { String[] parts = encodedCredentials.split(":"); if (parts.length != 3) { - throw new RuntimeException( + throw new UnauthorizedException( "Invalid credentials format, expected `Cassandra:Base64(username):Base64(password)`"); } try { @@ -247,7 +248,7 @@ public static UsernamePasswordCredentials from(String encodedCredentials) { String password = new String(Base64.getDecoder().decode(parts[2])); return new UsernamePasswordCredentials(userName, password); } catch (Exception e) { - throw new RuntimeException( + throw new UnauthorizedException( "Invalid credentials format, expected `Cassandra:Base64(username):Base64(password)`"); } } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTests.java b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTests.java index c925637244..da2718b2c2 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTests.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/cqldriver/CqlSessionCacheTests.java @@ -137,7 +137,10 @@ public void testOSSCxCQLSessionCacheWithInvalidFixedToken() operationsConfigField.set(cqlSessionCacheForTest, operationsConfig); // Throwable Throwable t = catchThrowable(cqlSessionCacheForTest::getSession); - assertThat(t).isNotNull().isInstanceOf(UnauthorizedException.class).hasMessage("Unauthorized"); + assertThat(t) + .isNotNull() + .isInstanceOf(UnauthorizedException.class) + .hasMessage("UNAUTHENTICATED: Invalid token"); // metrics test Gauge cacheSizeMetric = meterRegistry.find("cache.size").tag("cache", "cql_sessions_cache").gauge();