doc-next-gen

Init.System.IOError

Module docstring

{}

IO.Error inductive
Full source
/--
Exceptions that may be thrown in the `IO` monad.

Many of the constructors of `IO.Error` correspond to POSIX error numbers. In these cases, the
documentation string lists POSIX standard error macros that correspond to the error. This list is
not necessarily exhaustive, and these constructor includes a field for the underlying error number.
-/
-- Imitates the structure of IOErrorType in Haskell:
-- https://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO-Error.html#t:IOErrorType
inductive IO.Error where
  /--
  The operation failed because a file already exists.

  This corresponds to POSIX errors `EEXIST`, `EINPROGRESS`, and `EISCONN`.
  -/
  | alreadyExists (filename : Option String) (osCode : UInt32) (details : String)
  /--
  Some error not covered by the other constructors of `IO.Error` occurred.

  This also includes POSIX error `EFAULT`.
  -/
  | otherError (osCode : UInt32) (details : String)
  /--
  A necessary resource was busy.

  This corresponds to POSIX errors `EADDRINUSE`, `EBUSY`, `EDEADLK`, and `ETXTBSY`.
  -/
  | resourceBusy (osCode : UInt32) (details : String)
  /--
  A necessary resource is no longer available.

  This corresponds to POSIX errors `ECONNRESET`, `EIDRM`, `ENETDOWN`, `ENETRESET`, `ENOLINK`, and
  `EPIPE`.
  -/
  | resourceVanished (osCode : UInt32) (details : String)
  /--
  An operation was not supported.

  This corresponds to POSIX errors `EADDRNOTAVAIL`, `EAFNOSUPPORT`, `ENODEV`, `ENOPROTOOPT`
  `ENOSYS`, `EOPNOTSUPP`, `ERANGE`, `ESPIPE`, and `EXDEV`.
  -/
  | unsupportedOperation (osCode : UInt32) (details : String)
  /--
  The operation failed due to a hardware problem, such as an I/O error.

  This corresponds to the POSIX error `EIO`.
  -/
  | hardwareFault (osCode : UInt32) (details : String)
  /--
  A constraint required by an operation was not satisfied (e.g. a directory was not empty).

  This corresponds to the POSIX error `ENOTEMPTY`.
  -/
  | unsatisfiedConstraints (osCode : UInt32) (details : String)
  /--
  An inappropriate I/O control operation was attempted.

  This corresponds to the POSIX error `ENOTTY`.
  -/
  | illegalOperation (osCode : UInt32) (details : String)
  /--
  A protocol error occurred.

  This corresponds to the POSIX errors `EPROTO`, `EPROTONOSUPPORT`, and `EPROTOTYPE`.
  -/
  | protocolError (osCode : UInt32) (details : String)
  /--
  An operation timed out.

  This corresponds to the POSIX errors `ETIME`, and `ETIMEDOUT`.
  -/
  | timeExpired (osCode : UInt32) (details : String)
  /--
  The operation was interrupted.

  This corresponds to the POSIX error `EINTR`.
  -/
  | interrupted (filename : String) (osCode : UInt32) (details : String)
  /--
  No such file or directory.

  This corresponds to the POSIX error `ENOENT`.
  -/
  | noFileOrDirectory (filename : String) (osCode : UInt32) (details : String)
  /--
  An argument to an I/O operation was invalid.

  This corresponds to the POSIX errors `ELOOP`, `ENAMETOOLONG`, `EDESTADDRREQ`, `EILSEQ`, `EINVAL`, `EDOM`, `EBADF`
  `ENOEXEC`, `ENOSTR`, `ENOTCONN`, and `ENOTSOCK`.
  -/
  | invalidArgument (filename : Option String) (osCode : UInt32) (details : String)

  /--
  An operation failed due to insufficient permissions.

  This corresponds to the POSIX errors `EACCES`, `EROFS`, `ECONNABORTED`, `EFBIG`, and `EPERM`.
  -/
  | permissionDenied (filename : Option String) (osCode : UInt32) (details : String)

  /--
  A resource was exhausted.

  This corresponds to the POSIX errors  `EMFILE`, `ENFILE`, `ENOSPC`, `E2BIG`, `EAGAIN`, `EMLINK`,
  `EMSGSIZE`, `ENOBUFS`, `ENOLCK`, `ENOMEM`, and `ENOSR`.
  -/
  | resourceExhausted (filename : Option String) (osCode : UInt32) (details : String)

  /--
  An argument was the wrong type (e.g. a directory when a file was required).

  This corresponds to the POSIX errors `EISDIR`, `EBADMSG`, and `ENOTDIR`.
  -/
  | inappropriateType (filename : Option String) (osCode : UInt32) (details : String)

  /--
  A required resource does not exist.

  This corresponds to the POSIX errors `ENXIO`, `EHOSTUNREACH`, `ENETUNREACH`, `ECHILD`,
  `ECONNREFUSED`, `ENODATA`, `ENOMSG`, and `ESRCH`.
  -/
  | noSuchThing (filename : Option String) (osCode : UInt32) (details : String)

  /-- An unexpected end-of-file marker was encountered. -/
  | unexpectedEof
  /-- Some other error occurred. -/
  | userError (msg : String)
