Module Posix_errno

POSIX errno handling

Errno Type

type t = [
  1. | `E2BIG
  2. | `EACCES
  3. | `EADDRINUSE
  4. | `EADDRNOTAVAIL
  5. | `EAFNOSUPPORT
  6. | `EAGAIN
  7. | `EALREADY
  8. | `EBADF
  9. | `EBADMSG
  10. | `EBUSY
  11. | `ECANCELED
  12. | `ECHILD
  13. | `ECONNABORTED
  14. | `ECONNREFUSED
  15. | `ECONNRESET
  16. | `EDEADLK
  17. | `EDESTADDRREQ
  18. | `EDOM
  19. | `EDQUOT
  20. | `EEXIST
  21. | `EFAULT
  22. | `EFBIG
  23. | `EHOSTDOWN
  24. | `EHOSTUNREACH
  25. | `EIDRM
  26. | `EILSEQ
  27. | `EINPROGRESS
  28. | `EINTR
  29. | `EINVAL
  30. | `EIO
  31. | `EISCONN
  32. | `EISDIR
  33. | `ELOOP
  34. | `EMFILE
  35. | `EMSGSIZE
  36. | `EMULTIHOP
  37. | `ENAMETOOLONG
  38. | `ENETDOWN
  39. | `ENETRESET
  40. | `ENETUNREACH
  41. | `ENFILE
  42. | `ENOBUFS
  43. | `ENODATA
  44. | `ENODEV
  45. | `ENOENT
  46. | `ENOEXEC
  47. | `ENOLCK
  48. | `ENOMEM
  49. | `ENOMSG
  50. | `ENOPROTOOPT
  51. | `ENOSPC
  52. | `ENOSR
  53. | `ENOSTR
  54. | `ENOSYS
  55. | `ENOTCONN
  56. | `ENOTDIR
  57. | `ENOTEMPTY
  58. | `ENOTRECOVERABLE
  59. | `ENOTSOCK
  60. | `ENOTSUP
  61. | `ENOTTY
  62. | `ENXIO
  63. | `EOPNOTSUPP
  64. | `EOVERFLOW
  65. | `EOWNERDEAD
  66. | `EPERM
  67. | `EPFNOSUPPORT
  68. | `EPIPE
  69. | `EPROTO
  70. | `EPROTONOSUPPORT
  71. | `EPROTOTYPE
  72. | `ERANGE
  73. | `EREMOTE
  74. | `EROFS
  75. | `ESHUTDOWN
  76. | `ESOCKTNOSUPPORT
  77. | `ESPIPE
  78. | `ESRCH
  79. | `ESTALE
  80. | `ETIME
  81. | `ETIMEDOUT
  82. | `ETOOMANYREFS
  83. | `ETXTBSY
  84. | `EUSERS
  85. | `EWOULDBLOCK
  86. | `EXDEV
  87. | `EBADE
  88. | `EBADFD
  89. | `EBADR
  90. | `EBADRQC
  91. | `EBADSLT
  92. | `ECHRNG
  93. | `ECOMM
  94. | `EDEADLOCK
  95. | `EHWPOISON
  96. | `EISNAM
  97. | `EKEYEXPIRED
  98. | `EKEYREJECTED
  99. | `EKEYREVOKED
  100. | `EL2HLT
  101. | `EL2NSYNC
  102. | `EL3HLT
  103. | `EL3RST
  104. | `ELIBACC
  105. | `ELIBBAD
  106. | `ELIBEXEC
  107. | `ELIBMAX
  108. | `ELIBSCN
  109. | `ELNRNG
  110. | `EMEDIUMTYPE
  111. | `ENOANO
  112. | `ENOKEY
  113. | `ENOMEDIUM
  114. | `ENONET
  115. | `ENOPKG
  116. | `ENOTBLK
  117. | `ENOTUNIQ
  118. | `EREMCHG
  119. | `EREMOTEIO
  120. | `ERESTART
  121. | `ERFKILL
  122. | `ESTRPIPE
  123. | `ETOOBIG
  124. | `EUCLEAN
  125. | `EUNATCH
  126. | `EXFULL
  127. | `EAUTH
  128. | `EBADRPC
  129. | `EFTYPE
  130. | `ENEEDAUTH
  131. | `ENOCSI
  132. | `EPROCLIM
  133. | `EPROCUNAVAIL
  134. | `EPROGMISMATCH
  135. | `EPROGUNAVAIL
  136. | `ERPCMISMATCH
  137. | `EATTR
  138. | `EBADARCH
  139. | `EBADEXEC
  140. | `EBADMACHO
  141. | `EDEVERR
  142. | `ENOATTR
  143. | `ENOPOLICY
  144. | `EPWROFF
  145. | `ESHLIBVERS
  146. | `EOTHER
  147. | `EUNKNOWN of int
]

Comprehensive errno type covering Linux, BSD, macOS, and Windows values

Errno Conversion

val of_int : int -> t

Convert errno code to variant

val to_int : t -> int

Convert variant to errno code

Errno Access

val get_errno : unit -> t

Get current errno value

val get_errno_int : unit -> int

Get current errno as int

val reset_errno : unit -> unit

Reset errno to 0

Error Raising

val raise_on_error : ?call:string -> (unit -> 'a) -> ('a -> bool) -> 'a

Generic error-raising function.

raise_on_error ?call f check calls f() and checks the result with check. If check returns true, raises a Unix_error based on the current errno.

  • parameter call

    Optional name of the calling function (for error messages)

  • parameter f

    Function to execute

  • parameter check

    Function to check if result indicates an error

  • returns

    The result of f() if no error occurred

  • raises Unix_error

    if check returns true

Specialized Error Checkers

val raise_on_neg : ?call:string -> (unit -> int) -> int

Check for negative integer return (common for syscalls). Raises Unix_error if result < 0.

val raise_on_null : ?call:string -> (unit -> 'a Ctypes.ptr) -> 'a Ctypes.ptr

Check for null pointer return (for C functions). Raises Unix_error if result is null.

val raise_on_zero : ?call:string -> (unit -> int) -> int

Check for zero return value (some functions return 0 on error). Raises Unix_error if result = 0.

val raise_on_none : ?call:string -> (unit -> 'a option) -> 'a

Check for None return value. Raise Unix_error if result = None.

Unix Error Conversion

val to_unix_error : t -> Unix.error

Convert errno variant to Unix.error

val int_to_unix_error : int -> Unix.error

Convert errno int to Unix.error

Error String Functions

val strerror : t -> string

Get error message string for an errno value using strerror. This function is cross-platform (works on both POSIX and Windows) but not thread-safe.

  • parameter errnum

    The errno value to get the message for

  • returns

    Error message string

val strerror_r : ?buflen:int -> t -> string

Get error message string for an errno value using strerror_r. This function is thread-safe but only available on POSIX systems.

  • parameter buflen

    Optional buffer length for error message (default: 1024)

  • parameter errn

    The errno value to get the message for

  • returns

    Error message string

  • raises Invalid_argument

    on Windows where strerror_r is not available

  • raises Unix_error

    if strerror_r fails

Native Definition Detection

val is_native : t -> bool

Check if an errno value is natively defined on this platform. Returns true if the errno is natively defined by the system, false if it's using a placeholder fallback value.

This is useful to determine if an errno constant represents a real system error on the current platform, or if it's just a placeholder value (typically in the 10000+ range) that was assigned because the error doesn't exist on this system.

  • parameter errnum

    The errno value to check

  • returns

    true if natively defined, false if using placeholder