Skip to content

Commit

Permalink
chore(osv-client): Improve error messages
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
sschuberth committed Jul 4, 2023
1 parent 0726d0f commit 1f08feb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions clients/osv/src/main/kotlin/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,9 @@ data class Severity(
CVSS_V3
}
}

@Serializable
data class ErrorResponse(
val code: Int,
val message: String
)
9 changes: 8 additions & 1 deletion clients/osv/src/main/kotlin/OsvService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorResponse>(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 }
Expand Down

0 comments on commit 1f08feb

Please sign in to comment.