From 1f08feb8665a680f852ff376e1fe0673d98faea9 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 4 Jul 2023 08:51:40 +0200 Subject: [PATCH] chore(osv-client): Improve error messages Decode the dedicated error body in case of unsuccessful responses, and fall back to a message that includes the HTTP code otherwise. Signed-off-by: Sebastian Schuberth --- clients/osv/src/main/kotlin/Model.kt | 6 ++++++ clients/osv/src/main/kotlin/OsvService.kt | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clients/osv/src/main/kotlin/Model.kt b/clients/osv/src/main/kotlin/Model.kt index e75b4051f2ba1..24a726759f832 100644 --- a/clients/osv/src/main/kotlin/Model.kt +++ b/clients/osv/src/main/kotlin/Model.kt @@ -180,3 +180,9 @@ data class Severity( CVSS_V3 } } + +@Serializable +data class ErrorResponse( + val code: Int, + val message: String +) diff --git a/clients/osv/src/main/kotlin/OsvService.kt b/clients/osv/src/main/kotlin/OsvService.kt index 6a90bc7b85484..fb535722e5af3 100644 --- a/clients/osv/src/main/kotlin/OsvService.kt +++ b/clients/osv/src/main/kotlin/OsvService.kt @@ -67,7 +67,14 @@ class OsvService(serverUrl: String? = null, httpClient: OkHttpClient? = null) { val response = client.getVulnerabilityIdsForPackages(batchRequest).execute() val body = response.body() - if (!response.isSuccessful || body == null) return Result.failure(IOException(response.message())) + if (!response.isSuccessful || body == null) { + val errorMessage = response.errorBody()?.string()?.let { + val errorResponse = OsvApiClient.JSON.decodeFromString(it) + "Error code ${errorResponse.code}: ${errorResponse.message}" + } ?: with(response) { "HTTP code ${code()}: ${message()}" } + + return Result.failure(IOException(errorMessage)) + } result += body.results.map { batchResponse -> batchResponse.vulnerabilities.mapTo(mutableListOf()) { it.id }