Source Edit

The std/oserrors module implements OS error reporting.

Imports

winlean

Types

  1. OSErrorCode = distinct int32

Specifies an OS Error Code. Source Edit

Procs

  1. proc `$`(err: OSErrorCode): string {.borrow, ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc `==`(err1, err2: OSErrorCode): bool {.borrow, ...raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc newOSError(errorCode: OSErrorCode; additionalInfo = ""): owned(ref OSError) {.
  2. 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:

Source Edit

  1. proc osErrorMsg(errorCode: OSErrorCode): string {....raises: [], tags: [],
  2. 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:

Example:

  1. when defined(linux):
  2. assert osErrorMsg(OSErrorCode(0)) == ""
  3. assert osErrorMsg(OSErrorCode(1)) == "Operation not permitted"
  4. assert osErrorMsg(OSErrorCode(2)) == "No such file or directory"

Source Edit

  1. 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:

Source Edit

  1. proc raiseOSError(errorCode: OSErrorCode; additionalInfo = "") {.noinline,
  2. ...raises: [OSError], tags: [], forbids: [].}

Raises an OSError exception.

Read the description of the newOSError proc: owned(ref OSError)”) to learn how the exception object is created.

Source Edit