Input/Output Error Exceptions
Informal description
The inductive type `IO.Error` represents exceptions that may be thrown during input/output operations in the `IO` monad. Many of these exceptions correspond to POSIX standard error numbers, though the documentation notes this correspondence may not be exhaustive. Each constructor includes a field for the underlying error number.
instInhabitedError instance
: Inhabited IO.Error
Full source
instance : Inhabited IO.Error where
  default := .userError "(`Inhabited.default` for `IO.Error`)"
Inhabitedness of I/O Error Type
Informal description
The type `IO.Error` is inhabited, meaning there exists at least one value of this type.
IO.userError definition
(s : String) : IO.Error
Full source
/--
Constructs an `IO.Error` from a string.

`IO.Error` is the type of exceptions thrown by the `IO` monad.
-/
@[export lean_mk_io_user_error]
def IO.userError (s : String) : IO.Error :=
  IO.Error.userError s
User-defined I/O error constructor
Informal description
The function constructs an `IO.Error` from a given string `s`, representing a user-defined error message in input/output operations.
instCoeStringError instance
: Coe String IO.Error
Full source
instance : Coe String IO.Error := ⟨IO.userError⟩
String to I/O Error Coercion
Informal description
There is a canonical way to convert a string into an `IO.Error` value.
IO.Error.mkAlreadyExistsFile definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_already_exists_file]
def mkAlreadyExistsFile : StringUInt32StringIO.Error :=
  alreadyExistsalreadyExists ∘ some
File already exists I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a file already exists condition, taking a file path string, an error number, and an additional message string as arguments.
IO.Error.mkEofError definition
: Unit → IO.Error
Full source
@[export lean_mk_io_error_eof]
def mkEofError : UnitIO.Error := fun _ =>
  unexpectedEof
End-of-file input/output error constructor
Informal description
The function creates an input/output error representing an unexpected end-of-file condition. It takes a unit value (no arguments) and returns an `IO.Error` value indicating this specific error condition.
IO.Error.mkInappropriateTypeFile definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_inappropriate_type_file]
def mkInappropriateTypeFile : StringUInt32StringIO.Error :=
  inappropriateTypeinappropriateType ∘ some
Inappropriate file type I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing an inappropriate type error for a file operation, given a file path string, an error number, and an additional message string as arguments.
IO.Error.mkInterrupted definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_interrupted]
def mkInterrupted : StringUInt32StringIO.Error :=
  interrupted
Interrupted I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing an interrupted operation, given a string description, an unsigned 32-bit integer error number, and another string context.
IO.Error.mkInvalidArgumentFile definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_invalid_argument_file]
def mkInvalidArgumentFile : StringUInt32StringIO.Error :=
  invalidArgumentinvalidArgument ∘ some
Invalid argument I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing an invalid argument error, given a string description, an unsigned 32-bit integer error number, and another string context.
IO.Error.mkNoFileOrDirectory definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_no_file_or_directory]
def mkNoFileOrDirectory : StringUInt32StringIO.Error :=
  noFileOrDirectory
"No file or directory" I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a "no file or directory" error, given a string description, an unsigned 32-bit integer error number, and another string context.
IO.Error.mkNoSuchThingFile definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_no_such_thing_file]
def mkNoSuchThingFile : StringUInt32StringIO.Error :=
  noSuchThingnoSuchThing ∘ some
"No such thing" file I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a "no such thing" error for a file, given a string description, an unsigned 32-bit integer error number, and another string context.
IO.Error.mkPermissionDeniedFile definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_permission_denied_file]
def mkPermissionDeniedFile : StringUInt32StringIO.Error :=
  permissionDeniedpermissionDenied ∘ some
"Permission denied" file I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a "permission denied" error for a file, given a string description, an unsigned 32-bit integer error number, and another string context.
IO.Error.mkResourceExhaustedFile definition
: String → UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_resource_exhausted_file]
def mkResourceExhaustedFile : StringUInt32StringIO.Error :=
  resourceExhaustedresourceExhausted ∘ some
