Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include NSError code, domain and HTTP Status code in DarwinWebSocketException message #209

Merged
merged 10 commits into from
May 18, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ private class IosWebSocketListener(
didCompleteWithError: NSError?
) {
if (isConnecting) {
val ex = WebSocketConnectionException(url, cause = didCompleteWithError?.toIosWebSocketException())
val ex =
WebSocketConnectionException(url, cause = didCompleteWithError?.toWebSocketHandshakeException(task.response))
completeConnection {
resumeWithException(ex)
}
Expand Down Expand Up @@ -127,7 +128,6 @@ private class IosWebSocketListener(
incomingFrames.close()
}
}

private class IosWebSocketConnection(
override val url: String,
override val incomingFrames: Flow<WebSocketFrame>,
Expand All @@ -136,7 +136,7 @@ private class IosWebSocketConnection(

// no clear way to know if the websocket was closed by the peer, and we can't even fail in sendMessage reliably
override val canSend: Boolean = true

override suspend fun sendText(frameText: String) {
sendMessage(NSURLSessionWebSocketMessage(frameText))
}
Expand Down Expand Up @@ -248,9 +248,40 @@ private fun NSData.toByteArray(): ByteArray {
}
}

private fun NSError.toIosWebSocketException() = DarwinWebSocketException(this)
private fun NSError.toIosWebSocketException(): WebSocketException = DarwinWebSocketException(this)

private fun NSError.toWebSocketHandshakeException(
urlResponse: NSURLResponse? = null
): WebSocketException {
val httpResponse = urlResponse as? NSHTTPURLResponse
val statusCode = httpResponse?.statusCode?.toInt()
return DarwinWebSocketHandshakeException(this, statusCode)
}

/**
* A [WebSocketException] caused by a darwin [NSError]. It contains details about the actual error cause.
* A [WebSocketException] caused by a darwin [NSError].
* It contains details about the actual error cause.
*/
class DarwinWebSocketException(val nsError: NSError) : WebSocketException(nsError.localizedDescription)
class DarwinWebSocketException(
val nsError: NSError,
) : WebSocketException(nsError.description ?: nsError.localizedDescription)

/**
* A [WebSocketException] caused by a darwin [NSError], during the handshake phase.
* It contains details about the actual error cause, as well as the [httpStatusCode], if applicable.
*/
class DarwinWebSocketHandshakeException(
val nsError: NSError,
val httpStatusCode: Int?,
) : WebSocketException(handshakeExceptionMessage(nsError, httpStatusCode))

private fun handshakeExceptionMessage(nsError: NSError, httpStatusCode: Int?): String {
// [baseMessage] will look something like:
// Error Domain=<domain> Code=<code> UserInfo={NSLocalizedDescription=<localized_description>}
val baseMessage = nsError.description ?: nsError.localizedDescription
return if (httpStatusCode != null) {
"$baseMessage HTTP Status Code=$httpStatusCode"
} else {
baseMessage
}
}