Skip to content

Commit

Permalink
Merge pull request #107 from armanbilge/pr/better-errors
Browse files Browse the repository at this point in the history
Improve error reporting with `strerror`
  • Loading branch information
armanbilge committed Aug 28, 2023
2 parents 8bd84d5 + 56725ed commit 3b372b9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 3 additions & 1 deletion uring/src/main/scala/fs2/io/uring/IOExceptionHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package fs2.io.uring
import java.io.IOException
import java.net.ConnectException
import java.net.BindException
import scala.scalanative.posix.string._
import scala.scalanative.unsafe._

private[uring] object IOExceptionHelper {

Expand All @@ -29,7 +31,7 @@ private[uring] object IOExceptionHelper {
new BindException("Cannot assign requested address")
case 111 => // ECONNREFUSED
new ConnectException("Connection refused")
case _ => new IOException(errno.toString)
case _ => new IOException(fromCString(strerror(errno)))
}

}
12 changes: 6 additions & 6 deletions uring/src/main/scala/fs2/io/uring/net/ResizableBuffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

package fs2.io.uring.net
package fs2.io.uring
package net

import cats.effect.kernel.Resource
import cats.effect.kernel.Sync
import cats.syntax.all._

import scala.scalanative.libc.errno._
import scala.scalanative.libc.stdlib._
Expand All @@ -32,15 +32,15 @@ private[net] final class ResizableBuffer[F[_]] private (

def get(size: Int): F[Ptr[Byte]] = F.delay {
if (size <= this.size)
F.pure(ptr)
ptr
else {
ptr = realloc(ptr, size.toUInt)
this.size = size
if (ptr == null)
F.raiseError[Ptr[Byte]](new RuntimeException(s"realloc: ${errno}"))
else F.pure(ptr)
throw IOExceptionHelper(errno)
else ptr
}
}.flatten
}

}

Expand Down
3 changes: 1 addition & 2 deletions uring/src/main/scala/fs2/io/uring/net/UringSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import fs2.io.net.Socket
import fs2.io.uring.unsafe.uringOps._
import fs2.io.uring.unsafe.util._

import java.io.IOException
import scala.scalanative.libc.errno._
import scala.scalanative.posix.sys.socket._
import scala.scalanative.posix.errno._
Expand Down Expand Up @@ -109,7 +108,7 @@ private[net] object UringSocket {
F.delay {
SocketAddressHelpers.toSocketAddress { (addr, len) =>
if (getsockname(fd, addr, len) == -1)
Left(new IOException(s"getsockname: ${errno}"))
Left(IOExceptionHelper(errno))
else
Either.unit
}
Expand Down

0 comments on commit 3b372b9

Please sign in to comment.