"Resource exhausted" file I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a "resource exhausted" error for a file, given a string description, an unsigned 32-bit integer error number, and another string context.
IO.Error.mkUnsupportedOperation definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_unsupported_operation]
def mkUnsupportedOperation : UInt32StringIO.Error :=
  unsupportedOperation
"Unsupported operation" I/O error constructor
Informal description
The function constructs an `IO.Error` of type "unsupported operation" with an optional error number and description string.
IO.Error.mkResourceExhausted definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_resource_exhausted]
def mkResourceExhausted : UInt32StringIO.Error :=
  resourceExhausted none
"Resource exhausted" I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a "resource exhausted" error, given an unsigned 32-bit integer error number and a string description.
IO.Error.mkAlreadyExists definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_already_exists]
def mkAlreadyExists : UInt32StringIO.Error :=
  alreadyExists none
"Already exists" I/O error constructor
Informal description
The function constructs an `IO.Error` of type "already exists" with an optional error number and description string.
IO.Error.mkInappropriateType definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_inappropriate_type]
def mkInappropriateType : UInt32StringIO.Error :=
  inappropriateType none
"Inappropriate type" I/O error constructor
Informal description
The function constructs an `IO.Error` of type "inappropriate type" with an optional error number and description string.
IO.Error.mkNoSuchThing definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_no_such_thing]
def mkNoSuchThing : UInt32StringIO.Error :=
  noSuchThing none
"No such thing" I/O error constructor
Informal description
The function constructs an `IO.Error` of type "no such thing" with an optional error number and description string.
IO.Error.mkResourceVanished definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_resource_vanished]
def mkResourceVanished : UInt32StringIO.Error :=
  resourceVanished
"Resource vanished" I/O error constructor
Informal description
The function constructs an `IO.Error` of type "resource vanished" with an optional error number and description string.
IO.Error.mkResourceBusy definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_resource_busy]
def mkResourceBusy : UInt32StringIO.Error :=
  resourceBusy
"Resource busy" I/O error constructor
Informal description
The function constructs an `IO.Error` of type "resource busy" with an unsigned 32-bit integer error code and an optional descriptive string message.
IO.Error.mkInvalidArgument definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_invalid_argument]
def mkInvalidArgument : UInt32StringIO.Error :=
  invalidArgument none
Invalid Argument I/O Error Constructor
Informal description
The constructor `IO.Error.mkInvalidArgument` creates an input/output error of type `invalidArgument` with an unsigned 32-bit integer error code and an optional descriptive string message.
IO.Error.mkOtherError definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_other_error]
def mkOtherError : UInt32StringIO.Error :=
  otherError
Other I/O Error Constructor
Informal description
The constructor `IO.Error.mkOtherError` creates an input/output error of type `otherError` with an unsigned 32-bit integer error code and a descriptive string message.
IO.Error.mkPermissionDenied definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_permission_denied]
def mkPermissionDenied : UInt32StringIO.Error :=
  permissionDenied none
Permission Denied I/O Error Constructor
Informal description
The constructor `IO.Error.mkPermissionDenied` creates an input/output error of type `permissionDenied` with an unsigned 32-bit integer error code and an optional descriptive string message.
IO.Error.mkHardwareFault definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_hardware_fault]
def mkHardwareFault : UInt32StringIO.Error :=
  hardwareFault
Hardware fault I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a hardware fault error, taking an unsigned 32-bit integer error code and a string error message as arguments.
IO.Error.mkUnsatisfiedConstraints definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_unsatisfied_constraints]
def mkUnsatisfiedConstraints : UInt32StringIO.Error :=
  unsatisfiedConstraints
Unsatisfied constraints I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing an unsatisfied constraints error, taking an unsigned 32-bit integer error code and a string error message as arguments.
IO.Error.mkIllegalOperation definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_illegal_operation]
def mkIllegalOperation : UInt32StringIO.Error :=
  illegalOperation
Illegal operation I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing an illegal operation error, taking an unsigned 32-bit integer error code and a string error message as arguments.
IO.Error.mkProtocolError definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_protocol_error]
def mkProtocolError : UInt32StringIO.Error :=
  protocolError
Protocol error constructor for IO exceptions
Informal description
The function constructs an input/output error of type `IO.Error` representing a protocol error, taking an unsigned 32-bit integer error code and a string error message as arguments.
IO.Error.mkTimeExpired definition
: UInt32 → String → IO.Error
Full source
@[export lean_mk_io_error_time_expired]
def mkTimeExpired : UInt32StringIO.Error :=
  timeExpired
