11/*
2- * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+ * Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33 */
44
55package io.ktor.client.engine.darwin.internal
@@ -8,20 +8,22 @@ import io.ktor.client.engine.darwin.*
88import io.ktor.client.request.*
99import io.ktor.http.*
1010import io.ktor.util.date.*
11- import io.ktor.utils.io.InternalAPI
11+ import io.ktor.utils.io.*
1212import io.ktor.utils.io.core.*
1313import io.ktor.websocket.*
1414import kotlinx.cinterop.ExperimentalForeignApi
1515import kotlinx.cinterop.UnsafeNumber
1616import kotlinx.cinterop.convert
1717import kotlinx.coroutines.*
18+ import kotlinx.coroutines.CancellationException
1819import kotlinx.coroutines.channels.Channel
1920import kotlinx.coroutines.channels.ReceiveChannel
2021import kotlinx.coroutines.channels.SendChannel
2122import kotlinx.coroutines.channels.consumeEach
2223import kotlinx.io.readByteArray
2324import platform.Foundation.*
2425import platform.darwin.NSInteger
26+ import platform.posix.EMSGSIZE
2527import kotlin.coroutines.CoroutineContext
2628import kotlin.coroutines.resume
2729import kotlin.coroutines.resumeWithException
@@ -204,7 +206,7 @@ internal class DarwinWebsocketSession(
204206 return
205207 }
206208
207- val exception = DarwinHttpRequestException (error)
209+ val exception = convertWebsocketError (error)
208210 response.completeExceptionally(exception)
209211 socketJob.completeExceptionally(exception)
210212 }
@@ -239,7 +241,7 @@ private suspend fun NSURLSessionWebSocketTask.receiveMessage(): NSURLSessionWebS
239241 return @receiveMessageWithCompletionHandler
240242 }
241243
242- it.resumeWithException(DarwinHttpRequestException (error))
244+ it.resumeWithException(convertWebsocketError (error))
243245 return @receiveMessageWithCompletionHandler
244246 }
245247 if (message == null ) {
@@ -254,3 +256,11 @@ private suspend fun NSURLSessionWebSocketTask.receiveMessage(): NSURLSessionWebS
254256@OptIn(UnsafeNumber ::class )
255257@Suppress(" REDUNDANT_CALL_OF_CONVERSION_METHOD" )
256258internal fun NSURLSessionTask.getStatusCode () = (response() as NSHTTPURLResponse ? )?.statusCode?.toInt()
259+
260+ @OptIn(UnsafeNumber ::class , ExperimentalForeignApi ::class )
261+ private fun convertWebsocketError (error : NSError ): Exception = when {
262+ error.domain == NSPOSIXErrorDomain && error.code.convert<Int >() == EMSGSIZE -> {
263+ FrameTooBigException (frameSize = - 1L , DarwinHttpRequestException (error))
264+ }
265+ else -> DarwinHttpRequestException (error)
266+ }
0 commit comments