Skip to content

Commit 43a23b1

Browse files
googsvgclaude
andcommitted
test: add coverage for null error object fallback path
Add test case for the scenario where API returns valid JSON but the error object parses to null, triggering the final fallback error message path in NylasClient. This improves test coverage for the enhanced error handling introduced in the previous commit. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent b84b4d1 commit 43a23b1

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/test/kotlin/com/nylas/NylasClientTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,5 +734,32 @@ class NylasClientTest {
734734
assertNotNull(exception.providerError)
735735
assertEquals("invalid_grant", exception.providerError!!["error"])
736736
}
737+
738+
@Test
739+
fun `API error with valid JSON but null error object returns helpful fallback message`() {
740+
// This tests the final fallback path when JSON parsing succeeds but returns null
741+
val validJsonButNullError = """{"some_field": "some_value"}"""
742+
743+
val mockResponse = mock(okhttp3.Response::class.java)
744+
val mockBody = mock(ResponseBody::class.java)
745+
746+
whenever(mockResponse.isSuccessful).thenReturn(false)
747+
whenever(mockResponse.code).thenReturn(503)
748+
whenever(mockResponse.body).thenReturn(mockBody)
749+
whenever(mockBody.string()).thenReturn(validJsonButNullError)
750+
whenever(mockResponse.headers).thenReturn(Headers.headersOf())
751+
whenever(mockCall.execute()).thenReturn(mockResponse)
752+
753+
val client = NylasClient("test-api-key", mockOkHttpClientBuilder)
754+
755+
val exception = assertFailsWith<NylasApiError> {
756+
client.grants().list()
757+
}
758+
759+
assertEquals("unknown", exception.type)
760+
assert(exception.message.contains("API request failed with status 503"))
761+
assert(exception.message.contains("Response body:"))
762+
assertEquals(503, exception.statusCode)
763+
}
737764
}
738765
}

0 commit comments

Comments
 (0)