Time expired I/O error constructor
Informal description
The function constructs an input/output error of type `IO.Error` representing a time expired error, taking an unsigned 32-bit integer error code and a string error message as arguments.
IO.Error.fopenErrorToString definition
(gist fn : String) (code : UInt32) : Option String → String
Full source
def fopenErrorToString (gist fn : String) (code : UInt32) : Option StringString
  | somesome details => downCaseFirst gist ++ " (error code: " ++ toString code ++ ", " ++ downCaseFirst details ++ ")\n  file: " ++ fn
  | none => downCaseFirst gist ++ " (error code: " ++ toString code ++ ")\n  file: " ++ fn
File open error to string conversion
Informal description
The function takes a file name `fn`, an error code `code`, and an optional detailed error message, and returns a formatted string describing the file open error. If a detailed message is provided, it is included in lowercase first letter format along with the error code and file name. If no detailed message is provided, only the error code and file name are included.
IO.Error.otherErrorToString definition
(gist : String) (code : UInt32) : Option String → String
Full source
def otherErrorToString (gist : String) (code : UInt32) : Option StringString
  | somesome details => downCaseFirst gist ++ " (error code: " ++ toString code ++ ", " ++ downCaseFirst details ++ ")"
  | none => downCaseFirst gist ++ " (error code: " ++ toString code ++ ")"
Formatting function for other error messages
Informal description
The function takes a string `gist`, an unsigned 32-bit integer `code`, and an optional string `details`, and returns a formatted error message. If `details` is provided (`some details`), the message is constructed by concatenating the lowercased first letter of `gist`, the string `" (error code: "`, the string representation of `code`, the string `", "`, the lowercased first letter of `details`, and the string `")"`. If `details` is not provided (`none`), the message is constructed by concatenating the lowercased first letter of `gist`, the string `" (error code: "`, the string representation of `code`, and the string `")"`.
IO.Error.toString definition
: IO.Error → String
Full source
/--
Converts an `IO.Error` to a descriptive string.

`IO.Error.userError` is converted to its embedded message. The other constructors are converted in a
way that preserves structured information, such as error codes and filenames, that can help
diagnose the issue.
-/
@[export lean_io_error_to_string]
def toString : IO.ErrorString
  | unexpectedEof                            => "end of file"
  | inappropriateType (somesome fn) code details => fopenErrorToString "inappropriate type" fn code details
  | inappropriateType none code details      => otherErrorToString "inappropriate type" code details
  | interrupted fn code details              => fopenErrorToString "interrupted system call" fn code details
  | invalidArgument (somesome fn) code details   => fopenErrorToString "invalid argument" fn code details
  | invalidArgument none code details        => otherErrorToString "invalid argument" code details
  | noFileOrDirectory fn code _              => fopenErrorToString "no such file or directory" fn code none
  | noSuchThing (somesome fn) code details       => fopenErrorToString "no such thing" fn code details
  | noSuchThing none code details            => otherErrorToString "no such thing" code details
  | permissionDenied (somesome fn) code details  => fopenErrorToString details fn code none
  | permissionDenied none code details       => otherErrorToString details code none
  | resourceExhausted (somesome fn) code details => fopenErrorToString "resource exhausted" fn code details
  | resourceExhausted none code details      => otherErrorToString "resource exhausted" code details
  | alreadyExists none code details          => otherErrorToString "already exists" code details
  | alreadyExists (somesome fn) code details     => fopenErrorToString "already exists" fn code details
  | otherError code details                  => otherErrorToString details code none
  | resourceBusy code details                => otherErrorToString "resource busy" code details
  | resourceVanished code details            => otherErrorToString "resource vanished" code details
  | hardwareFault code _                     => otherErrorToString "hardware fault" code none
  | illegalOperation code details            => otherErrorToString "illegal operation" code details
  | protocolError code details               => otherErrorToString "protocol error" code details
  | timeExpired code details                 => otherErrorToString "time expired" code details
  | unsatisfiedConstraints code _            => otherErrorToString "directory not empty" code none
  | unsupportedOperation code details        => otherErrorToString "unsupported operation" code details
  | userError msg                            => msg
IO error to string conversion
Informal description
The function converts an `IO.Error` value into a descriptive string. For `IO.Error.userError`, it returns the embedded message directly. For other error types, it formats the error with relevant structured information (such as error codes and filenames) to aid in diagnosis.
IO.Error.instToString instance
: ToString IO.Error
Full source
instance : ToString IO.Error := ⟨ IO.Error.toString ⟩
String Representation of IO Errors
Informal description
The type `IO.Error` has a canonical string representation, where each error is converted to a descriptive message.