Skip to content

Commit ff93ebf

Browse files
Ian BirdIanDBird
Ian Bird
authored andcommitted
Improve error reporting by including optional error message
1 parent 898095f commit ff93ebf

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

sdk/src/main/java/com/uid2/UID2Client.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ internal class UID2Client(
111111
// Attempt to make the request via the provided NetworkSession.
112112
val response = session.loadData(url, request)
113113
if (response.code != HttpURLConnection.HTTP_OK) {
114-
logger.e(TAG) { "Client details failure: ${response.code}" }
115-
throw RequestFailureException(response.code)
114+
logger.e(TAG) { "Client details failure: ${response.code} ${response.data}" }
115+
throw RequestFailureException(response.code, response.data)
116116
}
117117

118118
// The response should be an encrypted payload. Let's attempt to decrypt it using the key we were provided.

sdk/src/main/java/com/uid2/UID2Exception.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class InputValidationException(description: String?) : UID2Exception(desc
2828
/**
2929
* An attempt was made to the API that resulted in a failure.
3030
*/
31-
internal class RequestFailureException(val statusCode: Int) : UID2Exception()
31+
internal class RequestFailureException(val statusCode: Int, message: String? = null) : UID2Exception(message)
3232

3333
/**
3434
* The encrypted payload could not be decrypted successfully.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.uid2.network
22

3+
import java.io.IOException
34
import java.net.HttpURLConnection
45
import java.net.URL
56

@@ -16,35 +17,46 @@ public open class DefaultNetworkSession : NetworkSession {
1617
* Loads the given [URL] and [NetworkRequest] using [HttpURLConnection].
1718
*/
1819
override fun loadData(url: URL, request: NetworkRequest): NetworkResponse {
19-
val connection = openConnection(url).apply {
20-
requestMethod = request.type.toRequestMethod()
20+
try {
21+
val connection = openConnection(url).apply {
22+
requestMethod = request.type.toRequestMethod()
2123

22-
setRequestProperty("Accept", "application/json")
24+
setRequestProperty("Accept", "application/json")
2325

24-
// If we've been given any request headers, add them to the request.
25-
request.headers.forEach {
26-
addRequestProperty(it.key, it.value)
27-
}
26+
// If we've been given any request headers, add them to the request.
27+
request.headers.forEach {
28+
addRequestProperty(it.key, it.value)
29+
}
2830

29-
// If data was provided, this will require us to write it to the output stream.
30-
request.data?.let { data ->
31-
doOutput = true
31+
// If data was provided, this will require us to write it to the output stream.
32+
request.data?.let { data ->
33+
doOutput = true
3234

33-
outputStream.use { outputStream ->
34-
outputStream.write(data.toByteArray(Charsets.UTF_8))
35+
outputStream.use { outputStream ->
36+
outputStream.write(data.toByteArray(Charsets.UTF_8))
37+
}
3538
}
3639
}
37-
}
3840

39-
// A successful response code should be in the [200-299] range.
40-
val responseCode = connection.responseCode
41-
if (responseCode !in SUCCESS_CODE_RANGE_MIN..SUCCESS_CODE_RANGE_MAX) {
42-
return NetworkResponse(responseCode)
43-
}
41+
// A successful response code should be in the [200-299] range. If we receive something outside that range, then
42+
// we should be reading from the errorStream rather than the standard inputStream.
43+
val responseCode = connection.responseCode
44+
val responseStream = if (NetworkSession.isSuccess(responseCode)) {
45+
connection.inputStream
46+
} else {
47+
connection.errorStream
48+
}
4449

45-
// We expect the response to be a String.
46-
val responseText = connection.inputStream.bufferedReader().use { it.readText() }
47-
return NetworkResponse(responseCode, responseText)
50+
// We expect the response to be a String.
51+
val responseText = runCatching {
52+
responseStream.bufferedReader().use { it.readText() }
53+
}.getOrDefault("")
54+
return NetworkResponse(responseCode, responseText)
55+
} catch (ex: IOException) {
56+
// If we're unable to make a request, e.g. due to lack of connection, we will simply report an internal
57+
// error.
58+
return NetworkResponse(HttpURLConnection.HTTP_INTERNAL_ERROR)
59+
}
4860
}
4961

5062
/**
@@ -62,9 +74,4 @@ public open class DefaultNetworkSession : NetworkSession {
6274
NetworkRequestType.GET -> "GET"
6375
NetworkRequestType.POST -> "POST"
6476
}
65-
66-
private companion object {
67-
const val SUCCESS_CODE_RANGE_MIN = HttpURLConnection.HTTP_OK
68-
const val SUCCESS_CODE_RANGE_MAX = 299
69-
}
7077
}

sdk/src/main/java/com/uid2/network/NetworkSession.kt

+12
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,16 @@ public interface NetworkSession {
5757
* @return The [NetworkResponse] which contains the outcome of the attempted request.
5858
*/
5959
public fun loadData(url: URL, request: NetworkRequest): NetworkResponse
60+
61+
public companion object {
62+
private const val SUCCESS_CODE_RANGE_MIN = 200
63+
private const val SUCCESS_CODE_RANGE_MAX = 299
64+
65+
/**
66+
* Function to determine if a given response code is considered a "success".
67+
*/
68+
@JvmStatic
69+
public fun isSuccess(responseCode: Int): Boolean =
70+
responseCode in SUCCESS_CODE_RANGE_MIN..SUCCESS_CODE_RANGE_MAX
71+
}
6072
}

0 commit comments

Comments
 (0)