Skip to content

Commit 1f08feb

Browse files
committed
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 <[email protected]>
1 parent 0726d0f commit 1f08feb

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clients/osv/src/main/kotlin/Model.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,9 @@ data class Severity(
180180
CVSS_V3
181181
}
182182
}
183+
184+
@Serializable
185+
data class ErrorResponse(
186+
val code: Int,
187+
val message: String
188+
)

clients/osv/src/main/kotlin/OsvService.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ class OsvService(serverUrl: String? = null, httpClient: OkHttpClient? = null) {
6767
val response = client.getVulnerabilityIdsForPackages(batchRequest).execute()
6868
val body = response.body()
6969

70-
if (!response.isSuccessful || body == null) return Result.failure(IOException(response.message()))
70+
if (!response.isSuccessful || body == null) {
71+
val errorMessage = response.errorBody()?.string()?.let {
72+
val errorResponse = OsvApiClient.JSON.decodeFromString<ErrorResponse>(it)
73+
"Error code ${errorResponse.code}: ${errorResponse.message}"
74+
} ?: with(response) { "HTTP code ${code()}: ${message()}" }
75+
76+
return Result.failure(IOException(errorMessage))
77+
}
7178

7279
result += body.results.map { batchResponse ->
7380
batchResponse.vulnerabilities.mapTo(mutableListOf()) { it.id }

0 commit comments

Comments
 (0)