diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/BadRequestException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/BadRequestException.kt index e7ce096f0..551a688f8 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/BadRequestException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/BadRequestException.kt @@ -10,7 +10,7 @@ import kotlin.jvm.optionals.getOrNull class BadRequestException private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) : - LangChainServiceException("400: $body", cause) { + LangChainServiceException(formatMessage(400, body), cause) { override fun statusCode(): Int = 400 diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/InternalServerException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/InternalServerException.kt index c7e239181..982ff78fb 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/InternalServerException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/InternalServerException.kt @@ -14,7 +14,7 @@ private constructor( private val headers: Headers, private val body: JsonValue, cause: Throwable?, -) : LangChainServiceException("$statusCode: $body", cause) { +) : LangChainServiceException(formatMessage(statusCode, body), cause) { override fun statusCode(): Int = statusCode diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/LangChainServiceException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/LangChainServiceException.kt index def4778b1..26d89bc9c 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/LangChainServiceException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/LangChainServiceException.kt @@ -2,6 +2,7 @@ package com.langchain.smith.errors +import com.fasterxml.jackson.databind.ObjectMapper import com.langchain.smith.core.JsonValue import com.langchain.smith.core.http.Headers @@ -14,4 +15,19 @@ protected constructor(message: String, cause: Throwable? = null) : abstract fun headers(): Headers abstract fun body(): JsonValue + + companion object { + + /** Formats a status code and [JsonValue] error body into an error message. */ + @JvmStatic + fun formatMessage(statusCode: Int, body: JsonValue): String { + val bodyStr = + try { + ObjectMapper().writeValueAsString(body) + } catch (_: Exception) { + body.toString() + } + return "$statusCode: $bodyStr" + } + } } diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/NotFoundException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/NotFoundException.kt index a070b703e..74c3b1e71 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/NotFoundException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/NotFoundException.kt @@ -10,7 +10,7 @@ import kotlin.jvm.optionals.getOrNull class NotFoundException private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) : - LangChainServiceException("404: $body", cause) { + LangChainServiceException(formatMessage(404, body), cause) { override fun statusCode(): Int = 404 diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/PermissionDeniedException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/PermissionDeniedException.kt index 5e2d1f819..d338288b4 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/PermissionDeniedException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/PermissionDeniedException.kt @@ -10,7 +10,7 @@ import kotlin.jvm.optionals.getOrNull class PermissionDeniedException private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) : - LangChainServiceException("403: $body", cause) { + LangChainServiceException(formatMessage(403, body), cause) { override fun statusCode(): Int = 403 diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/RateLimitException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/RateLimitException.kt index b8eda17b1..066d694a1 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/RateLimitException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/RateLimitException.kt @@ -10,7 +10,7 @@ import kotlin.jvm.optionals.getOrNull class RateLimitException private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) : - LangChainServiceException("429: $body", cause) { + LangChainServiceException(formatMessage(429, body), cause) { override fun statusCode(): Int = 429 diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnauthorizedException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnauthorizedException.kt index d2c472329..7323d1806 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnauthorizedException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnauthorizedException.kt @@ -10,7 +10,7 @@ import kotlin.jvm.optionals.getOrNull class UnauthorizedException private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) : - LangChainServiceException("401: $body", cause) { + LangChainServiceException(formatMessage(401, body), cause) { override fun statusCode(): Int = 401 diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnexpectedStatusCodeException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnexpectedStatusCodeException.kt index d66069ed3..6c41c6233 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnexpectedStatusCodeException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnexpectedStatusCodeException.kt @@ -14,7 +14,7 @@ private constructor( private val headers: Headers, private val body: JsonValue, cause: Throwable?, -) : LangChainServiceException("$statusCode: $body", cause) { +) : LangChainServiceException(formatMessage(statusCode, body), cause) { override fun statusCode(): Int = statusCode diff --git a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnprocessableEntityException.kt b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnprocessableEntityException.kt index 5b38550df..e18c5988e 100644 --- a/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnprocessableEntityException.kt +++ b/langsmith-java-core/src/main/kotlin/com/langchain/smith/errors/UnprocessableEntityException.kt @@ -10,7 +10,7 @@ import kotlin.jvm.optionals.getOrNull class UnprocessableEntityException private constructor(private val headers: Headers, private val body: JsonValue, cause: Throwable?) : - LangChainServiceException("422: $body", cause) { + LangChainServiceException(formatMessage(422, body), cause) { override fun statusCode(): Int = 422 diff --git a/langsmith-java-core/src/test/kotlin/com/langchain/smith/errors/ServiceExceptionErrorMessageTest.kt b/langsmith-java-core/src/test/kotlin/com/langchain/smith/errors/ServiceExceptionErrorMessageTest.kt new file mode 100644 index 000000000..bfa2a7e05 --- /dev/null +++ b/langsmith-java-core/src/test/kotlin/com/langchain/smith/errors/ServiceExceptionErrorMessageTest.kt @@ -0,0 +1,60 @@ +package com.langchain.smith.errors + +import com.langchain.smith.client.okhttp.LangsmithOkHttpClient +import com.langchain.smith.models.runs.Run +import com.langchain.smith.models.runs.RunIngestBatchParams +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Assumptions.assumeTrue +import org.junit.jupiter.api.Test + +/** + * Verifies that service exception error messages contain the actual API error body rather than + * opaque strings like `[object Object]`. + * + * Run with: + * ```bash + * ./gradlew :langsmith-java-core:test --tests "com.langchain.smith.errors.ServiceExceptionErrorMessageTest" + * ``` + */ +internal class ServiceExceptionErrorMessageTest { + + @Test + fun forbiddenErrorContainsDetail() { + val client = LangsmithOkHttpClient.builder().apiKey("ls-invalid-key").build() + + assertThatThrownBy { client.runs().ingestBatch() } + .isInstanceOf(PermissionDeniedException::class.java) + .satisfies({ e -> + val message = e.message ?: "" + assertThat(message).startsWith("403:") + assertThat(message).doesNotContain("[object Object]") + assertThat(message).contains("Forbidden") + }) + } + + @Test + fun badRequestErrorContainsDetail() { + assumeTrue( + !System.getenv("LANGSMITH_API_KEY").isNullOrBlank(), + "Skipping: LANGSMITH_API_KEY must be set", + ) + val client = LangsmithOkHttpClient.fromEnv() + + val params = + RunIngestBatchParams.builder() + .addPost( + Run.builder().id("not-a-uuid").name("test").runType(Run.RunType.CHAIN).build() + ) + .build() + + assertThatThrownBy { client.runs().ingestBatch(params) } + .isInstanceOf(UnprocessableEntityException::class.java) + .satisfies({ e -> + val message = e.message ?: "" + assertThat(message).startsWith("422:") + assertThat(message).doesNotContain("[object Object]") + assertThat(message).contains("error") + }) + } +}