std/oserrors (original) (raw)
The std/oserrors module implements OS error reporting.
Procs
proc newOSError(errorCode: OSErrorCode; additionalInfo = ""): owned(ref OSError) {. noinline, ...raises: [], tags: [], forbids: [].}
Creates a new OSError exception.
The errorCode will determine the message, osErrorMsg proc will be used to get this message.
The error code can be retrieved using the osLastError proc.
If the error code is 0 or an error message could not be retrieved, the message unknown OS error will be used.
See also:
proc osErrorMsg(errorCode: OSErrorCode): string {....raises: [], tags: [], forbids: [].}
Converts an OS error code into a human readable string.
The error code can be retrieved using the osLastError proc.
If conversion fails, or errorCode is 0 then "" will be returned.
See also:
- [raiseOSError proc](#raiseOSError,OSErrorCode,string "proc raiseOSError(errorCode: OSErrorCode; additionalInfo = "")")
- osLastError proc
Example:
when defined(linux): assert osErrorMsg(OSErrorCode(0)) == "" assert osErrorMsg(OSErrorCode(1)) == "Operation not permitted" assert osErrorMsg(OSErrorCode(2)) == "No such file or directory"
proc osLastError(): OSErrorCode {.sideEffect, ...raises: [], tags: [], forbids: [].}
Retrieves the last operating system error code.
This procedure is useful in the event when an OS call fails. In that case this procedure will return the error code describing the reason why the OS call failed. The OSErrorMsg procedure can then be used to convert this code into a string.
**Warning:**The behaviour of this procedure varies between Windows and POSIX systems. On Windows some OS calls can reset the error code to
0
causing this procedure to return
0
. It is therefore advised to call this procedure immediately after an OS call fails. On POSIX systems this is not a problem.
See also:
- osErrorMsg proc
- [raiseOSError proc](#raiseOSError,OSErrorCode,string "proc raiseOSError(errorCode: OSErrorCode; additionalInfo = "")") Source Edit
proc raiseOSError(errorCode: OSErrorCode; additionalInfo = "") {.noinline, ...raises: [OSError], tags: [], forbids: [].}
Raises an OSError exception.
Read the description of the [newOSError proc](#newOSError,OSErrorCode,string "proc newOSError(errorCode: OSErrorCode; additionalInfo = ""): owned(ref OSError)") to learn how the exception object is created.