Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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")
})
}
}
Loading